]> source.dussan.org Git - sonarqube.git/commitdiff
Replace old profiler call w/ new profiling watch
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 27 Jan 2014 13:29:56 +0000 (14:29 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 27 Jan 2014 13:30:03 +0000 (14:30 +0100)
sonar-server/src/main/java/org/sonar/server/qualityprofile/ESActiveRule.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/ESActiveRuleTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileRuleLookupTest.java

index f57aafbd23271e091594a1d6313bce078d63b9ce..3ee7b23f0dfac7809a9b70bd3611ff155bea8c76 100644 (file)
  */
 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<ActiveRuleDto> activeRules = activeRuleDao.selectAll(session);
       List<ActiveRuleParamDto> activeRuleParams = activeRuleDao.selectAllParams(session);
-      profiler.stop();
+      bulkWatch.stop(String.format("Loaded %d active rules from DB", activeRules.size()));
 
       Multimap<Integer, ActiveRuleParamDto> paramsByActiveRule = ArrayListMultimap.create();
       for (ActiveRuleParamDto param : activeRuleParams) {
@@ -109,6 +113,7 @@ public class ESActiveRule {
   }
 
   public String[] bulkIndexActiveRules(List<ActiveRuleDto> activeRules, Multimap<Integer, ActiveRuleParamDto> 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<String> indexIds) {
+  private void bulkDeleteActiveRules(List<String> 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);
+  }
 }
index 55b6b5fd780215679974c60d9c5238829c1e2739..af285737fd5fb657cee848b8dd7dc70478bb819b 100644 (file)
@@ -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(
index e6fb0c75ff84a18644ca98577de419b1ef68001c..7cfd3f71f8db28fa8c89a6a5a3398d199849f36d 100644 (file)
@@ -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);