JMH usage examples
Let’s now run a few tests and compare them. First, we run the following test method:
@Benchmark
@BenchmarkMode(Mode.All)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public void testTheMethod0() {
int res = 0;
for(int i = 0; i < 1000; i++){
int n = i * i;
if (n != 0 && n % 250_000 == 0) {
res += n;
}
}
}
As you can see, we have requested to measure all the performance characteristics and to use nanoseconds while presenting the results. On our system, the test execution took around 20 minutes and the final result summary looked like this:
Let’s now change the test as follows:
@Benchmark...