diff options
Diffstat (limited to 'microbenchmark-template')
4 files changed, 50 insertions, 13 deletions
diff --git a/microbenchmark-template/pom.xml b/microbenchmark-template/pom.xml index 32659e1d664..3d8695ba8ca 100644 --- a/microbenchmark-template/pom.xml +++ b/microbenchmark-template/pom.xml @@ -52,7 +52,7 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <jmh.version>1.1.1</jmh.version> + <jmh.version>1.5.1</jmh.version> <uberjar.name>microbenchmark</uberjar.name> </properties> diff --git a/microbenchmark-template/run.sh b/microbenchmark-template/run.sh index 10d0c60f6b3..db9c373374d 100755 --- a/microbenchmark-template/run.sh +++ b/microbenchmark-template/run.sh @@ -1,4 +1,8 @@ #!/bin/sh +# The command-line arguments can be used to list the benchmarks to be executed. +# By default all benchmarks are executed. +# 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 -i 5 -wi 5 -f 5 $* 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 6957fa1f403..9d12e64998d 100644 --- a/microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java +++ b/microbenchmark-template/src/main/java/org/sonar/microbenchmark/SerializationBenchmark.java @@ -28,6 +28,10 @@ import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; +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.batch.protocol.Constants; import org.sonar.batch.protocol.output.BatchOutput; @@ -43,6 +47,8 @@ import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; +import java.util.Arrays; +import java.util.List; @State(Scope.Thread) public class SerializationBenchmark { @@ -66,6 +72,7 @@ public class SerializationBenchmark { issue.message = "this is the message of issue " + i; issue.line = i; issue.author = "someone"; + issue.tags = Arrays.asList("tag" + i, "othertag" + i); gson.toJson(issue, Issue.class, writer); } writer.endArray(); @@ -84,6 +91,7 @@ public class SerializationBenchmark { issueBuilder.setMsg("this is the message of issue " + i); issueBuilder.setLine(i); issueBuilder.setAuthorLogin("someone"); + issueBuilder.addAllTags(Arrays.asList("tag" + i, "othertag" + i)); issueBuilder.build().writeDelimitedTo(out); } } @@ -99,6 +107,7 @@ public class SerializationBenchmark { issue.message = "this is the message of issue " + i; issue.line = i; issue.author = "someone"; + issue.tags = Arrays.asList("tag" + i, "othertag" + i); out.writeObject(issue); } } @@ -114,6 +123,7 @@ public class SerializationBenchmark { issue.message = "this is the message of issue " + i; issue.line = i; issue.author = "someone"; + issue.tags = Arrays.asList("tag" + i, "othertag" + i); out.writeObject(issue); } } @@ -131,6 +141,7 @@ public class SerializationBenchmark { issue.message = "this is the message of issue " + i; issue.line = i; issue.author = "someone"; + issue.tags = Arrays.asList("tag" + i, "othertag" + i); kryo.writeObject(output, issue); } output.close(); @@ -139,19 +150,27 @@ public class SerializationBenchmark { public static class Issue implements Serializable { String uuid, severity, message, author; int line; + List<String> tags; } public static class ExternalizableIssue implements Externalizable { String uuid, severity, message, author; int line; + List<String> tags; @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeBytes(uuid); - out.writeBytes(severity); - out.writeBytes(message); - out.writeBytes(author); + out.writeUTF(uuid); + out.writeUTF(severity); + out.writeUTF(message); + out.writeUTF(author); out.writeInt(line); + + // write number of tags then tags + out.writeByte(tags.size()); + for (String tag : tags) { + out.writeUTF(tag); + } } @Override @@ -159,4 +178,18 @@ public class SerializationBenchmark { throw new UnsupportedOperationException(); } } + + /** + * 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(SerializationBenchmark.class.getSimpleName()) + .warmupIterations(5) + .measurementIterations(5) + .forks(5) + .build(); + new Runner(opt).run(); + } } diff --git a/microbenchmark-template/src/test/java/org/sonar/microbenchmark/SerializationBenchmarkTest.java b/microbenchmark-template/src/test/java/org/sonar/microbenchmark/SerializationBenchmarkTest.java index 11fd98223eb..05be832fd8c 100644 --- a/microbenchmark-template/src/test/java/org/sonar/microbenchmark/SerializationBenchmarkTest.java +++ b/microbenchmark-template/src/test/java/org/sonar/microbenchmark/SerializationBenchmarkTest.java @@ -62,14 +62,14 @@ public class SerializationBenchmarkTest { Deflater deflater = new Deflater(); byte[] content = FileUtils.readFileToByteArray(input); deflater.setInput(content); - OutputStream outputStream = new FileOutputStream(zipFile); - deflater.finish(); - byte[] buffer = new byte[1024]; - while (!deflater.finished()) { - int count = deflater.deflate(buffer); // returns the generated code... index - outputStream.write(buffer, 0, count); + try (OutputStream outputStream = new FileOutputStream(zipFile)) { + deflater.finish(); + byte[] buffer = new byte[1024]; + while (!deflater.finished()) { + int count = deflater.deflate(buffer); // returns the generated code... index + outputStream.write(buffer, 0, count); + } } - outputStream.close(); deflater.end(); return zipFile; |