How to track Rust Criterion benchmarks in CI

Everett Pompeii

Everett Pompeii


Now that you have learned how to benchmark Rust code with Criterion, let’s see how to track those benchmarks in CI. Continuous Benchmarking is the practice of running benchmarks on every changeset to ensure the changes do not introduce a performance regressions. The easiest way to implement Continuous Benchmarking with Criterion benchmarks is to use Bencher.

What is Bencher?

Bencher is a suite of continuous benchmarking tools. Have you ever had a performance regression impact your users? Bencher could have prevented that from happening. Bencher allows you to detect and prevent performance regressions before they merge.

  • Run: Run your benchmarks locally or in CI using the exact same bare metal runners and your favorite benchmarking tools. The bencher CLI orchestrates running your benchmarks on bare metal and stores the results.
  • Track: Track the results of your benchmarks over time. Monitor, query, and graph the results using the Bencher web console based on the source branch, testbed, benchmark, and measure.
  • Catch: Catch performance regressions locally or in CI using the exact same bare metal hardware. Bencher uses state of the art, customizable analytics to detect performance regressions before they merge.

For the same reasons that unit tests are run to prevent feature regressions, benchmarks should be run with Bencher to prevent performance regressions. Performance bugs are bugs!

Steps for Bencher Cloud

  1. Create a Bencher Cloud account.
  2. Create a user API key and add it to your CI as a secret.
  3. Create a workflow for your CI, like GitHub Actions or GitLab CI/CD.
  4. Install the Bencher CLI in your CI workflow.
  5. Run your benchmarks with the bencher run subcommand in your CI workflow using the rust_criterion adapter.

Steps for Bencher Self-Hosted

  1. Create a Bencher Self-Hosted instance.
  2. Create an account on your Bencher Self-Hosted instance.
  3. Create a user API key and add it to your CI as a secret.
  4. Create a workflow for your CI, like GitHub Actions or GitLab CI/CD.
  5. Install the Bencher CLI in your CI workflow. Make sure the CLI version matches the version of your Bencher Self-Hosted instance.
  6. Run your benchmarks with the bencher run subcommand in your CI workflow using the rust_criterion adapter and setting the --host option to your Bencher Self-Hosted instance URL.

🦀 Rust Criterion

The Rust Criterion Adapter (rust_criterion) expects Criterion output. For the most complete results, use cargo-criterion’s JSON messages (ie --message-format=json), which the adapter reads before the text output:

Terminal window
bencher run --adapter rust_criterion "cargo criterion --message-format=json"

Criterion’s text output is also read:

Terminal window
bencher run --adapter rust_criterion "cargo bench"

Each Benchmark is named by its Criterion benchmark id, such as fib 20 or checksum/fold/1024, with no Parameters. From the JSON messages, the latency Measure (ie nanoseconds (ns)) is gathered with these Metrics, each with the bounds of its confidence interval (ie mean_ci_lower and mean_ci_upper):

  • typical: Criterion’s own estimate, the slope when Criterion computes one and the mean otherwise
  • mean and median, and the slope when Criterion computes one
  • median_abs_dev: the median absolute deviation

From the text output, the latency Measure is gathered as typical with typical_ci_lower and typical_ci_upper. With --verbose, Criterion also prints the intervals of the slope, mean, std_dev, median, and median_abs_dev, which are gathered as their bounds (ie mean_ci_lower and mean_ci_upper).

A benchmark with a throughput gathers its rate as typical with the bounds of its interval: bytes in the bytes-throughput Measure (ie bytes / second (B/s)), and elements in the throughput Measure (ie operations / second (ops/s)). A Criterion measurement plugin that counts cycles writes to the cycles Measure, and any other unit writes to a Measure of its own, named for its unit.

Threshold the median Metric first for the JSON messages, with --threshold-metric median, and the typical Metric first for the text output, with --threshold-metric typical. A rate has only typical and its bounds, so threshold typical for the bytes-throughput and throughput Measures.

The Rust Divan adapter is tried before Criterion’s text output, so run only one benchmark harness per bencher run.

BMF version 0

Only the text output is read, and only the latency Measure is gathered. The lower_value and upper_value are the lower and upper bounds of either the slope (if available) or the mean (if not) (ie value) respectively.

Your next performance regression won't announce itself

Catch it in review, or pay for it in production.



Published: Sat, November 9, 2024 at 7:15:00 PM UTC | Last Updated: Sun, November 15, 2026 at 3:10:20 PM UTC