From: Simon Brandhof Date: Thu, 12 Feb 2015 14:14:04 +0000 (+0100) Subject: Improve microbenchmark templates X-Git-Tag: 5.1-RC1~298 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a508911239639061a054d45b47f6a7b075927b2e;p=sonarqube.git Improve microbenchmark templates --- diff --git a/microbenchmark-template/pom.xml b/microbenchmark-template/pom.xml index 3d8695ba8ca..7a07993a8b8 100644 --- a/microbenchmark-template/pom.xml +++ b/microbenchmark-template/pom.xml @@ -26,6 +26,11 @@ sonar-plugin-api ${project.version} + + ${project.groupId} + sonar-core + ${project.version} + com.google.code.gson gson diff --git a/microbenchmark-template/run.sh b/microbenchmark-template/run.sh index db9c373374d..ab222188dee 100755 --- a/microbenchmark-template/run.sh +++ b/microbenchmark-template/run.sh @@ -5,4 +5,4 @@ # Example: run.sh org.sonar.microbenchmark.SerializationBenchmark mvn clean install -java -jar target/microbenchmark.jar -i 5 -wi 5 -f 5 $* +java -jar target/microbenchmark.jar $* diff --git a/microbenchmark-template/src/main/java/org/sonar/microbenchmark/FileSourceDbBenchmark.java b/microbenchmark-template/src/main/java/org/sonar/microbenchmark/FileSourceDbBenchmark.java new file mode 100644 index 00000000000..99bd0c1026f --- /dev/null +++ b/microbenchmark-template/src/main/java/org/sonar/microbenchmark/FileSourceDbBenchmark.java @@ -0,0 +1,151 @@ +/* + * 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.microbenchmark; + +import com.google.protobuf.CodedOutputStream; +import org.apache.commons.lang.RandomStringUtils; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.sonar.server.source.db.FileSourceDb; + +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * + * See https://code.google.com/p/xxhash/ and https://github.com/jpountz/lz4-java + * + */ +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +@Fork(1) +@Warmup(iterations = 5) +@Measurement(iterations = 5) +@BenchmarkMode(Mode.Throughput) +public class FileSourceDbBenchmark { + + @Param({"10", "100", "1000", "100000"}) + public int linesNumber; + + List lines = new ArrayList<>(); + FileSourceDb.Data data; + + @Setup + public void setup() throws Exception { + FileSourceDb.Data.Builder builder = FileSourceDb.Data.newBuilder(); + for (int i = 0; i < linesNumber; i++) { + FileSourceDb.Line.Builder lineBuilder = builder.addLinesBuilder(); + lines.add(lineBuilder + .setLine(i + 1) + .setScmAuthor("charlie") + .setScmRevision("ABCDE") + .setScmDate(15000000000L) + .setUtLineHits(5) + .setUtConditions(2) + .setUtCoveredConditions(1) + .setSource(RandomStringUtils.randomAlphanumeric(10)) + .setHighlighting(RandomStringUtils.randomAlphanumeric(20)) + .setSymbols(RandomStringUtils.randomAlphanumeric(20)) + .addAllDuplications(Arrays.asList(12,13,15)) + .build()); + } + data = builder.build(); + } + + @Benchmark + public int container() throws Exception { + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + data.writeTo(byteOutput); + byteOutput.close(); + return byteOutput.toByteArray().length; + } + + @Benchmark + public int delimiters() throws Exception { + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + for (FileSourceDb.Line line : lines) { + line.writeDelimitedTo(byteOutput); + } + byteOutput.close(); + return byteOutput.toByteArray().length; + } + + @Benchmark + public int codedstream_container() throws Exception { + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + CodedOutputStream writer = CodedOutputStream.newInstance(byteOutput); + writer.writeRawVarint32(data.getSerializedSize()); + writer.writeRawBytes(data.toByteArray()); + writer.flush(); + byteOutput.close(); + return byteOutput.toByteArray().length; + } + + @Benchmark + public int codedstream_container_known_size() throws Exception { + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(data.getSerializedSize()); + CodedOutputStream writer = CodedOutputStream.newInstance(byteOutput); + writer.writeRawVarint32(data.getSerializedSize()); + writer.writeRawBytes(data.toByteArray()); + writer.flush(); + byteOutput.close(); + return byteOutput.toByteArray().length; + } + + @Benchmark + public int codedstream_delimiters() throws Exception { + ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); + CodedOutputStream writer = CodedOutputStream.newInstance(byteOutput); + for (FileSourceDb.Line line : lines) { + writer.writeRawVarint32(line.getSerializedSize()); + writer.writeRawBytes(line.toByteArray()); + } + writer.flush(); + byteOutput.close(); + return byteOutput.toByteArray().length; + } + + /** + * You can this benchmark with maven command-line (see run.sh) or by executing this method + * in IDE + */ + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(FileSourceDbBenchmark.class.getSimpleName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java b/microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java index 8c599a82c87..7286eabff3b 100644 --- a/microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java +++ b/microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java @@ -25,9 +25,15 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.stream.JsonWriter; import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; @@ -49,8 +55,14 @@ import java.io.OutputStream; import java.io.Serializable; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; +@OutputTimeUnit(TimeUnit.MILLISECONDS) @State(Scope.Thread) +@Fork(1) +@Warmup(iterations = 5) +@Measurement(iterations = 5) +@BenchmarkMode(Mode.Throughput) public class SerializationBenchmark { File outputFile; @@ -186,9 +198,6 @@ public class SerializationBenchmark { public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(SerializationBenchmark.class.getSimpleName()) - .warmupIterations(5) - .measurementIterations(5) - .forks(5) .build(); new Runner(opt).run(); } diff --git a/pom.xml b/pom.xml index 57b1e8ba278..ee6004292c0 100644 --- a/pom.xml +++ b/pom.xml @@ -1502,6 +1502,13 @@ + + + includeMicrobenchmarkModule + + microbenchmark-template + +