From b7e2585251c5e5116dadf4167f1f3d527818a5b5 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 27 Jan 2014 14:29:56 +0100 Subject: [PATCH] Replace old profiler call w/ new profiling watch --- .../server/qualityprofile/ESActiveRule.java | 50 +++++++++++-------- .../qualityprofile/ESActiveRuleTest.java | 2 +- .../QProfileRuleLookupTest.java | 2 +- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ESActiveRule.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ESActiveRule.java index f57aafbd232..3ee7b23f0df 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/ESActiveRule.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/ESActiveRule.java @@ -19,31 +19,34 @@ */ package org.sonar.server.qualityprofile; -import org.elasticsearch.common.io.BytesStream; -import org.elasticsearch.index.query.FilterBuilders; -import org.elasticsearch.index.query.QueryBuilders; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.sonar.server.rule.RuleDocument; import com.google.common.base.Function; -import com.google.common.collect.Iterables; -import org.sonar.server.es.SearchQuery; -import org.sonar.server.es.ESIndex; -import org.sonar.server.rule.RuleRegistry; import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import org.apache.ibatis.session.SqlSession; +import org.elasticsearch.common.io.BytesStream; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.index.query.FilterBuilders; +import org.elasticsearch.index.query.QueryBuilders; import org.sonar.api.utils.TimeProfiler; +import org.sonar.core.persistence.MyBatis; +import org.sonar.core.profiling.Profiling; +import org.sonar.core.profiling.Profiling.Level; +import org.sonar.core.profiling.StopWatch; +import org.sonar.core.qualityprofile.db.ActiveRuleDao; import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleParamDto; +import org.sonar.server.es.ESIndex; +import org.sonar.server.es.SearchQuery; +import org.sonar.server.rule.RuleDocument; +import org.sonar.server.rule.RuleRegistry; import java.io.IOException; import java.util.Collection; import java.util.List; import static com.google.common.collect.Lists.newArrayList; -import org.sonar.core.persistence.MyBatis; -import org.sonar.core.qualityprofile.db.ActiveRuleDao; public class ESActiveRule { @@ -51,11 +54,13 @@ public class ESActiveRule { private final ESIndex esIndex; private final ActiveRuleDao activeRuleDao; private final MyBatis myBatis; + private final Profiling profiling; - public ESActiveRule(ESIndex esIndex, ActiveRuleDao activeRuleDao, MyBatis myBatis) { + public ESActiveRule(ESIndex esIndex, ActiveRuleDao activeRuleDao, MyBatis myBatis, Profiling profiling) { this.esIndex = esIndex; this.activeRuleDao = activeRuleDao; this.myBatis = myBatis; + this.profiling = profiling; } public void start() { @@ -65,12 +70,11 @@ public class ESActiveRule { public void bulkRegisterActiveRules() { SqlSession session = myBatis.openSession(); try { - TimeProfiler profiler = new TimeProfiler(); - profiler.start("Rebuilding active rules index - query"); + StopWatch bulkWatch = startWatch(); List activeRules = activeRuleDao.selectAll(session); List activeRuleParams = activeRuleDao.selectAllParams(session); - profiler.stop(); + bulkWatch.stop(String.format("Loaded %d active rules from DB", activeRules.size())); Multimap paramsByActiveRule = ArrayListMultimap.create(); for (ActiveRuleParamDto param : activeRuleParams) { @@ -109,6 +113,7 @@ public class ESActiveRule { } public String[] bulkIndexActiveRules(List activeRules, Multimap paramsByActiveRule) { + StopWatch bulkWatch = startWatch(); try { int size = activeRules.size(); String[] ids = new String[size]; @@ -116,24 +121,22 @@ public class ESActiveRule { String[] parentIds = new String[size]; int index = 0; - TimeProfiler profiler = new TimeProfiler(); - profiler.start("Build active rules documents"); for (ActiveRuleDto activeRule : activeRules) { ids[index] = activeRule.getId().toString(); docs[index] = activeRuleDocument(activeRule, paramsByActiveRule.get(activeRule.getId())); parentIds[index] = activeRule.getRulId().toString(); index++; } - profiler.stop(); if (!activeRules.isEmpty()) { - profiler.start("Index active rules"); esIndex.bulkIndex(RuleRegistry.INDEX_RULES, ESActiveRule.TYPE_ACTIVE_RULE, ids, docs, parentIds); - profiler.stop(); } + bulkWatch.stop(String.format("Indexed %d active rules", size)); return ids; } catch (IOException e) { + bulkWatch.stop("Failed to indes active rules"); throw new IllegalStateException("Unable to index active rules", e); + } finally { } } @@ -153,7 +156,7 @@ public class ESActiveRule { bulkDeleteActiveRules(indexIds); } - protected void bulkDeleteActiveRules(List indexIds) { + private void bulkDeleteActiveRules(List indexIds) { if (!indexIds.isEmpty()) { esIndex.bulkDelete(RuleRegistry.INDEX_RULES, ESActiveRule.TYPE_ACTIVE_RULE, indexIds.toArray(new String[0])); } @@ -212,4 +215,7 @@ public class ESActiveRule { return document; } + private StopWatch startWatch() { + return profiling.start("qprofile", Level.BASIC); + } } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java index 55b6b5fd780..af285737fd5 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java @@ -93,7 +93,7 @@ public class ESActiveRuleTest { RuleRegistry esRule = new RuleRegistry(searchIndex, null); esRule.start(); - esActiveRule = new ESActiveRule(searchIndex, activeRuleDao, myBatis); + esActiveRule = new ESActiveRule(searchIndex, activeRuleDao, myBatis, profiling); esActiveRule.start(); esSetup.execute( diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java index e6fb0c75ff8..7cfd3f71f8d 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java @@ -62,7 +62,7 @@ public class QProfileRuleLookupTest { index.start(); RuleRegistry registry = new RuleRegistry(index, null); registry.start(); - ESActiveRule esActiveRule = new ESActiveRule(index, null, null); + ESActiveRule esActiveRule = new ESActiveRule(index, null, null, null); esActiveRule.start(); profileRules = new QProfileRuleLookup(index); -- 2.39.5