Benchmarking
Recipe | Crates | Categories |
---|---|---|
cargo flamegraph | ||
criterion | ||
divan | ||
hyperfine |
[benchmarking.incl: titles (P2)](https://github.com/john-cd/rust_howto/issues/334)
cargo flamegraph
cargo flamegraph
generates execution flamegraphs.
criterion
criterion
⮳ is a statistically accurate benchmarking tool. criterion
⮳ helps you write fast code by detecting and measuring performance improvements or regressions, even small ones, quickly and accurately. You can optimize with confidence, knowing how each change affects the performance of your code.
use criterion::Criterion; use criterion::black_box; use criterion::criterion_group; use criterion::criterion_main; // Define a simple fibonacci function. fn fibonacci(n: u64) -> u64 { match n { 0 => 0, 1 => 1, _ => fibonacci(n - 1) + fibonacci(n - 2), } } // The benchmark_fibonacci function benchmarks the performance of the fibonacci // function with the input 20. fn benchmark_fibonacci(c: &mut Criterion) { c.bench_function("fibonacci 20", |b| b.iter(|| fibonacci(black_box(20)))); } // Use criterion_group! and criterion_main! macros to specify and run the // benchmarks. criterion_group!(benches, benchmark_fibonacci); criterion_main!(benches); // Run the benchmarks with `cargo bench`
divan
divan
⮳ is a simple yet powerful benchmarking library with allocation profiling.
// Divan is a Rust benchmarking framework. // Add the following to your project's `Cargo.toml`: // [dev-dependencies] // divan = "0.1.17" // // [[bench]] // name = "example" // harness = false // // Run the benchmark with `cargo bench` // Define a `fibonacci` function to benchmark fn compute_fibonacci(n: u64) -> u64 { if n <= 1 { 1 } else { compute_fibonacci(n - 2) + compute_fibonacci(n - 1) } } mod bench { use super::*; // Register the function above for benchmarking. #[divan::bench] // #[divan::bench( // max_time = 0.001, // seconds // sample_size = 64, // //)] fn fibonacci() -> u64 { compute_fibonacci(divan::black_box(10)) } } // When `harness` is set to false, you are responsible for defining a main() // function to run tests and benchmarks. fn main() { // Run registered benchmarks. #[cfg(not(test))] divan::main(); } // Example adapted from https://nikolaivazquez.com/blog/divan/
hyperfine
hyperfine
⮳ is a tool for benchmarking compiled binaries (similar to unix time command but better).
- Statistical analysis across multiple runs.
- Support for arbitrary shell commands.
- Constant feedback about the benchmark progress and current estimates.
- Warm-up runs can be executed before the actual benchmark.
- Cache-clearing commands can be set up before each timing run.
- Statistical outlier detection to detect interference from other programs and caching effects.
- Export results to various formats: CSV, JSON, Markdown, AsciiDoc.
- Parameterized benchmarks (e.g. vary the number of threads).
- Cross-platform
cargo install --locked hyperfine
# or
apt install hyperfine
hyperfine 'sleep 0.3'
# Change the number of runs to perform
hyperfine --runs 5 'sleep 0.3'
# Compare the runtimes of different programs
hyperfine 'hexdump file' 'xxd file'
# Run the benchmark on a warm cache
hyperfine --warmup 3 'grep -R TODO *'