Sub topic 13
Using benchmarks in continuous integration
This chapter assumes you can write a Criterion benchmark with harness = false, and that you know how to save and compare baselines with --save-baseline and --baseline. It also assumes basic familiarity with a CI service such as GitHub Actions.
What CI can and cannot tell you
Continuous integration (CI) runs your checks automatically on every push or pull request. Benchmarks in CI can catch a slowdown before it merges. Two properties of CI make this harder than running locally:
- Shared runners are virtual machines. Other jobs on the same host change your timings from run to run.
- Criterion reports regressions in its output but does not fail the build.
cargo benchexits with status 0 even when it prints "Performance has regressed."
So you have to decide what CI is for. There are three common levels.
| Level | Command | What it catches | Reliability |
|---|---|---|---|
| Smoke test | `cargo bench -- --test` | Benchmarks that no longer compile or panic | High |
| Same-job comparison | Save a baseline, then compare | Large regressions | Medium |
| Absolute thresholds on shared runners | Compare to numbers from another day | Almost nothing reliably | Low |
Level 1: check that benchmarks still run
With --test, Criterion runs each benchmark function once without measuring. This takes seconds and keeps your benchmark code from rotting.
cargo bench --bench fib_bench -- --testName the bench target with --bench. Without it, Cargo also passes --test to the default test harness of your library, which does not understand Criterion's other flags and fails with "Unrecognized option".
Level 2: compare two commits on one machine
Timings from different runners are not comparable. Timings from the same job, minutes apart, are much closer. The approach is to benchmark the base commit and the new commit inside one job.
name: bench
on: pull_request
jobs:
bench:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: dtolnay/rust-toolchain@stable
- name: Benchmark the base commit
run: |
git checkout ${{ github.event.pull_request.base.sha }}
cargo bench --bench fib_bench -- --save-baseline base --noplot
- name: Benchmark the pull request
run: |
git checkout ${{ github.event.pull_request.head.sha }}
cargo bench --bench fib_bench -- --baseline base --noplot | tee bench.txt
- name: Fail on regression
run: "! grep -q 'Performance has regressed' bench.txt"Several details matter here:
- Criterion stores baselines under
target/criterion. Checking out another commit does not touchtarget, so thebasebaseline survives the switch. --noplotskips chart generation, which saves time when nobody opens the HTML report.- The last step turns Criterion's text output into a pass/fail signal. It is crude, but it needs no extra tools.
- The
! grepform fails the step when the phrase is found. The step is quoted in YAML because it starts with!.
Reducing false alarms
A flaky benchmark job gets ignored, which is worse than having none. These settings help:
- Raise the noise threshold so small changes do not count. Criterion treats changes within 1% as noise by default. On shared runners, try
--noise-threshold 0.05. - Lower the significance level to demand stronger evidence, for example
--significance-level 0.01. - Increase
--sample-sizeor--measurement-timefor benchmarks that jump around. - Re-run the failed job once before blocking a merge. If the regression is real, it appears again.
- Benchmark only code on hot paths. Fewer benchmarks means a shorter job and fewer chances for a false positive.
cargo bench --bench fib_bench -- --baseline base --noise-threshold 0.05 --significance-level 0.01When shared runners are not enough
If you need trustworthy absolute numbers, use a dedicated machine with a fixed CPU frequency and no other workloads. Alternatively, count instructions instead of time with a tool such as Iai or Valgrind-based benchmarks. Instruction counts do not vary with neighbors on the host, but they ignore cache and memory effects.
Exercise
A pull request job runs cargo bench -- --baseline base and prints "Performance has regressed", yet the job is green. Why, and what is one way to make it fail?
Show answer
Criterion reports regressions in its output but exits with status 0, so the CI service sees success. Capture the output (for example with tee bench.txt) and add a step such as ! grep -q 'Performance has regressed' bench.txt that fails when the phrase appears.
Exercise
Why should the base and pull request benchmarks run in the same CI job instead of comparing against a baseline from yesterday's run?
Show answer
Different jobs may run on different virtual machines with different CPUs and neighbors, so their timings are not comparable. Running both commits in one job on one runner removes most of that hardware variance.