Skip to content

Commit fd8f45d

Browse files
feat: add readme to divan_compat
1 parent 38f7270 commit fd8f45d

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

crates/divan_compat/README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
1-
TODO
1+
<div align="center">
2+
<h1>codspeed-divan-compat</h1>
3+
4+
[![CI](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/CodSpeedHQ/codspeed-rust/actions/workflows/ci.yml)
5+
[![Crates.io](https://img.shields.io/crates/v/codspeed-divan-compat)](https://crates.io/crates/codspeed-divan-compat)
6+
[![Discord](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](https://discord.com/invite/MxpaCfKSqF)
7+
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](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

Comments
 (0)