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

# Continuous Benchmarking for Rust in CI

> Run criterion and divan benchmarks in GitHub Actions on every pull request, compare them against main and catch performance regressions before merge.

Keep your criterion or divan benchmarks as they are, add one crate, and run them
with `cargo codspeed` in GitHub Actions. Every pull request then gets a
comparison against main, with a flamegraph for each benchmark. The CPU
simulation instrument runs each benchmark once on a standard GitHub runner, and
the result repeats exactly from run to run.

## Swap the criterion crate

Add the compatibility layer under the name `criterion`, so your benchmark code
doesn't change:

```bash theme={null}
cargo add --dev codspeed-criterion-compat --rename criterion
```

Under plain `cargo bench`, the layer passes straight through to criterion, so
local runs behave as before. divan has its own layer, `codspeed-divan-compat`,
and bencher is supported too.

## Build and run locally

Install the cargo subcommand, then build and run the benchmarks under CodSpeed:

```bash theme={null}
cargo install cargo-codspeed --locked
cargo codspeed build
cargo codspeed run
```

## Add the workflow

Create `.github/workflows/codspeed.yml`:

```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
```

The `push` trigger on main gives every pull request a baseline to compare
against. `workflow_dispatch` lets CodSpeed backtest older commits to build the
first data points. `id-token: write` lets the action authenticate with OIDC, so
a private repository doesn't need a stored token. Public repositories can skip
the `permissions` block.

## Catch regressions on every pull request

Once the workflow runs, each pull request gets a performance report comment and
a status check named "CodSpeed Performance Analysis". Make that check required
in your branch protection, and a regression above the threshold blocks the
merge. The default threshold is 10%, and you can set one per benchmark.

## What Ruff runs

Ruff benchmarks its formatter, lexer, linter and parser this way. Its workflow
builds them with `cargo codspeed build -m simulation -m memory` and runs the
action with `mode: "simulation,memory"`, so one job reports both CPU and memory
results.

## When to use walltime instead

CPU simulation leaves out system calls. If your benchmark spends its time in
I/O, threads or the kernel, run it with `mode: walltime` on a Macro Runner.
Walltime measures elapsed time, and the runner is a bare-metal machine with
isolated cores.

Next, read
[what each number in the report measures](https://codspeed.io/docs/features/understanding-the-metrics.md).


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