bench_matrix
bench_matrix is a Rust utility crate that supercharges your parameterized benchmarks. It provides a powerful and ergonomic framework for running benchmarks across a complex matrix of configurations, integrating seamlessly with the Criterion harness.
Stop writing repetitive benchmark functions. Define your parameter axes once, and let bench_matrix handle the rest, generating a full suite of benchmarks with clean, hierarchical reporting.
Why use bench_matrix?
- Eliminate Boilerplate: Define your parameters (e.g., data sizes, algorithms, concurrency levels) in one place.
bench_matrixgenerates the Cartesian product, ensuring every combination is tested without repetitive code. - Memory Efficient: Lazily generates benchmark combinations on the fly. You can define a test matrix with millions of variants without consuming gigabytes of memory upfront.
- Clean, Hierarchical Reports: Automatically creates well-named Criterion groups, leading to organized and readable benchmark results (e.g.,
MySuite/Algorithm-QuickSort_DataSize-1000). - Seamless Criterion Integration: Built from the ground up to work with Criterion, leveraging its powerful statistical analysis and plotting features.
- Async & Sync Ready: Provides dedicated, consistent APIs for both synchronous (
SyncBenchmarkSuite) and asynchronous (AsyncBenchmarkSuite) code. - Type-Safe & Customizable: Use your own strongly-typed configuration structs and hook into a flexible lifecycle with
setup,teardown, andglobal_setupfunctions.
A Quick Look
Here's how you can set up a benchmark for a function across multiple data sizes and processing intensities:
// In benches/my_bench.rs
use ;
use ;
criterion_group!;
criterion_main!;
This will produce benchmark results like:
DataProcessingSuite/Elements-100_Intensity-LowDataProcessingSuite/Elements-100_Intensity-HighDataProcessingSuite/Elements-1000_Intensity-LowDataProcessingSuite/Elements-1000_Intensity-High
Installation
Add bench_matrix and its companions to the [dev-dependencies] section of your Cargo.toml:
[]
= "0.2.0" # Replace with the latest version
= "0.5"
= { = "1", = ["full"] } # Required for async benchmarks
The criterion_integration feature is enabled by default.
Documentation
- Usage Guide: A comprehensive guide on concepts, API, and examples. Start here!
- API Reference (docs.rs): Detailed documentation for every public type and function.
- Examples (
benches/directory): Fully working examples demonstrating synchronous and asynchronous suites.