LinuxCommandLibrary

cargo-bench

Benchmark Rust code

TLDR

Execute all benchmarks of a package

$ cargo bench
copy

Don't stop when a benchmark fails
$ cargo bench --no-fail-fast
copy

Compile, but don't run benchmarks
$ cargo bench --no-run
copy

Benchmark the specified benchmark
$ cargo bench --bench [benchmark]
copy

Benchmark with the given profile (default: bench)
$ cargo bench --profile [profile]
copy

Benchmark all example targets
$ cargo bench --examples
copy

Benchmark all binary targets
$ cargo bench --bins
copy

Benchmark the package's library
$ cargo bench --lib
copy

SYNOPSIS

cargo bench [OPTIONS] [--] [<ARGS>]

PARAMETERS

--bench <NAME>
    Run only the specified benchmark

--no-run
    Compile benchmarks but do not run them

-p SPEC, --package SPEC
    Benchmark only the specified package

--workspace, --workspaces
    Benchmark all packages in the workspace

--jobs N
    Number of parallel jobs (default: number of CPUs)

--lib
    Benchmark the library

--bench NAME
    Benchmark the specified bench binary

--features FEATURES
    Space or comma separated list of features to activate

--all-features
    Activate all available features

--no-default-features
    Do not activate the default features

--target TRIPLE
    Target triple to bench

-r, --release
    Build in release mode (default for bench)

--manifest-path PATH
    Path to Cargo.toml

-v, --verbose
    Use verbose output

--quiet
    No output printed to stdout

DESCRIPTION

The cargo bench command compiles and executes benchmarks in a Rust package. Benchmarks are defined using the #[bench] attribute in lib.rs or separate files in the benches/ directory. It utilizes Rust's built-in benchmarking harness from libtest, measuring execution time over multiple iterations for statistical accuracy.

By default, cargo bench builds in release mode (--release) for optimized performance metrics. It supports package-specific selection, workspace execution, and integration with external benchmarking libraries like Criterion, which provide advanced features such as plotting and regression detection.

Requires a Cargo.toml with bench dependencies if using external crates. Output includes timings, standard deviations, and comparisons. Ideal for performance regression testing in CI pipelines.

CAVEATS

Built-in libtest harness is deprecated; prefer Criterion for production use. Benchmarks run in release mode only. Requires bench profile in Cargo.toml for custom setups. Not suitable for short-running functions due to overhead.

EXAMPLE USAGE

cargo bench # Run all benchmarks
cargo bench --bench regex # Run specific benchmark
cargo bench --no-run # Compile only

CONFIGURATION

Benchmarks require [[bench]] sections in Cargo.toml for external deps like criterion = "0.5". Use #[criterion::criterion_group] for custom suites.

HISTORY

Introduced in Cargo 0.9.0 (February 2015) alongside initial benchmarking support. Enhanced in Cargo 1.0 (2018) with workspace and cross-compilation features. libtest harness deprecated in Rust 1.80 (2024), encouraging external tools.

SEE ALSO

cargo test(1), cargo build(1), cargo run(1), rustc(1)

Copied to clipboard