cargo bench: Usage, Options & Common CI Errors
Compile and run benchmark targets.
cargo bench builds your crate with optimizations and runs benchmark targets. The built-in #[bench] harness is unstable, so most projects benchmark with Criterion on stable Rust.
What it does
Compiles in the bench profile (optimized) and executes registered benchmarks. Arguments after -- pass to the bench harness, mirroring cargo test.
Common usage
Terminal
cargo bench # run all benchmarks
cargo bench parse # filter by name
cargo bench --no-run # build benches only
cargo bench -- --save-baseline ci # Criterion baselineCommon CI error: bench attribute needs nightly
Using #[bench] / test::Bencher on stable fails with "error[E0658]: use of unstable library feature test." Fix: either run on the nightly toolchain, or switch to Criterion (a harness = false bench target in Cargo.toml) which runs on stable.
Options
| Flag | Effect |
|---|---|
| --no-run | Compile benches without running |
| --bench <name> | Run a specific bench target |
| -- <args> | Pass args to the harness |
Related guides
cargo test: Usage, Options & Common CI ErrorsHow cargo test builds and runs unit, integration, and doc tests, the flags for filtering and CI, and the comm…
cargo build: Usage, Options & Common CI ErrorsWhat cargo build does, the flags you use most in CI (--release, --locked, --target), and how to fix the depen…
Rust "cargo test failed" - Find the Failing Test in CIFix a failing "cargo test" in CI -- a real assertion failure, a panic, or a flaky test. Read the "test result…