jmh

JMH: Using the same static object in all Benchmark tests

我的未来我决定 提交于 2019-12-01 00:06:50
问题 I have a class that constructs some complicated data (imagine a large XML or JSON structure - that sort of thing). Constructing it takes time. So I want to construct it once, and then use that same data in all tests. Currently I basically have a public static object instance defined in a class that defines main , and then refer to it explicitly in the tests (code is a very simplified example): public class Data { // This class constructs some complicated data } public class TestSet { public

newInstance vs new in jdk-9/jdk-8 and jmh

天大地大妈咪最大 提交于 2019-11-29 22:11:08
I've seen a lot of threads here that compare and try to answer which is faster: newInstance or new operator . Looking at the source code, it would seem that newInstance should be much slower , I mean it does so many security checks and uses reflection. And I've decided to measure, first running jdk-8. Here is the code using jmh . @BenchmarkMode(value = { Mode.AverageTime, Mode.SingleShotTime }) @Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) @State(Scope.Benchmark) public class TestNewObject { public static void

Why is the StringBuilder chaining pattern sb.append(x).append(y) faster than regular sb.append(x); sb.append(y)?

帅比萌擦擦* 提交于 2019-11-28 16:30:47
I have a microbenchmark that shows very strange results: @BenchmarkMode(Mode.Throughput) @Fork(1) @State(Scope.Thread) @Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1000) @Measurement(iterations = 40, time = 1, timeUnit = TimeUnit.SECONDS, batchSize = 1000) public class Chaining { private String a1 = "111111111111111111111111"; private String a2 = "222222222222222222222222"; private String a3 = "333333333333333333333333"; @Benchmark public String typicalChaining() { return new StringBuilder().append(a1).append(a2).append(a3).toString(); } @Benchmark public String

How to run JMH from inside JUnit tests?

让人想犯罪 __ 提交于 2019-11-28 15:44:22
How can I run JMH benchmarks inside my existing project using JUnit tests? The official documentation recommends making a separate project, using Maven shade plugin, and launching JMH inside the main method. Is this necessary and why is it recommended? I've been running JMH inside my existing Maven project using JUnit with no apparent ill effects. I cannot answer why the authors recommend doing things differently. I have not observed a difference in results. JMH launches a separate JVM to run benchmarks to isolate them. Here is what I do: Add the JMH dependencies to your POM: <dependency>

Why is StringBuilder#append(int) faster in Java 7 than in Java 8?

血红的双手。 提交于 2019-11-28 15:24:50
While investigating for a little debate w.r.t. using "" + n and Integer.toString(int) to convert an integer primitive to a string I wrote this JMH microbenchmark: @Fork(1) @OutputTimeUnit(TimeUnit.MILLISECONDS) @State(Scope.Benchmark) public class IntStr { protected int counter; @GenerateMicroBenchmark public String integerToString() { return Integer.toString(this.counter++); } @GenerateMicroBenchmark public String stringBuilder0() { return new StringBuilder().append(this.counter++).toString(); } @GenerateMicroBenchmark public String stringBuilder1() { return new StringBuilder().append("")

“No matching benchmarks” when running JMH from main in eclipse

会有一股神秘感。 提交于 2019-11-28 11:07:46
I wanted to try out the new feature of JMH by running it as Java Application in eclipse. I imported and built jmh-samples project. Compiled classes ended in /jmh-samples/target/generated-sources/annotations, there are several JARs in /target/ and running microbenchmarks.jar from command line works as usual. However when I execute main I always get No matching benchmarks. Miss-spelled regexp? Any ideas? I am using version 0.3 jmh-dev@ is a better way to communicate this with the developers. Few things to try: Hijacking Main is probably not a good idea. Use Java API instead, like this sample .

Strange JIT pessimization of a loop idiom

你说的曾经没有我的故事 提交于 2019-11-28 09:03:58
While analyzing the results of a recent question here , I encountered a quite peculiar phenomenon: apparently an extra layer of HotSpot's JIT-optimization actually slows down execution on my machine. Here is the code I have used for the measurement: @OutputTimeUnit(TimeUnit.NANOSECONDS) @BenchmarkMode(Mode.AverageTime) @OperationsPerInvocation(Measure.ARRAY_SIZE) @Warmup(iterations = 2, time = 1) @Measurement(iterations = 5, time = 1) @State(Scope.Thread) @Threads(1) @Fork(2) public class Measure { public static final int ARRAY_SIZE = 1024; private final int[] array = new int[ARRAY_SIZE];

Why is StringBuilder#append(int) faster in Java 7 than in Java 8?

大城市里の小女人 提交于 2019-11-27 19:48:41
问题 While investigating for a little debate w.r.t. using "" + n and Integer.toString(int) to convert an integer primitive to a string I wrote this JMH microbenchmark: @Fork(1) @OutputTimeUnit(TimeUnit.MILLISECONDS) @State(Scope.Benchmark) public class IntStr { protected int counter; @GenerateMicroBenchmark public String integerToString() { return Integer.toString(this.counter++); } @GenerateMicroBenchmark public String stringBuilder0() { return new StringBuilder().append(this.counter++).toString(

Erratic performance of Arrays.stream().map().sum()

落爺英雄遲暮 提交于 2019-11-27 12:18:30
I have chanced upon an instance of exceedingly erratic performance profile of a very simple map/reduce operation on primitive arrays. Here is my jmh benchmark code: @OutputTimeUnit(TimeUnit.NANOSECONDS) @BenchmarkMode(Mode.AverageTime) @OperationsPerInvocation(Measure.ARRAY_SIZE) @Warmup(iterations = 300, time = 200, timeUnit=MILLISECONDS) @Measurement(iterations = 1, time = 1000, timeUnit=MILLISECONDS) @State(Scope.Thread) @Threads(1) @Fork(1) public class Measure { static final int ARRAY_SIZE = 1<<20; final int[] ds = new int[ARRAY_SIZE]; private IntUnaryOperator mapper; @Setup public void

Empty methods noticeably slower in Java 11 than Java 8

余生颓废 提交于 2019-11-27 11:04:05
问题 I was comparing the performance of JDK 8 and 11 using jmh 1.21 when I ran across some surprising numbers: Java version: 1.8.0_192, vendor: Oracle Corporation Benchmark Mode Cnt Score Error Units MyBenchmark.emptyMethod avgt 25 0.362 ± 0.001 ns/op Java version: 9.0.4, vendor: Oracle Corporation Benchmark Mode Cnt Score Error Units MyBenchmark.emptyMethod avgt 25 0.362 ± 0.001 ns/op Java version: 10.0.2, vendor: Oracle Corporation Benchmark Mode Cnt Score Error Units MyBenchmark.emptyMethod