|
1 |
| -TODO |
| 1 | +<div align="center"> |
| 2 | +<h1>codspeed-divan-compat</h1> |
| 3 | + |
| 4 | +[](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml) |
| 5 | +[](https://crates.io/crates/codspeed-divan-compat) |
| 6 | +[](https://discord.com/invite/MxpaCfKSqF) |
| 7 | +[](https://codspeed.io/CodSpeedHQ/codspeed-rust) |
| 8 | + |
| 9 | +Divan compatibility layer for CodSpeed |
| 10 | + |
| 11 | +</div> |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```sh |
| 16 | +cargo add --dev codspeed-divan-compat --rename divan |
| 17 | +``` |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> This will install the `codspeed-divan-compat` crate and rename it to `divan` in your `Cargo.toml`. |
| 21 | +> This way, you can keep your existing imports and the compatibility layer will take care of the rest. |
| 22 | +> |
| 23 | +> Using the compatibility layer won't change the behavior of your benchmark suite and divan will still run it as usual. |
| 24 | +> |
| 25 | +> If you prefer, you can also install `codspeed-divan-compat` as is and change your imports to use this new crate name. |
| 26 | +
|
| 27 | +## Usage |
| 28 | + |
| 29 | +Let's start with the example from the [divan documentation](https://docs.rs/divan/0.1.17/divan/index.html#getting-started), |
| 30 | +creating a benchmark suite for the Fibonacci function (in `benches/my_benchmark.rs`): |
| 31 | + |
| 32 | +```rust |
| 33 | +fn main() { |
| 34 | + // Run registered benchmarks. |
| 35 | + divan::main(); |
| 36 | +} |
| 37 | + |
| 38 | +// Register a `fibonacci` function and benchmark it over multiple cases. |
| 39 | +#[divan::bench(args = [1, 2, 4, 8, 16, 32])] |
| 40 | +fn fibonacci(n: u64) -> u64 { |
| 41 | + if n <= 1 { |
| 42 | + 1 |
| 43 | + } else { |
| 44 | + fibonacci(n - 2) + fibonacci(n - 1) |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +The last step in creating the divan benchmark is to add the new benchmark target in your `Cargo.toml`: |
| 50 | + |
| 51 | +```toml title="Cargo.toml" |
| 52 | +[[bench]] |
| 53 | +name = "my_benchmark" |
| 54 | +harness = false |
| 55 | +``` |
| 56 | + |
| 57 | +And that's it! You can now run your benchmark suite with `cargo-codspeed`: |
| 58 | + |
| 59 | +``` |
| 60 | +$ cargo codspeed build |
| 61 | + Finished release [optimized] target(s) in 0.12s |
| 62 | + Finished built 1 benchmark suite(s) |
| 63 | +
|
| 64 | +$ cargo codspeed run |
| 65 | + Collected 1 benchmark suite(s) to run |
| 66 | +Running my_benchmark |
| 67 | +NOTICE: codspeed is enabled, but no performance measurement will be made since it's running in an unknown environment. |
| 68 | +Checked: benches/my_benchmark.rs::fibonacci[1] |
| 69 | +Checked: benches/my_benchmark.rs::fibonacci[2] |
| 70 | +Checked: benches/my_benchmark.rs::fibonacci[4] |
| 71 | +Checked: benches/my_benchmark.rs::fibonacci[8] |
| 72 | +Checked: benches/my_benchmark.rs::fibonacci[16] |
| 73 | +Checked: benches/my_benchmark.rs::fibonacci[32] |
| 74 | +Done running my_benchmark |
| 75 | +Finished running 1 benchmark suite(s) |
| 76 | +``` |
| 77 | + |
| 78 | +### Not supported: |
| 79 | + |
| 80 | +- `divan::bench(const = xxx)`: we do not support this feature yet, if you need it don't hesitate to create an issue. |
| 81 | +- `divan::bench(crate = xxx)`: due to how the compatibility layer works internally, we do not plan to support this feature. |
0 commit comments