Installation
For all Rust integrations, you will need
the
cargo-codspeed command to
build and run your CodSpeed benchmarksdivan compatibility layer:
Cargo.toml if you already have divan installed:
codspeed-divan-compat crate and rename it to divan in
your Cargo.toml. This way, you can keep your existing imports and the
compatibility layer will take care of the rest.
If you prefer, you can also install
codspeed-divan-compat as is and change
your imports to use this new crate name.Usage
Creating benchmarks
As an example, let’s follow the example from the divan documentation: a benchmark suite for the Fibonacci function:benches/my_benchmark.rs
Cargo.toml:
Cargo.toml
Testing the benchmarks locally
terminal
Use
--measurement-mode / -m to select the CodSpeed instrument:simulation(default): Runs benchmarks once on a simulated CPU for consistent measurements.walltime: Measures wall-clock time for real-world scenarios.memory: Benchmarks are run once using memory profiling to track heap allocations and memory usage.
cargo-codspeed reference
for more information.Running the benchmarks in your CI
To generate performance reports, you need to run the benchmarks in your CI. This allows CodSpeed to automatically run benchmarks and warn you about regressions during development. Here is an example of a GitHub Actions workflow that runs the benchmarks and reports the results to CodSpeed on every push to themain branch and every
pull request:
.github/workflows/codspeed.yml
Advanced usage
Divan provides a lot of convenient features to help you write benchmars, below is a selection that can be useful in CodSpeed benchmarks, but check out the divan documentation for an exhaustive list of features.Type generics
benches/types.rs
Combining type generics and arguments
benches/types_and_args.rs
Generating dynamic inputs
Time spent generating inputs is not measured in benchmarks.benches/with_inputs.rs
Recipes
Running benchmarks in parallel CI jobs
With Rust, if you use multiple packages, a first sharding optimization is to split your benchmarks across these packages. For example, using Github Actions:Same benchmark with different variationsFor now, you cannot run the same benchmarks several times within the same run.
If the same benchmark is run multiple times, you will receive the following
comment on your pull request:

To combine measurement modes like simulation and memory, check out the
documentation on running multiple instruments
serially.