Brunch
Brunch is a very simple Rust micro-benchmark runner inspired by easybench. It has roughly a million times fewer dependencies than criterion, does not require nightly, and maintains a "last run" state so can show relative changes benchmark-to-benchmark.
(The formatting is also quite pretty.)
As with all Rust benchmarking, there are a lot of caveats, and results might be artificially fast or slow. For best results:
- Build optimized;
- Collect lots of samples;
- Repeat identical runs to get a feel for the natural variation;
Brunch cannot measure time below the level of a nanosecond, so if you're trying to benchmark methods that are really fast, you may need to wrap them in a method that runs through several iterations at once. For example:
use Bench;
///# Generate Strings to Test.
///# Generate Strings to Test.
benches!;
Installation
Add brunch to your dev-dependencies in Cargo.toml, like:
brunch = "0.3.*"
Benchmarks should also be defined in Cargo.toml. Just be sure to set harness = false for each:
name = "encode"
harness = false
The following optional environmental variables are supported:
NO_BRUNCH_HISTORY=1: don't save or load run-to-run history data;BRUNCH_DIR=/some/directory: save run-to-run history data to this folder instead ofstd::env::temp_dir;
Usage
The heart of Brunch is the Bench struct, which defines a single benchmark. There isn't much configuration required, but each Bench has the following:
| Data | Description | Default |
|---|---|---|
| Name | A unique identifier. This is arbitrary, but works best as a string representation of the method itself, like foo::bar(10) |
|
| Samples | The number of samples to collect. | 2500 |
| Timeout | A cutoff time to keep it from running forever. | 10 seconds |
| Method | A method to run over and over again! |
The struct uses builder-style methods to allow everything to be set in a single chain. You always need to start with Bench::new and end with one of the runner methods — Bench::run, Bench::run_seeded, or Bench::run_seeded_with. If you want to change the sample or timeout limits, you can add Bench::with_samples or Bench::with_timeout in between.
There is also a special Bench::spacer method that can be used to inject a linebreak into the results. See below for an example.
Examples
In terms of running benchmarks, the simplest approach is to use the provided benches macro. That generates the required main() method, runs all the benches, and prints the results automatically.
use Bench;
use NiceU8;
/// # Silly seed method.
benches!;
If you prefer to handle things manually, you'll need to use the Benches struct instead. It's pretty easy too:
use ;
use NiceU8;
Interpreting Results
If you run the example benchmark for this crate, you should see a summary like the following:
Method Mean Change Samples
------------------------------------------------------------
fibonacci_recursive(30) 2.22 ms +1.02% 2,408/2,500
fibonacci_loop(30) 56.17 ns --- 2,499/2,500
The Method column speaks for itself, but the numbers deserve a little explanation:
| Column | Description |
|---|---|
| Mean | The adjusted, average execution time for a single run, scaled to the most appropriate time unit to keep the output tidy. |
| Change | The relative difference between this run and the last run, if more than two standard deviations. |
| Samples | The number of valid/total samples, the difference being outliers (5th and 95th quantiles) excluded from consideration. |
License
See also: CREDITS.md
Copyright © 2022 Blobfolio, LLC <hello@blobfolio.com>
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.