|
1 |
| -# codspeed-criterion-compat |
| 1 | +<div align="center"> |
| 2 | +<h1>codspeed-criterion-compat</h1> |
2 | 3 |
|
3 |
| -### Not supported yet |
| 4 | +[](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml) |
| 5 | + |
4 | 6 |
|
5 |
| -- AsyncBencher (Criterion) |
| 7 | +Criterion.rs compatibility layer for CodSpeed |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +```sh |
| 14 | +cargo add --dev codspeed-criterion-compat |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Let's start with the example from the [Criterion.rs documentation](https://bheisler.github.io/criterion.rs/book/getting_started.html), |
| 20 | +creating a benchmark suite for the Fibonacci function (in `benches/my_benchmark.rs`): |
| 21 | + |
| 22 | +```rust |
| 23 | +use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 24 | + |
| 25 | +fn fibonacci(n: u64) -> u64 { |
| 26 | + match n { |
| 27 | + 0 => 1, |
| 28 | + 1 => 1, |
| 29 | + n => fibonacci(n-1) + fibonacci(n-2), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub fn criterion_benchmark(c: &mut Criterion) { |
| 34 | + c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20)))); |
| 35 | +} |
| 36 | + |
| 37 | +criterion_group!(benches, criterion_benchmark); |
| 38 | +criterion_main!(benches); |
| 39 | +``` |
| 40 | + |
| 41 | +The last step in creating the Criterion benchmark is to add the new benchmark target in your `Cargo.toml`: |
| 42 | + |
| 43 | +```toml title="Cargo.toml" |
| 44 | +[[bench]] |
| 45 | +name = "my_benchmark" |
| 46 | +harness = false |
| 47 | +``` |
| 48 | + |
| 49 | +### Plugging CodSpeed |
| 50 | + |
| 51 | +To allow CodSpeed to interact with this suite as well, you simply need to replace |
| 52 | +the imports from the `criterion` crate to the `codspeed-criterion-compat` crate: |
| 53 | + |
| 54 | +```diff |
| 55 | +- use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 56 | ++ use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion}; |
| 57 | +``` |
| 58 | + |
| 59 | +And that's it! You can now run your benchmark suite with `cargo-codspeed`: |
| 60 | + |
| 61 | +``` |
| 62 | +$ cargo codspeed build |
| 63 | + Finished release [optimized] target(s) in 0.12s |
| 64 | + Finished built 1 benchmark suite(s) |
| 65 | +
|
| 66 | +$ cargo codspeed run |
| 67 | + Collected 1 benchmark suite(s) to run |
| 68 | + Running my_benchmark |
| 69 | +Using codspeed-criterion-compat v1.0.0 compatibility layer |
| 70 | +NOTICE: codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment. |
| 71 | +Checked: benches/bencher_example.rs::fib_20 (group: benches) |
| 72 | + Done running bencher_example |
| 73 | + Finished running 1 benchmark suite(s) |
| 74 | +``` |
| 75 | + |
| 76 | +### Not supported yet: |
| 77 | + |
| 78 | +- AsyncBencher |
0 commit comments