Skip to content

SONARJAVA-5695 Report speed of analysis and analysis errors - #5259

Merged
alban-auzeill merged 3 commits into
masterfrom
alban/SONARJAVA-5695
Jul 16, 2025
Merged

SONARJAVA-5695 Report speed of analysis and analysis errors#5259
alban-auzeill merged 3 commits into
masterfrom
alban/SONARJAVA-5695

Conversation

@alban-auzeill

@alban-auzeill alban-auzeill commented Jul 14, 2025

Copy link
Copy Markdown
Member

Comment thread java-frontend/src/main/java/org/sonar/java/ast/JavaAstScanner.java Outdated
@alban-auzeill
alban-auzeill force-pushed the alban/SONARJAVA-5695 branch 2 times, most recently from 932a728 to d7d730b Compare July 16, 2025 06:05

assertThat(extractTelemetryLogs(buildResult))
.matches(patternWithLiteralDot("""
Telemetry java.analysis.generated.success.size_chars: \\d{5}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment that the size of the generated files is not matched exactly, beaucase it varies due depending on the environment.

private VisitorsBridge visitor;
private boolean reportedMisconfiguredVersion = false;

public JavaAstScanner(@Nullable SonarComponents sonarComponents) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it is used only for tests. Let's remove this constructor and use the prod version. (Or at leaste annotate it @VisibleForTesting)

return prefix + suffix;
}

public static int charCount(InputFile inputFile, int defaultValue) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change defaultValue to onError (or something similar, like valueOnError / defaultOnError)

try {
return inputFile.contents().length();
} catch (IOException e) {
LOG.debug("ERROR, failed to get content size for: {}, {}: {}" , inputFile, e.getClass().getSimpleName(), e.getMessage());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error instead of ERROR becuse the latter is a valid log level.

@Test
void char_count() throws IOException {
InputFile inputFile = Mockito.mock(InputFile.class);
Mockito.when(inputFile.contents()).thenReturn("1234");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: import static Mockito.when


@Test
void char_count() throws IOException {
InputFile inputFile = Mockito.mock(InputFile.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let make inputFile a field, create the mock in @Before (alternative,, use @mock).

(when(inputFile) should stay in @Test)

"time_ms", SpeedKeys::timeMsKey
);
artifacts.forEach((artifactName, artifact) -> results.forEach(
(resultName, result) -> metrics.forEach((

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting :)


// There are additional entries, but we do not test them.
assertThat(telemetry.toMap()).contains(
String codeType = (onType == InputFile.Type.MAIN) ? "main" : "test";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move the assertion to the each test, so we do not need this logic here.


}

public static Map<String, String> normalizeTimeMs(Map<String, String> map) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use more features of assertJ, so we can assert that we have the right keys without checking the values.

@alban-auzeill
alban-auzeill force-pushed the alban/SONARJAVA-5695 branch from 48f875a to aa2a1f5 Compare July 16, 2025 10:04
@alban-auzeill
alban-auzeill force-pushed the alban/SONARJAVA-5695 branch from aa2a1f5 to bb5dd8d Compare July 16, 2025 10:13
@sonarqube-next

Copy link
Copy Markdown
Contributor

InputFile inputFile = Mockito.mock(InputFile.class);
Mockito
.doReturn("/tmp/Example.java")
InputFile inputFile = mock(InputFile.class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change anything, but I just learned https://stackoverflow.com/questions/20353846/mockito-difference-between-doreturn-and-when and wonder if we should have when(...).thenReturn(...) here. We have a mock and not a spy here, so when/thenReturn and doReturn/when work the same in this case, but the latter is more idiomatic.

@alban-auzeill
alban-auzeill merged commit ff79c5b into master Jul 16, 2025
17 checks passed
@alban-auzeill
alban-auzeill deleted the alban/SONARJAVA-5695 branch July 16, 2025 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants