Sub topic 14
Reading score, error and units
This chapter assumes you can build and run a JMH benchmark jar, and that you know the benchmark modes and @Param from earlier chapters. It covers what the summary table at the end of a run contains and how to compare two benchmarks without fooling yourself.
The summary table
When a run finishes, JMH prints one row per benchmark and parameter combination. This is an illustrative result for the SumBenchmark class from earlier, run with 2 forks and 5 measurement iterations:
Benchmark (size) Mode Cnt Score Error Units
SumBenchmark.sum 100 avgt 10 28.412 ± 0.356 ns/op
SumBenchmark.sum 10000 avgt 10 2874.905 ± 41.730 ns/opEach column means the following.
| Column | Meaning |
|---|---|
| Benchmark | Class and method name. |
| (size) | One column per `@Param` field, showing the value used for that row. |
| Mode | `thrpt`, `avgt`, `sample`, `ss`, matching the `@BenchmarkMode` setting. |
| Cnt | Number of measurement iterations that went into the score, summed over all forks. |
| Score | The aggregate result. For `avgt` it is the mean across iterations. |
| Error | Half-width of the 99.9% confidence interval around the score. |
| Units | The unit of the score, which depends on the mode and `@OutputTimeUnit`. |
Cnt is forks times measurement iterations. With @Fork(2) and 5 measurement iterations you get 10. Warmup iterations are not counted. If Cnt is not what you expected, a command-line flag such as -f or -i probably overrode your annotations.
Units follow the mode
The unit string is built from the mode and the output time unit. Reading it correctly matters because the same code gives numbers that point in opposite directions.
| Mode | Example units | Better is |
|---|---|---|
| `Throughput` (`thrpt`) | `ops/s`, `ops/ms` | Higher |
| `AverageTime` (`avgt`) | `ns/op`, `us/op` | Lower |
| `SampleTime` (`sample`) | `ns/op` | Lower |
| `SingleShotTime` (`ss`) | `ms/op` | Lower |
A throughput score of 2,000,000 ops/s and an average time of 500 ns/op describe the same speed. Never compare a thrpt row with an avgt row directly. Convert one into the other first, and only when both were measured under the same conditions.
You can change the units without editing code. The -tu flag sets the time unit and -bm sets the mode:
java -jar target/benchmarks.jar SumBenchmark -bm thrpt -tu msPick a unit that keeps the score in a readable range. A score like 0.000028 s/op is accurate but hard to compare by eye. A score like 28.4 ns/op is easy to compare.
What the error value says
Error is half the width of a 99.9% confidence interval. JMH computes it from the per-iteration scores, treating them as samples from a distribution. The true mean of the benchmark is likely to lie within score ± error.
For the first row above, the interval is 28.412 ± 0.356, so roughly 28.06 to 28.77 ns/op. Three details are easy to miss:
- The error comes from the variation between iterations. It does not include systematic bias such as dead-code elimination or a bad input. A tiny error on a wrong benchmark is still wrong.
- More iterations and forks tighten the interval, but only slowly. Halving the error takes about four times as many samples.
- The error is not a standard deviation. Do not read it as the spread of single operations. Use
samplemode if you need the distribution of individual calls.
An error that is large relative to the score, say more than 10 to 20 percent, means the result is noisy. Typical causes are other processes on the machine, CPU frequency scaling, a laptop on battery, garbage collection pauses, or forks that settle at different steady states.
Per-iteration output and what it reveals
The summary hides how each iteration behaved. Scroll up in the console output and you will see lines like this for each fork:
# Fork: 1 of 2
# Warmup Iteration 1: 41.882 ns/op
# Warmup Iteration 2: 29.104 ns/op
# Warmup Iteration 3: 28.377 ns/op
Iteration 1: 28.401 ns/op
Iteration 2: 28.390 ns/op
Iteration 3: 28.455 ns/opCheck three things here:
- Warmup values should fall and level off. If the last warmup iterations are still dropping, the benchmark has not reached steady state and the measured iterations include compilation effects. Add warmup iterations or time.
- Measured iterations within one fork should be close to each other.
- Compare forks to each other. If fork 1 settles near 28 ns/op and fork 2 near 35 ns/op, the JIT made different decisions in each JVM. The mean of the two is not a meaningful single number, and the large error tells you so.
Comparing two results
To compare two benchmarks, put them in the same run, on the same machine, with the same mode, units, forks and iteration counts. Then apply these rules.
- Compare the intervals, not only the scores. If
a ± eaandb ± ebdo not overlap, the difference is likely real. If they overlap, you cannot tell them apart with this data. - Compute the ratio and report it with the error in mind. "B is about 1.4 times faster than A" is more useful than "B is 12 ns faster".
- Normalize when input sizes differ. In the example above, 2874.905 ns for 10,000 elements is about 0.29 ns per element, while 28.412 ns for 100 elements is about 0.28 ns per element. The work scales linearly, and the per-element cost is almost the same.
- Do not compare numbers from different machines, JDK versions or JVM flags. Treat each machine and JVM as its own experiment.
Saving results for later
Console text is awkward to compare across runs. JMH can write machine-readable results with -rf (format) and -rff (file):
java -jar target/benchmarks.jar SumBenchmark -rf json -rff results.jsonSupported formats include text, csv, scsv, json and latex. The JSON file holds the primary metric (score, error, unit, and the raw per-iteration data) for each benchmark, plus any secondary metrics from profilers. The same options exist in the Runner API:
package org.sample;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
public class SavedRunner {
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(SumBenchmark.class.getSimpleName())
.resultFormat(ResultFormatType.JSON)
.result("results.json")
.build();
new Runner(options).run();
}
}Keep the JSON files from before and after a change. That lets you compare the two runs later with a script or a visualizer instead of from memory.
Secondary metrics
Some options add extra rows under the main result, with their own units. With the GC profiler enabled, you will see lines such as these:
SumBenchmark.sum:·gc.alloc.rate.norm 10000 avgt 10 0.001 ± 0.001 B/opThe :· marks a secondary result belonging to the benchmark above it. Read it with its own unit. B/op is bytes allocated per operation. A value near zero means the benchmark allocates nothing per call, so garbage collection is not what drives its time.
Common misreadings
- Treating the score as exact. Report it with its error, and round to the precision the error supports. "28.4 ns/op" is honest. "28.412 ns/op" claims more than the data holds when the error is ±0.356.
- Ignoring
Cnt. A very low count makes the error itself unreliable. - Reading microbenchmark numbers as application speed. A 20 percent gain in an isolated method may vanish in a program where that method is a small part of the total time.
- Comparing rows with different
@Paramvalues as though they were the same benchmark. They are separate experiments.
Exercise
A run prints these two rows, both with mode avgt, units ns/op and Cnt 10: ListSum.loop 412.6 ± 18.9 and ListSum.stream 431.2 ± 22.4. Can you conclude the loop is faster? Explain using the intervals.
Show answer
No. The loop interval is about 393.7 to 431.5 and the stream interval is about 408.8 to 453.6. They overlap between 408.8 and 431.5, so this run cannot separate them. You would need more forks or iterations, or less noise, to narrow the errors before claiming a difference.
Exercise
A benchmark in thrpt mode reports Score 4,000,000 ops/s. What average time per operation does that correspond to, in ns/op, and which mode would give the same information directly?
Show answer
One second is 1,000,000,000 ns, so 1,000,000,000 / 4,000,000 = 250 ns/op. AverageTime mode reports this directly. This conversion is only valid when the benchmark runs on a single thread and both measurements use the same conditions.