MCPcopy Index your code
hub / github.com/bheisler/criterion.rs

github.com/bheisler/criterion.rs @0.7.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.7.0 ↗ · + Follow
689 symbols 2,098 edges 88 files 172 documented · 25%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Criterion.rs

Statistics-driven Microbenchmarking in Rust

Getting Started | User Guide | Master API Docs | Released API Docs | Changelog

GitHub branch checks state | Crates.io

Criterion.rs 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.

Table of Contents

Features

  • Statistics: Statistical analysis detects if, and by how much, performance has changed since the last benchmark run
  • Charts: Uses gnuplot to generate detailed graphs of benchmark results
  • Stable-compatible: Benchmark your code without installing nightly Rust

Quickstart

In order to generate plots, you must have gnuplot installed. See the gnuplot website for installation instructions. See Compatibility Policy for details on the minimum supported Rust version.

To start with Criterion.rs, add the following to your Cargo.toml file:

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }

[[bench]]
name = "my_benchmark"
harness = false

Next, define a benchmark by creating a file at $PROJECT/benches/my_benchmark.rs with the following contents:

use std::hint::black_box;
use criterion::{criterion_group, criterion_main, Criterion};

fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 1,
        1 => 1,
        n => fibonacci(n-1) + fibonacci(n-2),
    }
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

Finally, run this benchmark with cargo bench. You should see output similar to the following:

     Running target/release/deps/example-423eedc43b2b3a93
fib 20                  time:   [26.029 us 26.251 us 26.505 us]
Found 11 outliers among 99 measurements (11.11%)
  6 (6.06%) high mild
  5 (5.05%) high severe

See the Getting Started guide for more details.

Goals

The primary goal of Criterion.rs is to provide a powerful and statistically rigorous tool for measuring the performance of code, preventing performance regressions and accurately measuring optimizations. Additionally, it should be as programmer-friendly as possible and make it easy to create reliable, useful benchmarks, even for programmers without an advanced background in statistics.

Contributing

First, thank you for contributing.

One great way to contribute to Criterion.rs is to use it for your own benchmarking needs and report your experiences, file and comment on issues, etc.

Code or documentation improvements in the form of pull requests are also welcome. If you're not sure what to work on, try checking the Beginner label.

If your issues or pull requests have no response after a few days, feel free to ping me (@bheisler).

For more details, see the CONTRIBUTING.md file.

Compatibility Policy

Criterion.rs supports the last three stable minor releases of Rust. At time of writing, this means Rust 1.59 or later. Older versions may work, but are not guaranteed.

Currently, the oldest version of Rust believed to work is 1.57. Future versions of Criterion.rs may break support for such old versions, and this will not be considered a breaking change. If you require Criterion.rs to work on old versions of Rust, you will need to stick to a specific patch version of Criterion.rs.

Maintenance

Criterion.rs was originally created by Jorge Aparicio (@japaric) and is currently being maintained by Brook Heisler (@bheisler).

License

Criterion.rs is dual licensed under the Apache 2.0 license and the MIT license.

Related Projects

  • bencher - A port of the libtest benchmark runner to stable Rust
  • criterion - The Haskell microbenchmarking library that inspired Criterion.rs
  • cargo-benchcmp - Cargo subcommand to compare the output of two libtest or bencher benchmark runs
  • cargo-flamegraph - Cargo subcommand to profile an executable and produce a flamegraph

Criterion.rs Extensions

Extension points exported contracts — how you extend this code

Set (Interface)
Overloaded `set` method [39 implementers]
plot/src/traits.rs
AsyncExecutor (Interface)
Plugin trait used to allow benchmarking on multiple different async runtimes. Smol, Tokio and Async-std are supported o [7 …
src/async_executor.rs
Tuple (Interface)
Any tuple: `(A, B, ..)` [4 implementers]
src/stats/tuple.rs
Report (Interface)
(no doc) [5 implementers]
src/report.rs
Profiler (Interface)
Extension trait for external crates to implement which provides start/stop hooks when profiling (but not when benchmarki [2 …
src/profiler.rs
ValueFormatter (Interface)
Trait providing functions to format measured values to string so that they can be displayed on the command line or in th [2 …
src/measurement.rs
IntoBenchmarkId (Interface)
Sealed trait which allows users to automatically convert strings to benchmark IDs. [2 implementers]
src/benchmark_group.rs
Routine (Interface)
PRIVATE [1 implementers]
src/routine.rs

Core symbols most depended-on inside this repo

set
called by 254
plot/src/lib.rs
iter
called by 141
src/stats/bivariate/mod.rs
map
called by 92
src/stats/univariate/kde/mod.rs
push
called by 86
src/stats/tuple.rs
bench_function
called by 69
src/lib.rs
len
called by 58
src/stats/bivariate/mod.rs
configure
called by 55
plot/src/lib.rs
max
called by 51
src/stats/univariate/sample.rs

Shape

Method 334
Function 184
Class 101
Enum 45
Interface 25

Languages

Rust100%
Python1%

Modules by API surface

src/report.rs63 symbols
src/lib.rs59 symbols
plot/src/lib.rs53 symbols
tests/criterion_tests.rs41 symbols
src/html/mod.rs30 symbols
src/benchmark_group.rs24 symbols
src/measurement.rs19 symbols
src/stats/univariate/sample.rs18 symbols
src/connection.rs18 symbols
src/stats/univariate/outliers/tukey.rs17 symbols
benches/benchmarks/custom_measurement.rs16 symbols
src/estimate.rs14 symbols

For agents

$ claude mcp add criterion.rs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page