Supported Benchmarking Crates

CodSpeed offers compatibility layers for several popular benchmarking crates:

divan (recommended)

The most convenient way to run your Rust benchmarks

We recommend using the divan plugin due to its extensive features, such as running type-generic benchmarks. Its popularity is growing rapidly within the Rust ecosystem.

If you are already using criterion or bencher, consider their respective plugins, as they will require minimal adjustments to work with CodSpeed

How does CodSpeed work with Rust benchmarks?

Rust, being a compiled language, has CodSpeed integrations that differ from those for interpreted languages. The CodSpeed benchmarking process for Rust occurs at both build time and runtime.

To facilitate this, CodSpeed provides:

  1. A cargo-codspeed cargo subcommand: used regardless of the benchmarking crate.
  2. Compatibility layers for popular benchmarking crates: chose the appropriate one based on your project’s needs.

cargo-codspeed

To integrate CodSpeed with your Rust codebase, use the cargo subcommand: cargo-codspeed. This tool allows you to run CodSpeed benchmarks without modifying the behavior of the standard cargo bench command.

Creating benchmarks with cargo-codspeed is the same as with the supported APIs. So if you already have benchmarks written with one of these, only a minor import change is required.

Installation

To check your benchmarks with CodSpeed, you first need to install th cargo-codspeed CLI tool:

cargo install cargo-codspeed --locked

This tool can then be used directly within cargo:

$ cargo codspeed
Cargo extension to build & run your codspeed benchmarks

Usage: cargo codspeed <COMMAND>

Commands:
  build  Build the benchmarks
  run    Run the previously built benchmarks

Options:
  -h, --help     Print help information
  -V, --version  Print version information

Usage

No matter which benchmarking crate you’re using, the cargo-codspeed command is used to help you build and run the benchmark in a CodSpeed environment.

$ cargo codspeed build
    Finished release [optimized] target(s) in 0.12s
    Finished built 1 benchmark suite(s)
$ cargo codspeed run
   Collected 1 benchmark suite(s) to run
     Running example
Using codspeed-bencher-compat v1.0.0 compatibility layer
NOTICE: codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment.
Checked: benches/example.rs::a (group: benches)
Checked: benches/example.rs::b (group: benches)
        Done running bencher_example
    Finished running 1 benchmark suite(s)

Cargo Workspaces

If you’re using CodSpeed within a workspace you can use the -p flag to specify the crate to run the build command on:

cargo codspeed build -p my_package

Build only specific benchmarks

By default, build will build and run all the benchmarks in your project.

Sometimes you may want to build and run only a subset of your benchmarks. With the following folder structure:

benches/
  ├── bench1.rs
  └── bench2.rs

To build only bench1, you can pass its name as an argument to the build and commands:

cargo codspeed build bench1

Feature flags

If you’re using feature flags in your benchmark suite, you can use the --features flag to specify the features to enable:

cargo codspeed build --features my_feature

Run the built benchmarks

By default, cargo codspeed run will run all the built benchmarks (of the latest cargo codspeed build ... command you ran).

To run only a subset of the built benchmarks, you can do the following:

cargo codspeed run -p my_package # Run all the benchmarks of the `my_package` crate
cargo codspeed run bench1        # Run only the `bench1` benchmark

Continue by choosing a benchmarking crate

divan (recommended)

The most convenient way to run your Rust benchmarks