Browse Source

Improve microbenchmark templates

tags/5.1-RC1
Simon Brandhof 9 years ago
parent
commit
a508911239

+ 5
- 0
microbenchmark-template/pom.xml View File

@@ -26,6 +26,11 @@
<artifactId>sonar-plugin-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>

+ 1
- 1
microbenchmark-template/run.sh View File

@@ -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 $*

+ 151
- 0
microbenchmark-template/src/main/java/org/sonar/microbenchmark/FileSourceDbBenchmark.java View File

@@ -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<FileSourceDb.Line> 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();
}
}

+ 12
- 3
microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java View File

@@ -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();
}

+ 7
- 0
pom.xml View File

@@ -1502,6 +1502,13 @@
</dependency>
</dependencies>
</profile>
<profile>
<!-- add microbenchmarks module to IDE -->
<id>includeMicrobenchmarkModule</id>
<modules>
<module>microbenchmark-template</module>
</modules>
</profile>
</profiles>

</project>

Loading…
Cancel
Save