Sub topic 6
Benchmark modes and time units
This chapter assumes you can build a JMH project and write a basic @Benchmark method, as covered earlier. It explains how to choose what JMH measures and which unit it reports.
Four ways to measure
A benchmark mode tells JMH which quantity to report. You set it with @BenchmarkMode.
| Mode | What it reports | Score reads as |
|---|---|---|
| `Mode.Throughput` | Operations completed per unit of time | ops/time, higher is better |
| `Mode.AverageTime` | Mean time of one operation | time/op, lower is better |
| `Mode.SampleTime` | Time of individually sampled calls, with percentiles | time/op, lower is better |
| `Mode.SingleShotTime` | Time of one call, with no warmup repetition | time/op, lower is better |
Throughput is the default if you add no annotation. Mode.All runs every mode in turn.
Setting the time unit
@OutputTimeUnit sets the unit of the reported score. It does not change what is measured. It only changes how the result is printed.
For AverageTime, the unit is the time per operation, such as ns/op. For Throughput, the unit is the time window, such as ops/ms. A fast operation in Throughput mode with TimeUnit.SECONDS gives a very large number. A slow operation with TimeUnit.NANOSECONDS gives a tiny, hard-to-read one.
Both annotations work on a class or on a method. A method-level annotation overrides the class-level one.
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.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class ModesBenchmark {
private double x = Math.PI;
@Benchmark
public double averageTime() {
return Math.log(x);
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public double throughput() {
return Math.log(x);
}
@Benchmark
@BenchmarkMode({Mode.SampleTime, Mode.SingleShotTime})
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public double sampledAndSingle() {
return Math.log(x);
}
}The first method inherits AverageTime and nanoseconds from the class. The second overrides both. The third lists two modes in an array, so JMH reports it twice.
Choosing a mode
- Use
AverageTimewhen you want to say "this call costs about N ns". It is the usual choice for comparing two implementations. - Use
Throughputwhen you care about work done per second, for example in a request-handling path. - Use
SampleTimewhen the tail matters. It prints percentiles such as p0.99, which show slow outliers that an average hides. - Use
SingleShotTimeto measure cold start, such as the first call into a class. Warmup does not apply, so the number is noisy and includes JIT and class-loading effects. Use many forks to get a useful result.
Overriding from the command line
You can change the mode and unit without editing the code:
java -jar target/benchmarks.jar ModesBenchmark -bm avgt -tu us-bm takes thrpt, avgt, sample, ss or all. -tu takes a unit such as ns, us, ms or s.
Common errors
- Comparing a
Throughputscore with anAverageTimescore. One is higher-is-better and the other is lower-is-better, and their units differ. - Comparing two scores in different units without converting.
0.8 us/opis slower than500 ns/op. - Reading
SingleShotTimeas a steady-state cost. - Choosing a unit so coarse that the score rounds to
0.000. Pick a unit that gives values with a few significant digits.
Exercise
A benchmark reports 12.5 ops/us in Throughput mode. What average time per operation does that imply, and which unit would you set with @OutputTimeUnit to read it as time per operation?
Show answer
One operation takes 1 / 12.5 us = 0.08 us, which is 80 ns. Switch to Mode.AverageTime with TimeUnit.NANOSECONDS to see about 80 ns/op directly.