Sub topic 3
What JMH does for you
This chapter assumes you know basic Java and annotations, and that the JVM compiles hot code while the program runs. The previous chapters showed how hand-written timing loops go wrong. Here you will see which of those problems JMH handles and which it leaves to you.
What JMH is
The Java Microbenchmark Harness (JMH) is a benchmarking library maintained by the OpenJDK project. You write small methods and annotate them. JMH generates the code that calls them, controls the JVM they run in, and computes the statistics.
You supply the code under test. JMH supplies the measurement procedure.
Problems JMH controls
| Problem with a hand-written loop | What JMH does |
|---|---|
| Early iterations run in the interpreter, before the JIT compiler has optimized the code | It runs warmup iterations first and discards their results |
| Profile data left by earlier code in the same JVM skews the compiled code | It runs each benchmark in a freshly forked JVM |
| One run says little about run-to-run variation | It repeats measurement iterations and forks, then reports a mean and an error |
| The JIT removes work whose result is never used | It consumes the value your method returns, so the work stays live |
| Timer calls and loop overhead pollute the measurement | It generates the timing loop itself and keeps it outside your method |
| Setup cost leaks into the timing | It runs `@Setup` methods outside the timed region |
What a run looks like
For each benchmark and each parameter combination, JMH repeats the same cycle.
The warmup count, measurement count and fork count all have defaults. You can change them with annotations such as @Warmup, @Measurement and @Fork, or with command-line flags. A later chapter covers them in detail.
What JMH does not do for you
JMH removes many sources of error, but it cannot judge whether your benchmark measures something meaningful.
- Constant folding. If your benchmark computes something from a
static finalvalue or a literal, the JIT may calculate the result at compile time. JMH cannot detect this. You avoid it by reading inputs from fields of a@Stateobject. - Unrealistic inputs. A benchmark over a tiny sorted array can produce a number that does not predict how the code behaves on production data.
- Environmental noise. Other processes, CPU frequency scaling and power-saving modes still affect results. Run benchmarks on a quiet machine.
- Interpretation. JMH reports numbers. Deciding whether a difference matters is your job.
What you write
A benchmark class stays small. This one is complete, and the next chapters explain each piece.
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@State(Scope.Thread)
public class FirstBenchmark {
private double x = Math.PI;
@Benchmark
public double logOfPi() {
return Math.log(x);
}
}The method contains no timer calls and no loop. JMH generates the loop, fires the calls and consumes the returned double. The input x is a field, not a literal.
Exercise
A colleague times a method with System.nanoTime in a loop of 1,000 calls inside the same JVM that has already run other tests. Name two of the problems from the table that JMH would address, and one problem it would not address.
Show answer
JMH addresses missing warmup (it runs and discards warmup iterations) and a shared, already-profiled JVM (it forks a fresh JVM per benchmark). It would not address constant folding or unrealistic inputs. If the colleague's method works on a literal value, you must move that input into a @State field yourself.