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

# criterion.rs compatibility layer documentation

> A compatibility layer for `criterion.rs`

export const BuildkiteIcon = props => <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" width={31} height={31} viewBox="0 0 31 31" fill="none" {...props}>
    <g clipPath="url(#a)">
      <path fill="#30F2A2" d="M1.652 6.448v9.005l9.011 4.509V10.95l-9.01-4.503Zm18.017 0v9.005l9.011-4.502-9.011-4.503Z" />
      <path fill="#14CC80" d="M19.668 15.459v9.006l9.011-4.503V10.95l-9.011 4.508Z" />
      <path fill="#14CC80" d="M10.664 10.95v9.012l9.011-4.503V6.448l-9.01 4.503Z" />
    </g>
    <defs>
      <clipPath id="a">
        <path fill="#fff" d="M1.652 6.448H28.68v18.055H1.652z" />
      </clipPath>
    </defs>
  </svg>;

export const GitLabIcon = props => <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" width={31} height={31} viewBox="0 0 31 31" fill="none" {...props}>
    <g clipPath="url(#a)">
      <path fill="#E24329" d="m29.301 12.446-.041-.105-3.968-10.356a1.034 1.034 0 0 0-1.026-.65c-.217.012-.425.09-.597.224-.17.137-.292.324-.352.534l-2.68 8.197H9.79l-2.68-8.197a1.041 1.041 0 0 0-.352-.536 1.063 1.063 0 0 0-1.214-.065c-.184.117-.327.289-.408.492l-3.976 10.35-.04.106a7.368 7.368 0 0 0 2.445 8.515l.013.011.037.026 6.044 4.526 2.99 2.264 1.822 1.375a1.224 1.224 0 0 0 1.482 0l1.821-1.375 2.99-2.264 6.082-4.553.015-.013a7.371 7.371 0 0 0 2.441-8.506Z" />
      <path fill="#FC6D26" d="m29.302 12.446-.04-.105a13.404 13.404 0 0 0-5.336 2.399l-8.715 6.59 5.551 4.195 6.081-4.554.015-.012a7.371 7.371 0 0 0 2.444-8.513Z" />
      <path fill="#FCA326" d="m9.656 25.525 2.99 2.263 1.822 1.375a1.225 1.225 0 0 0 1.482 0l1.821-1.375 2.99-2.263s-2.586-1.957-5.553-4.196c-2.968 2.24-5.552 4.196-5.552 4.196Z" />
      <path fill="#FC6D26" d="M6.493 14.74a13.386 13.386 0 0 0-5.334-2.405l-.04.105a7.368 7.368 0 0 0 2.445 8.516l.013.01.037.026 6.044 4.527 5.552-4.196-8.717-6.584Z" />
    </g>
    <defs>
      <clipPath id="a">
        <path fill="#fff" d="M.055.097h30.27v30.27H.056z" />
      </clipPath>
    </defs>
  </svg>;

export const GitHubIcon = props => <svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" viewBox="0 0 32 31" width={32} height={31} fill="none" {...props}>
    <path fill="#F4F4F5" fillRule="evenodd" d="M16.093 1.187c-7.778 0-14.066 6.334-14.066 14.17A14.159 14.159 0 0 0 11.645 28.8c.7.141.955-.305.955-.68 0-.329-.023-1.455-.023-2.628-3.913.845-4.728-1.69-4.728-1.69-.628-1.641-1.56-2.064-1.56-2.064-1.28-.867.093-.867.093-.867 1.42.093 2.166 1.454 2.166 1.454 1.258 2.158 3.284 1.549 4.099 1.173.116-.915.489-1.548.885-1.9-3.121-.329-6.404-1.549-6.404-6.992 0-1.548.558-2.815 1.443-3.8-.14-.352-.629-1.807.14-3.754 0 0 1.188-.376 3.866 1.454a13.524 13.524 0 0 1 3.516-.469c1.188 0 2.399.164 3.517.47 2.678-1.83 3.866-1.455 3.866-1.455.768 1.947.279 3.402.14 3.754.908.985 1.443 2.252 1.443 3.8 0 5.443-3.283 6.64-6.427 6.992.512.445.954 1.29.954 2.627 0 1.9-.023 3.426-.023 3.895 0 .375.257.821.955.68a14.16 14.16 0 0 0 9.618-13.443c.023-7.836-6.288-14.17-14.043-14.17Z" clipRule="evenodd" />
  </svg>;

<div className="flex flex-row gap-2 items-center">
  <a className="border-none" target="_blank" href="https://github.com/CodSpeedHQ/codspeed-rust/actions">
    <img className="my-0" noZoom src="https://github.com/CodSpeedHQ/codspeed-rust/workflows/CI/badge.svg" alt="CI Status" />
  </a>

  <a className="border-none" target="_blank" href="https://crates.io/crates/codspeed-criterion-compat">
    <img className="my-0" noZoom src="https://img.shields.io/crates/v/codspeed-criterion-compat" alt="Crates.io Version" />
  </a>
</div>

## Overview

`codspeed-criterion-compat` is a Criterion.rs compatibility layer for CodSpeed
that allows you to seamlessly integrate existing Criterion benchmarks with
CodSpeed performance measurement.

<Note>
  This crate acts as a drop-in replacement for Criterion, maintaining your
  existing benchmark code while enabling CodSpeed integration.
</Note>

## Installation

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

<Tip>
  This will install the `codspeed-criterion-compat` crate and rename it to `criterion` in your `Cargo.toml`.
  This way, you can keep your existing imports and the compatibility layer will take care of the rest.

  Using the compatibility layer won't change the behavior of your benchmark suite
  and criterion will still run it as usual.

  If you prefer, you can also install `codspeed-criterion-compat` as is and change
  your imports to use this new crate name.
</Tip>

## Usage

Let's start with the example from the
[Criterion.rs documentation](https://bheisler.github.io/criterion.rs/book/getting_started.html),
creating a benchmark suite for the Fibonacci function (in
`benches/my_benchmark.rs`):

```rust theme={null}
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 1,
        1 => 1,
        n => fibonacci(n-1) + fibonacci(n-2),
    }
}

pub fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
```

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

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

And that's it! You can now run your benchmark suite with `cargo-codspeed`:

```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
Using codspeed-criterion-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/bencher_example.rs::fib_20 (group: benches)
        Done running bencher_example
    Finished running 1 benchmark suite(s)
```

## Compatibility

### Not (yet) supported

* `iter_custom`
* `with_filter`

## Resources

* [Official criterion.rs documentation](https://bheisler.github.io/criterion.rs/book/index.html)
* [Official criterion.rs GitHub repository](https://github.com/bheisler/criterion.rs)
