13 stable releases

new 2.5.1 Apr 22, 2024
2.4.0 Feb 26, 2024
2.3.3 Nov 20, 2023
2.1.0 Jul 27, 2023
1.1.0 Mar 3, 2023

#69 in Profiling

Download history 7911/week @ 2024-01-01 10861/week @ 2024-01-08 13346/week @ 2024-01-15 11351/week @ 2024-01-22 14651/week @ 2024-01-29 18177/week @ 2024-02-05 17418/week @ 2024-02-12 6142/week @ 2024-02-19 4601/week @ 2024-02-26 8225/week @ 2024-03-04 9672/week @ 2024-03-11 15416/week @ 2024-03-18 11004/week @ 2024-03-25 10775/week @ 2024-04-01 14124/week @ 2024-04-08 2511/week @ 2024-04-15

38,680 downloads per month
Used in 12 crates

MIT/Apache

40KB
949 lines

codspeed-criterion-compat

CI Crates.io Discord CodSpeed Badge

Criterion.rs compatibility layer for CodSpeed

Installation

cargo add --dev codspeed-criterion-compat

Usage

Let's start with the example from the Criterion.rs documentation, creating a benchmark suite for the Fibonacci function (in benches/my_benchmark.rs):

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:

[[bench]]
name = "my_benchmark"
harness = false

Plugging CodSpeed

To allow CodSpeed to interact with this suite as well, you simply need to replace the imports from the criterion crate to the codspeed-criterion-compat crate:

- use criterion::{black_box, criterion_group, criterion_main, Criterion};
+ use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion};

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

$ 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)

Not supported:

  • iter_custom
  • with_filter

Dependencies

~6–23MB
~306K SLoC