Sub topic 6
Reading the Console Output and HTML Report
This chapter assumes you can run cargo bench on a Criterion target with harness = false and that you have written a benchmark with bench_function. Here you will learn to read what Criterion prints and what it writes to disk.
The console output
Run the fib 20 benchmark from the earlier chapter:
cargo benchThe output looks like this. Your numbers will differ.
fib 20 time: [20.115 µs 20.208 µs 20.311 µs]
Found 6 outliers among 100 measurements (6.00%)
4 (4.00%) high mild
2 (2.00%) high severeThe time: line holds three numbers.
- The left and right numbers are the lower and upper bounds of a 95% confidence interval for the mean time of one iteration.
- The middle number is Criterion's best estimate of that mean.
A narrow interval means the measurement was stable. A wide interval means noise affected the run, so treat the middle number with caution.
Outliers
Criterion classifies samples that sit far from the rest. It uses the interquartile range (IQR), which is the spread of the middle half of the samples.
| Label | Meaning |
|---|---|
| low mild / high mild | Beyond 1.5 × IQR from the quartiles |
| low severe / high severe | Beyond 3 × IQR from the quartiles |
Most outliers are high, because something else on the machine briefly slowed the run. A few outliers are normal. If more than about 10% of samples are outliers, close other programs and run again.
Change detection
Run the same benchmark a second time. Criterion now compares the new run with the previous one stored in target/criterion.
fib 20 time: [20.051 µs 20.134 µs 20.230 µs]
change: [-1.2018% -0.6532% -0.1190%] (p = 0.02 < 0.05)
Change within noise threshold.The change: line is also a confidence interval, this time for the relative change in time. A negative value means the code got faster. A positive value means it got slower.
The p value is the probability of seeing a difference this large if nothing had really changed. Criterion compares it with a significance level of 0.05. Criterion also has a noise threshold, 1% by default. A change smaller than that is reported as "within noise threshold", even when p is small. The verdict line can say:
Performance has improved.Performance has regressed.No change in performance detected.Change within noise threshold.
The HTML report
The html_reports feature in Cargo.toml makes Criterion write reports under target/criterion. Open this file in a browser:
target/criterion/report/index.htmlIt lists every benchmark. Each benchmark also has its own page at target/criterion/<name>/report/index.html. Spaces in a name become underscores in the folder name.
A benchmark page contains these parts:
- A summary of the mean, median, standard deviation, and median absolute deviation (MAD), each with its own interval.
- A probability density plot (PDF), which shows how the iteration times were distributed. Look for one tight peak. Several peaks suggest the code has more than one typical speed, for example because of cache effects.
- A slope or regression plot. Each point is one sample, with the iteration count on the x-axis and total time on the y-axis. The points should lie near a straight line.
- A comparison plot against the previous run, when one exists.
If you benchmark with a group and BenchmarkId, the group's page also has a violin plot. It puts each input's distribution side by side.
How the pieces connect
Common reading mistakes
- Comparing the middle value alone. Check that the intervals of two runs do not overlap before you call one faster.
- Ignoring the unit. Criterion switches between ns, µs, ms, and s depending on the size of the result.
- Treating a significant
pas a large change. A tiny change can be statistically clear and still not matter.
Exercise
A benchmark prints change: [+0.4% +0.9% +1.5%] (p = 0.01 < 0.05) followed by "Change within noise threshold." Did the code regress? Explain.
Show answer
Probably not in a way that matters. The change is positive, so the code looks slightly slower, and the small p value says the difference is unlikely to be chance. But the whole interval is close to 1%, which is Criterion's default noise threshold, so it reports the change as within noise. Rerun the benchmark on a quiet machine before you act on it.
Exercise
Where do you find the HTML report for all benchmarks, and which feature must be enabled in Cargo.toml to produce it?
Show answer
Open target/criterion/report/index.html. Enable the html_reports feature on the criterion dev-dependency.