From c3e3df59aca2702177b8e9ba9ce925c1f4a27714 Mon Sep 17 00:00:00 2001 From: Godin Date: Mon, 6 Dec 2010 14:03:32 +0000 Subject: [PATCH] Improve performance of NewViolationsDecorator --- .../timemachine/NewViolationsDecorator.java | 68 +++++++++---------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index 30cae39532b..f9c62d786e0 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -21,7 +21,7 @@ package org.sonar.plugins.core.timemachine; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Maps; -import org.apache.commons.lang.ObjectUtils; +import com.google.common.collect.Multimap; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.DependedUpon; @@ -42,6 +42,8 @@ public class NewViolationsDecorator implements Decorator { // temporary data for current resource private Map ruleToLevel = Maps.newHashMap(); + private Multimap violationsByPriority = ArrayListMultimap.create(); + private Multimap violationsByRule = ArrayListMultimap.create(); public NewViolationsDecorator(TimeMachineConfiguration timeMachineConfiguration) { this.timeMachineConfiguration = timeMachineConfiguration; @@ -58,14 +60,25 @@ public class NewViolationsDecorator implements Decorator { } public void decorate(Resource resource, DecoratorContext context) { - resetCache(); + clearCache(); + prepareCurrentResourceViolations(context); saveNewViolations(context); saveNewViolationsByPriority(context); saveNewViolationsByRule(context); } - private void resetCache() { + private void clearCache() { ruleToLevel.clear(); + violationsByPriority.clear(); + violationsByRule.clear(); + } + + private void prepareCurrentResourceViolations(DecoratorContext context) { + for (Violation violation : context.getViolations()) { + violationsByPriority.put(violation.getPriority(), violation); + violationsByRule.put(violation.getRule(), violation); + ruleToLevel.put(violation.getRule(), violation.getPriority()); + } } private void saveNewViolations(DecoratorContext context) { @@ -73,8 +86,8 @@ public class NewViolationsDecorator implements Decorator { for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { int variationIndex = variationSnapshot.getIndex(); Collection children = context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS); - int value = countViolations(context.getViolations(), variationSnapshot.getDate()); - double sum = sumChildren(variationIndex, children) + value; + int count = countViolations(context.getViolations(), variationSnapshot.getDate()); + double sum = sumChildren(variationIndex, children) + count; measure.setVariation(variationIndex, sum); } context.saveMeasure(measure); @@ -87,7 +100,7 @@ public class NewViolationsDecorator implements Decorator { for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { int variationIndex = variationSnapshot.getIndex(); Collection children = context.getChildrenMeasures(MeasuresFilters.rulePriority(CoreMetrics.NEW_VIOLATIONS, priority)); - int count = countViolations(context.getViolations(), variationSnapshot.getDate(), priority); + int count = countViolations(violationsByPriority.get(priority), variationSnapshot.getDate()); double sum = sumChildren(variationIndex, children) + count; measure1.setVariation(variationIndex, sum); measure2.setVariation(variationIndex, sum); @@ -114,38 +127,32 @@ public class NewViolationsDecorator implements Decorator { measure.setRulePriority(ruleToLevel.get(rule)); for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { int variationIndex = variationSnapshot.getIndex(); - int value = countViolations(context.getViolations(), variationSnapshot.getDate(), rule); - double sum = sumChildren(variationIndex, childrenByRule.get(rule)) + value; + int count = countViolations(violationsByRule.get(rule), variationSnapshot.getDate()); + double sum = sumChildren(variationIndex, childrenByRule.get(rule)) + count; measure.setVariation(variationIndex, sum); } context.saveMeasure(measure); } } - int countViolations(Collection violations, Date targetDate) { - int count = 0; - for (Violation violation : violations) { - if (isAfter(violation, targetDate)) { - count++; + int sumChildren(int variationIndex, Collection measures) { + int sum = 0; + for (Measure measure : measures) { + Double var = measure.getVariation(variationIndex); + if (var != null) { + sum += var.intValue(); } } - return count; + return sum; } - int countViolations(List violations, Date targetDate, RulePriority priority) { - int count = 0; - for (Violation violation : violations) { - if (isAfter(violation, targetDate) && ObjectUtils.equals(violation.getPriority(), priority)) { - count++; - } + int countViolations(Collection violations, Date targetDate) { + if (violations == null) { + return 0; } - return count; - } - - int countViolations(List violations, Date targetDate, Rule rule) { int count = 0; for (Violation violation : violations) { - if (isAfter(violation, targetDate) && ObjectUtils.equals(violation.getRule(), rule)) { + if (isAfter(violation, targetDate)) { count++; } } @@ -156,17 +163,6 @@ public class NewViolationsDecorator implements Decorator { return !violation.getCreatedAt().before(date); } - int sumChildren(int variationIndex, Collection measures) { - int sum = 0; - for (Measure measure : measures) { - Double var = measure.getVariation(variationIndex); - if (var != null) { - sum += var.intValue(); - } - } - return sum; - } - private Metric getMetricForPriority(RulePriority priority) { Metric metric = null; if (priority.equals(RulePriority.BLOCKER)) { -- 2.39.5