> ## Documentation Index
> Fetch the complete documentation index at: https://codspeed.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing Benchmarks with divan

> Using the divan compatibility layer for CodSpeed

## Installation

> [!NOTE]
>
> For all Rust integrations, you will need
> [the `cargo-codspeed` command](https://codspeed.io/docs/benchmarks/rust/overview#cargo-codspeed) to
> build and run your CodSpeed benchmarks

Install the
[`divan` compatibility layer](https://crates.io/crates/codspeed-divan-compat):

```sh theme={null}
cargo add --dev codspeed-divan-compat --rename divan
```

Or directly change your `Cargo.toml` if you already have `divan` installed:

```toml theme={null}
[dev-dependencies]
divan = { package = "codspeed-divan-compat", version = "*" }
```

This will install the `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.

> [!TIP]
>
> The compatibility layer is a passthrough when not using `cargo codspeed`,
> running `cargo bench` will behave exactly as with the default `divan` crate.

> [!NOTE]
>
> 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](https://docs.rs/divan/0.1.17/divan/#getting-started): a
benchmark suite for the Fibonacci function:

```rust benches/my_benchmark.rs theme={null}
fn main() {
    // Run registered benchmarks.
    divan::main();
}

// Register a `fibonacci` function and benchmark it over multiple cases.
#[divan::bench(args = [1, 2, 4, 8, 16, 32])]
fn fibonacci(n: u64) -> u64 {
    if n <= 1 {
        1
    } else {
        fibonacci(n - 2) + fibonacci(n - 1)
    }
}
```

The last step in creating the divan benchmark is to add the new benchmark target
in your `Cargo.toml`:

```toml Cargo.toml theme={null}
[[bench]]
name = "my_benchmark"
harness = false
```

And that's it! You can now run your benchmark suite with CodSpeed

### Testing the benchmarks locally

```shellsession title=terminal icon="square-terminal" theme={null}
$ 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 my_benchmark
NOTICE: codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment.
Checked: benches/my_benchmark.rs::fibo_bench[1]
Checked: benches/my_benchmark.rs::fibo_bench[2]
Checked: benches/my_benchmark.rs::fibo_bench[4]
Checked: benches/my_benchmark.rs::fibo_bench[8]
Checked: benches/my_benchmark.rs::fibo_bench[16]
Checked: benches/my_benchmark.rs::fibo_bench[32]
        Done running my_benchmark
    Finished running 1 benchmark suite(s)
```

Congrats ! 🎉 You can now
[run those benchmark in your CI](#running-the-benchmarks-in-your-ci) to get the
actual performance measurements.

> [!NOTE]
>
> Use `--measurement-mode` / `-m` to select the CodSpeed instrument:
>
> * **`simulation`** (default): Runs benchmarks once on a
>   [simulated CPU](https://codspeed.io/docs/instruments/cpu.md) for consistent measurements.
> * **`walltime`**: Measures [wall-clock time](https://codspeed.io/docs/instruments/walltime.md) for
>   real-world scenarios.
> * **`memory`**: Benchmarks are run once using
>   [memory profiling](https://codspeed.io/docs/instruments/memory.md) to track heap allocations and memory
>   usage.
>
> See the
> [`cargo-codspeed` reference](https://codspeed.io/docs/reference/codspeed-rust/cargo-codspeed#the-measurement-mode-flag)
> 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.

> [!TIP]
>
> If you want more details on how to configure the CodSpeed action, you can check
> out the [Continuous Reporting section](https://codspeed.io/docs/integrations/ci.md).

Here is an example of a GitHub Actions workflow that runs the benchmarks and
reports the results to CodSpeed on every push to the `main` branch and every
pull request:

```yaml .github/workflows/codspeed.yml icon="github" theme={null}
name: CodSpeed Benchmarks

on:
  push:
    branches:
      - "main" # or "master"
  pull_request:
  # `workflow_dispatch` allows CodSpeed to trigger backtest
  # performance analysis in order to generate initial data.
  workflow_dispatch:

jobs:
  benchmarks:
    name: Run benchmarks
    runs-on: ubuntu-latest
    permissions: # optional for public repositories
      contents: read # required for actions/checkout
      id-token: write # required for OIDC authentication with CodSpeed
    steps:
      - uses: actions/checkout@v7
      - name: Setup rust toolchain, cache and cargo-codspeed binary
        uses: moonrepo/setup-rust@v1
        with:
          channel: stable
          cache-target: release
          bins: cargo-codspeed
      - name: Build the benchmark target(s)
        run: cargo codspeed build
      - name: Run the benchmarks
        uses: CodSpeedHQ/action@v5
        with:
          mode: simulation
          run: cargo codspeed run
```

### Advanced usage

Divan provides a lot of convenient features to help you write benchmarks, below
is a selection that can be useful in CodSpeed benchmarks, but check out the
[divan documentation](https://docs.rs/divan/latest/divan/) for an exhaustive
list of features.

> [card] [How to Benchmark Rust with divan](https://codspeed.io/docs/guides/how-to-benchmark-rust-with-divan.md)
>
> An in-depth guide to writing divan benchmarks: parameterized benchmarks, type
> generics, dynamic inputs, and CodSpeed CI integration.

#### Type generics

```rust benches/types.rs theme={null}
#[divan::bench(types = [&str, String])]
fn from_str<'a, T>() -> T
where
    T: From<&'a str>,
{
    divan::black_box("hello world").into()
}
```

#### Combining type generics and arguments

```rust benches/types_and_args.rs theme={null}
use std::collections::{BTreeSet, HashSet};

#[divan::bench(
    types = [Vec<i32>, BTreeSet<i32>, HashSet<i32>],
    args = [0, 2, 4, 16, 256, 4096],
)]
fn from_range<T>(n: i32) -> T
where
    T: FromIterator<i32>,
{
    (0..n).collect()
}
```

#### Generating dynamic inputs

Time spent generating inputs is not measured in benchmarks.

```rust benches/with_inputs.rs theme={null}
#[divan::bench]
fn bench(bencher: divan::Bencher) {
    bencher
        .with_inputs(|| {
            // Generate input:
            String::from("...")
        })
        .bench_values(|s| {
            // Use input by-value:
            s + "123"
        });
}
```

## 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:

```yaml highlight={5,6,7,8,9,19,24} theme={null}
jobs:
  benchmarks:
    name: Run benchmarks
    runs-on: ubuntu-latest
    strategy:
      matrix:
        package:
          - my-first-package
          - my-second-package
    steps:
      - uses: actions/checkout@v7
      - name: Setup rust toolchain, cache and cargo-codspeed binary
        uses: moonrepo/setup-rust@v1
        with:
          channel: stable
          cache-target: release
          bins: cargo-codspeed
      - name: Build the benchmark target(s)
        run: cargo codspeed build -p ${{ matrix.package }}
      - name: Run the benchmarks
        uses: CodSpeedHQ/action@v5
        with:
          mode: simulation
          run: cargo codspeed run # only runs the built benchmarks
```

> [!TIP]
>
> It is not required to pass a `-p` flag as only the benchmarks built by `cargo
>     codspeed build` will be run.

For more information about multiple packages, check
[the cargo-codspeed docs](https://codspeed.io/docs/benchmarks/rust.md).

> [!CAUTION]
>
> **Same benchmark with different variations**
>
> For 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:
>
> ![Multiple Benchmark Variations Error Message](https://mintcdn.com/codspeed/jKaxX6yy-Kzw1C-0/assets/parallel-benchmarks-variations-warning.png?fit=max\&auto=format\&n=jKaxX6yy-Kzw1C-0\&q=85\&s=f5e87d3fecc76f2fe3ce7f302e7f9a93)

Learn more about
[benchmark sharding and how to integrate with your CI provider](https://codspeed.io/docs/features/sharded-benchmarks.md).

> [!NOTE]
>
> To combine measurement modes like simulation and memory, check out the
> documentation on [running multiple instruments
> serially](https://codspeed.io/docs/integrations/ci/github-actions/configuration.md#running-multiple-instruments-serially).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.