| 1 |
<div align="center"> |
| 2 |
<h1>Divan</h1> |
| 3 |
<a href="https://docs.rs/divan"> |
| 4 |
<img src="https://img.shields.io/crates/v/divan.svg?label=docs&color=blue&logo=rust" alt="docs.rs badge"> |
| 5 |
</a> |
| 6 |
<a href="https://crates.io/crates/divan"> |
| 7 |
<img src="https://img.shields.io/crates/d/divan.svg" alt="Downloads badge"> |
| 8 |
</a> |
| 9 |
<a href="https://github.com/nvzqz/divan"> |
| 10 |
<img src="https://img.shields.io/github/stars/nvzqz/divan.svg?style=flat&color=black" alt="GitHub stars badge"> |
| 11 |
</a> |
| 12 |
<a href="https://github.com/nvzqz/divan/actions/workflows/ci.yml"> |
| 13 |
<img src="https://github.com/nvzqz/divan/actions/workflows/ci.yml/badge.svg" alt="CI build status badge"> |
| 14 |
</a> |
| 15 |
<p> |
| 16 |
<strong>Comfy bench</strong>marking for Rust projects, brought to you by |
| 17 |
<a href="https://nikolaivazquez.com">Nikolai Vazquez</a>. |
| 18 |
</p> |
| 19 |
</div> |
| 20 |
|
| 21 |
## Sponsor |
| 22 |
|
| 23 |
If you or your company find Divan valuable, consider [sponsoring on |
| 24 |
GitHub](https://github.com/sponsors/nvzqz) or [donating via |
| 25 |
PayPal](https://paypal.me/nvzqz). Sponsorships help me progress on what's |
| 26 |
possible with benchmarking in Rust. |
| 27 |
|
| 28 |
## Guide |
| 29 |
|
| 30 |
A guide is being worked on. In the meantime, see: |
| 31 |
- [Announcement post](https://nikolaivazquez.com/blog/divan/) |
| 32 |
- ["Proving Performance" FOSDEM talk](https://youtu.be/P87C4jNakGs) |
| 33 |
|
| 34 |
## Getting Started |
| 35 |
|
| 36 |
Divan `0.1.17` requires Rust `1.80.0` or later. |
| 37 |
|
| 38 |
1. Add the following to your project's [`Cargo.toml`](https://doc.rust-lang.org/cargo/reference/manifest.html): |
| 39 |
|
| 40 |
```toml |
| 41 |
[dev-dependencies] |
| 42 |
divan = "0.1.17" |
| 43 |
|
| 44 |
[[bench]] |
| 45 |
name = "example" |
| 46 |
harness = false |
| 47 |
``` |
| 48 |
|
| 49 |
2. Create a benchmarks file at `benches/example.rs`[^1] with your benchmarking code: |
| 50 |
|
| 51 |
```rust |
| 52 |
fn main() { |
| 53 |
// Run registered benchmarks. |
| 54 |
divan::main(); |
| 55 |
} |
| 56 |
|
| 57 |
// Register a `fibonacci` function and benchmark it over multiple cases. |
| 58 |
#[divan::bench(args = [1, 2, 4, 8, 16, 32])] |
| 59 |
fn fibonacci(n: u64) -> u64 { |
| 60 |
if n <= 1 { |
| 61 |
1 |
| 62 |
} else { |
| 63 |
fibonacci(n - 2) + fibonacci(n - 1) |
| 64 |
} |
| 65 |
} |
| 66 |
``` |
| 67 |
|
| 68 |
3. Run your benchmarks with [`cargo bench`](https://doc.rust-lang.org/cargo/commands/cargo-bench.html): |
| 69 |
|
| 70 |
```txt |
| 71 |
example fastest │ slowest │ median │ mean │ samples │ iters |
| 72 |
╰─ fibonacci │ │ │ │ │ |
| 73 |
├─ 1 0.626 ns │ 1.735 ns │ 0.657 ns │ 0.672 ns │ 100 │ 819200 |
| 74 |
├─ 2 2.767 ns │ 3.154 ns │ 2.788 ns │ 2.851 ns │ 100 │ 204800 |
| 75 |
├─ 4 6.816 ns │ 7.671 ns │ 7.061 ns │ 7.167 ns │ 100 │ 102400 |
| 76 |
├─ 8 57.31 ns │ 62.51 ns │ 57.96 ns │ 58.55 ns │ 100 │ 12800 |
| 77 |
├─ 16 2.874 µs │ 3.812 µs │ 2.916 µs │ 3.006 µs │ 100 │ 200 |
| 78 |
╰─ 32 6.267 ms │ 6.954 ms │ 6.283 ms │ 6.344 ms │ 100 │ 100 |
| 79 |
``` |
| 80 |
|
| 81 |
See [`#[divan::bench]`][bench_attr] for info on benchmark function registration. |
| 82 |
|
| 83 |
## Examples |
| 84 |
|
| 85 |
Practical example benchmarks can be found in the [`examples/benches`](https://github.com/nvzqz/divan/tree/main/examples/benches) |
| 86 |
directory. These can be benchmarked locally by running: |
| 87 |
|
| 88 |
```sh |
| 89 |
git clone https://github.com/nvzqz/divan.git |
| 90 |
cd divan |
| 91 |
|
| 92 |
cargo bench -q -p examples --all-features |
| 93 |
``` |
| 94 |
|
| 95 |
More thorough usage examples can be found in the [`#[divan::bench]` documentation][bench_attr_examples]. |
| 96 |
|
| 97 |
## License |
| 98 |
|
| 99 |
Like the Rust project, this library may be used under either the |
| 100 |
[MIT License](https://github.com/nvzqz/divan/blob/main/LICENSE-MIT) or |
| 101 |
[Apache License (Version 2.0)](https://github.com/nvzqz/divan/blob/main/LICENSE-APACHE). |
| 102 |
|
| 103 |
[^1]: Within your crate directory, i.e. [`$CARGO_MANIFEST_DIR`](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates) |
| 104 |
|
| 105 |
[bench_attr]: https://docs.rs/divan/latest/divan/attr.bench.html |
| 106 |
[bench_attr_examples]: https://docs.rs/divan/latest/divan/attr.bench.html#examples |