Sub topic 12
Warmup, measurement and forks
This chapter assumes you can build a JMH project (see the setup chapter), write a @Benchmark method, and that you know the JVM compiles hot code at runtime. Here you will learn how JMH splits a run into warmup and measurement, and why it starts fresh JVMs called forks.
Iterations: warmup and measurement
JMH runs each benchmark in iterations. An iteration is a fixed time window in which JMH calls your method as many times as it can.
- Warmup iterations run first. The JVM interprets, profiles and compiles your code during them. JMH throws these results away.
- Measurement iterations run after warmup. JMH records one score per iteration and reports the average.
The goal of warmup is to reach steady state, where the JIT has finished compiling the hot code. Measuring before that point mixes interpreter speed with compiled speed.
Forks
A fork is a separate JVM process that JMH starts to run the benchmark. Each fork does its own warmup and measurement. JMH then combines the scores from all forks.
Forks matter because a JVM carries history. The JIT profiles the code it sees, and a profile built by one benchmark can change how another is compiled. Running every benchmark in a fresh JVM keeps them independent. Several forks also reveal run-to-run variation, since each JVM may settle on slightly different compiled code.
Configuring with annotations
Set these values on the benchmark class or method.
package org.sample;
import java.util.concurrent.TimeUnit;
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.State;
import org.openjdk.jmh.annotations.Warmup;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 5, time = 1)
@Fork(2)
public class ForkBenchmark {
private double x = Math.PI;
@Benchmark
public double log() {
return Math.log(x);
}
}With these settings, JMH starts 2 JVMs. Each runs 3 one-second warmup iterations and 5 one-second measurement iterations. That gives 10 recorded scores in total.
Overriding from the command line
Command-line flags take precedence over annotations, so you can change a run without rebuilding.
java -jar target/benchmarks.jar ForkBenchmark -wi 3 -i 5 -f 1Here -wi sets warmup iterations, -i sets measurement iterations, and -f sets forks.
Common errors
- Too few warmup iterations. Early measurement iterations are slower than later ones. Check the per-iteration output. If scores keep improving, add warmup.
@Fork(0). The benchmark runs inside the JMH launcher JVM. This is useful for debugging in an IDE, but the numbers are unreliable.- A single fork. You cannot see variation between JVMs, so the reported error looks smaller than the real uncertainty.
Exercise
A benchmark is annotated with @Warmup(iterations = 5, time = 2), @Measurement(iterations = 10, time = 1) and @Fork(3). How many measurement scores does JMH record, and how many seconds does the whole run take, ignoring JVM startup?
Show answer
It records 3 forks x 10 measurement iterations = 30 scores. Each fork takes 5 x 2 = 10 seconds of warmup plus 10 x 1 = 10 seconds of measurement, so 20 seconds. Three forks take 60 seconds.
Exercise
Why does JMH run each fork in a new JVM instead of running all iterations in one JVM?
Show answer
A JVM keeps JIT profile data and compiled code from earlier work, which can change how later code is compiled. A fresh JVM per fork keeps benchmarks independent, and several forks expose variation between JVM runs.