diff options
author | Alain Kermis <alain.kermis@sonarsource.com> | 2023-02-17 11:44:37 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-03-15 20:03:02 +0000 |
commit | cba03a5443c9bcbe4066ab8a811e360292e9f66a (patch) | |
tree | 8bd35568d8bfd1a1863d084f88442d300e772427 /server/sonar-db-dao/src/test | |
parent | f05ed0615b9a683f1c7e97baaeafc01399183936 (diff) | |
download | sonarqube-cba03a5443c9bcbe4066ab8a811e360292e9f66a.tar.gz sonarqube-cba03a5443c9bcbe4066ab8a811e360292e9f66a.zip |
SONAR-18472 Resolve telemetry performance issue
Co-authored-by: Jacek Poreda <jacek.poreda@sonarsource.com>
(cherry picked from commit a20f2bce3cc9111152aa810030253db6acbd4af8)
Diffstat (limited to 'server/sonar-db-dao/src/test')
-rw-r--r-- | server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java | 74 | ||||
-rw-r--r-- | server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java | 2 |
2 files changed, 65 insertions, 11 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java index 04f12e74729..13cf0c87aff 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.IntStream; import org.apache.commons.lang.RandomStringUtils; @@ -43,6 +44,7 @@ import static java.util.Collections.singleton; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.groups.Tuple.tuple; +import static org.sonar.api.measures.Metric.ValueType.DATA; import static org.sonar.api.measures.Metric.ValueType.INT; import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; import static org.sonar.db.component.ComponentTesting.newFileDto; @@ -373,7 +375,8 @@ public class LiveMeasureDaoTest { @Test public void get_branch_with_max_ncloc_per_project() { - setupProjectsWithLoc(); + Map<String, MetricDto> metrics = setupMetrics(); + setupProjectsWithLoc(metrics.get("ncloc"), metrics.get("ncloc_language_distribution"), metrics.get("lines")); List<LargestBranchNclocDto> results = underTest.getLargestBranchNclocPerProject(db.getSession()); @@ -386,6 +389,24 @@ public class LiveMeasureDaoTest { } @Test + public void get_loc_language_distribution() { + Map<String, MetricDto> metrics = setupMetrics(); + MetricDto ncloc = metrics.get("ncloc"); + MetricDto nclocLanguageDistribution = metrics.get("ncloc_language_distribution"); + Map<String, ComponentDto> components = setupProjectsWithLoc(ncloc, nclocLanguageDistribution, metrics.get("lines")); + + List<ProjectLocDistributionDto> results = underTest.selectLargestBranchesLocDistribution(db.getSession(), ncloc.getUuid(), nclocLanguageDistribution.getUuid()); + + assertThat(results) + .containsExactlyInAnyOrder( + new ProjectLocDistributionDto(components.get("projectWithTieOnBranchSize").uuid(), components.get("projectWithTieOnBranchSize").uuid(), "java=250;js=0"), + new ProjectLocDistributionDto(components.get("projectWithTieOnOtherBranches").uuid(), components.get("tieBranch1").uuid(), "java=230;js=0"), + new ProjectLocDistributionDto(components.get("projectWithBranchBiggerThanMaster").uuid(), components.get("notMasterBranch").uuid(), "java=100;js=100"), + new ProjectLocDistributionDto(components.get("simpleProject").uuid(), components.get("simpleProject").uuid(), "java=10;js=0"), + new ProjectLocDistributionDto(components.get("projectWithLinesButNoLoc").uuid(), components.get("projectWithLinesButNoLoc").uuid(), "java=0;js=0")); + } + + @Test public void countNcloc_empty() { db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString())); @@ -696,29 +717,55 @@ public class LiveMeasureDaoTest { "componentUuid", "projectUuid", "metricUuid", "value", "textValue", "data"); } - private void setupProjectsWithLoc() { + private Map<String, MetricDto> setupMetrics() { MetricDto ncloc = db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); + MetricDto nclocDistribution = db.measures().insertMetric(m -> m.setKey("ncloc_language_distribution").setValueType(DATA.toString())); MetricDto lines = db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString())); + return Map.of("ncloc", ncloc, + "ncloc_language_distribution", nclocDistribution, + "lines", lines); + } - addProjectWithMeasure("simpleProject", ncloc, 10d); + private Map<String, ComponentDto> setupProjectsWithLoc(MetricDto ncloc, MetricDto nclocDistribution, MetricDto lines) { + ComponentDto simpleProject = addProjectWithMeasure("simpleProject", ncloc, 10d); + addMeasureToComponent(simpleProject, nclocDistribution, "java=10;js=0"); ComponentDto projectWithBranchBiggerThanMaster = addProjectWithMeasure("projectWithBranchBiggerThanMaster", ncloc, 100d); - addBranchToProjectWithMeasure(projectWithBranchBiggerThanMaster,"notMasterBranch", ncloc, 200d); + addMeasureToComponent(projectWithBranchBiggerThanMaster, nclocDistribution, "java=100;js=0"); + + ComponentDto notMasterBranch = addBranchToProjectWithMeasure(projectWithBranchBiggerThanMaster, "notMasterBranch", ncloc, 200d); + addMeasureToComponent(notMasterBranch, nclocDistribution, "java=100;js=100"); ComponentDto projectWithLinesButNoLoc = addProjectWithMeasure("projectWithLinesButNoLoc", lines, 365d); - addMeasureToComponent(projectWithLinesButNoLoc,ncloc,0d,false); + addMeasureToComponent(projectWithLinesButNoLoc, nclocDistribution, "java=0;js=0"); + addMeasureToComponent(projectWithLinesButNoLoc, ncloc, 0d, false); ComponentDto projectWithTieOnBranchSize = addProjectWithMeasure("projectWithTieOnBranchSize", ncloc, 250d); - addBranchToProjectWithMeasure(projectWithTieOnBranchSize,"tieBranch", ncloc, 250d); + addMeasureToComponent(projectWithTieOnBranchSize, nclocDistribution, "java=250;js=0"); + ComponentDto tieBranch = addBranchToProjectWithMeasure(projectWithTieOnBranchSize, "tieBranch", ncloc, 250d); + addMeasureToComponent(tieBranch, nclocDistribution, "java=250;js=0"); ComponentDto projectWithTieOnOtherBranches = addProjectWithMeasure("projectWithTieOnOtherBranches", ncloc, 220d); - addBranchToProjectWithMeasure(projectWithTieOnOtherBranches,"tieBranch1", ncloc, 230d); - addBranchToProjectWithMeasure(projectWithTieOnOtherBranches,"tieBranch2", ncloc, 230d); + addMeasureToComponent(projectWithTieOnOtherBranches, nclocDistribution, "java=220;js=0"); + ComponentDto tieBranch1 = addBranchToProjectWithMeasure(projectWithTieOnOtherBranches, "tieBranch1", ncloc, 230d); + addMeasureToComponent(tieBranch1, nclocDistribution, "java=230;js=0"); + ComponentDto tieBranch2 = addBranchToProjectWithMeasure(projectWithTieOnOtherBranches, "tieBranch2", ncloc, 230d); + addMeasureToComponent(tieBranch2, nclocDistribution, "java=230;js=0"); + + return Map.of("simpleProject", simpleProject, + "projectWithBranchBiggerThanMaster", projectWithBranchBiggerThanMaster, + "notMasterBranch", notMasterBranch, + "projectWithLinesButNoLoc", projectWithLinesButNoLoc, + "projectWithTieOnBranchSize", projectWithTieOnBranchSize, + "tieBranch", tieBranch, + "projectWithTieOnOtherBranches", projectWithTieOnOtherBranches, + "tieBranch1", tieBranch1, + "tieBranch2", tieBranch2); } private ComponentDto addProjectWithMeasure(String projectKey, MetricDto metric, double metricValue) { ComponentDto project = db.components().insertPublicProject(p -> p.setKey(projectKey)); - addMeasureToComponent(project, metric, metricValue,true); + addMeasureToComponent(project, metric, metricValue, true); return project; } @@ -729,9 +776,14 @@ public class LiveMeasureDaoTest { } } - private void addBranchToProjectWithMeasure(ComponentDto project, String branchKey, MetricDto metric, double metricValue) { + private void addMeasureToComponent(ComponentDto component, MetricDto metric, String metricValue) { + db.measures().insertLiveMeasure(component, metric, m -> m.setData(metricValue)); + } + + private ComponentDto addBranchToProjectWithMeasure(ComponentDto project, String branchKey, MetricDto metric, double metricValue) { ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH).setKey(branchKey)); - addMeasureToComponent(branch, metric, metricValue,true); + addMeasureToComponent(branch, metric, metricValue, true); + return branch; } private void assertLocForProject(LargestBranchNclocDto result, String projectKey, String branchKey, long linesOfCode) { diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java index 2ba89f513bc..4e7e97c0cdd 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java @@ -46,6 +46,7 @@ public class MeasureDaoTest { private MetricDto coverage; private MetricDto complexity; private MetricDto ncloc; + private MetricDto nclocDistribution; @Rule @@ -60,6 +61,7 @@ public class MeasureDaoTest { coverage =db.measures().insertMetric(m -> m.setKey("coverage")); complexity = db.measures().insertMetric(m -> m.setKey( "complexity")); ncloc = db.measures().insertMetric(m -> m.setKey("ncloc")); + nclocDistribution = db.measures().insertMetric(m -> m.setKey("ncloc_language_distribution")); } @Test |