From 20ae7f5d735a0139de612fb46a9e5478e8d264c2 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 26 Aug 2014 15:32:06 +0200 Subject: [PATCH] SONAR-5528 Improve performance of persistit serialization for highlighting --- .../batch/duplication/DuplicationCache.java | 1 - .../SyntaxHighlightingDataValueCoder.java | 50 +++++++++++++++++++ .../SyntaxHighlightingRuleValueCoder.java | 43 ++++++++++++++++ .../sonar/batch/index/ComponentDataCache.java | 3 ++ .../highlighting/HighlightingMediumTest.java | 4 +- 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataValueCoder.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationCache.java b/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationCache.java index 573ac7a7a13..9652308ef82 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/duplication/DuplicationCache.java @@ -38,7 +38,6 @@ public class DuplicationCache implements BatchComponent { public DuplicationCache(Caches caches) { caches.registerValueCoder(DuplicationGroup.class, new DuplicationGroupValueCoder()); - caches.registerValueCoder(DuplicationGroup.Block.class, new DuplicationBlockValueCoder()); cache = caches.createCache("duplications"); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataValueCoder.java new file mode 100644 index 00000000000..b3be41fa2f6 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingDataValueCoder.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.highlighting; + +import com.persistit.Value; +import com.persistit.encoding.CoderContext; +import com.persistit.encoding.ValueCoder; + +import java.util.ArrayList; + +public class SyntaxHighlightingDataValueCoder implements ValueCoder { + + private SyntaxHighlightingRuleValueCoder rulesCoder = new SyntaxHighlightingRuleValueCoder(); + + @Override + public void put(Value value, Object object, CoderContext context) { + SyntaxHighlightingData data = (SyntaxHighlightingData) object; + value.put(data.syntaxHighlightingRuleSet().size()); + for (SyntaxHighlightingRule rule : data.syntaxHighlightingRuleSet()) { + rulesCoder.put(value, rule, context); + } + } + + @Override + public Object get(Value value, Class clazz, CoderContext context) { + int count = value.getInt(); + ArrayList rules = new ArrayList(count); + for (int i = 0; i < count; i++) { + rules.add((SyntaxHighlightingRule) rulesCoder.get(value, SyntaxHighlightingRule.class, context)); + } + return new SyntaxHighlightingData(rules); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java new file mode 100644 index 00000000000..e56ee8f0d13 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/highlighting/SyntaxHighlightingRuleValueCoder.java @@ -0,0 +1,43 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.highlighting; + +import com.persistit.Value; +import com.persistit.encoding.CoderContext; +import com.persistit.encoding.ValueCoder; + +class SyntaxHighlightingRuleValueCoder implements ValueCoder { + + @Override + public void put(Value value, Object object, CoderContext context) { + SyntaxHighlightingRule rule = (SyntaxHighlightingRule) object; + value.put(rule.getStartPosition()); + value.put(rule.getEndPosition()); + value.put(rule.getTextType()); + } + + @Override + public Object get(Value value, Class clazz, CoderContext context) { + int startPosition = value.getInt(); + int endPosition = value.getInt(); + String type = value.getString(); + return SyntaxHighlightingRule.create(startPosition, endPosition, type); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java index 0523212bf75..b94aa0973b1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java @@ -20,6 +20,8 @@ package org.sonar.batch.index; import org.sonar.api.BatchComponent; +import org.sonar.batch.highlighting.SyntaxHighlightingData; +import org.sonar.batch.highlighting.SyntaxHighlightingDataValueCoder; import javax.annotation.CheckForNull; @@ -27,6 +29,7 @@ public class ComponentDataCache implements BatchComponent { private final Cache cache; public ComponentDataCache(Caches caches) { + caches.registerValueCoder(SyntaxHighlightingData.class, new SyntaxHighlightingDataValueCoder()); cache = caches.createCache("componentData"); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java index 0fa6c76365f..7e2c71db869 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/highlighting/HighlightingMediumTest.java @@ -104,13 +104,14 @@ public class HighlightingMediumTest { File xooFile = new File(srcDir, "sample.xoo"); File xoohighlightingFile = new File(srcDir, "sample.xoo.highlighting"); FileUtils.write(xooFile, "Sample xoo\ncontent"); - int chunkSize = 10000; + int chunkSize = 100000; StringBuilder sb = new StringBuilder(16 * chunkSize); for (int i = 0; i < chunkSize; i++) { sb.append(i).append(":").append(i + 1).append(":s\n"); } FileUtils.write(xoohighlightingFile, sb.toString()); + long start = System.currentTimeMillis(); TaskResult result = tester.newTask() .properties(ImmutableMap.builder() .put("sonar.task", "scan") @@ -122,6 +123,7 @@ public class HighlightingMediumTest { .put("sonar.sources", "src") .build()) .start(); + System.out.println("Duration: " + (System.currentTimeMillis() - start)); InputFile file = result.inputFiles().get(0); assertThat(result.highlightingTypeFor(file, 0)).isEqualTo(HighlightingBuilder.TypeOfText.STRING); -- 2.39.5