Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute `cargo bench` on a per-file basis?

I'm working through Project Euler problems, and I have each problem in a different file. As I try different approaches to each problem, I create a series of benchmarks for each problem in its respective file.

My directory structure looks like:

euler/
    Cargo.lock
    Cargo.toml
    README.md
    src/
        main.rs
        p001.rs
        p002.rs
        ...etc.

The contents of main.rs are:

#![feature(test)]

extern crate primes;
extern crate test;

mod p001;
mod p002;
// ... etc

fn main() {}

When I run cargo bench, it runs the benchmarks for every single file in my project:

$ cargo bench
    Finished release [optimized] target(s) in 0.02 secs
     Running target/release/deps/euler_rust-5be87dff38a04da2

running 11 tests
test p001::bench_fold_sum                     ... bench:      12,955 ns/iter (+/- 13,501)
test p001::bench_for_sum                      ... bench:      11,385 ns/iter (+/- 7,383)
test p002::bench_for_sum                      ... bench:         270 ns/iter (+/- 90)
test p002::bench_takewhile_filter_mutable_sum ... bench:         322 ns/iter (+/- 333)
test p002::bench_takewhile_filter_sum         ... bench:         512 ns/iter (+/- 379)
test p002::bench_takewhile_sum                ... bench:         273 ns/iter (+/- 117)
test p002::bench_while_sum                    ... bench:         286 ns/iter (+/- 240)

I'm interested in only running the benchmarks contained in one file, but specifying the filename after cargo bench yields the following:

$ cargo bench src/p002.rs
    Finished release [optimized] target(s) in 0.02 secs
     Running target/release/deps/euler_rust-5be87dff38a04da2

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 11 filtered out

How do you run the benchmarks in a single file?

like image 427
piercebot Avatar asked Oct 17 '25 18:10

piercebot


1 Answers

There is no direct "run the tests contained in this file on disk" option. The help for cargo bench says:

$ cargo bench --help
Execute all benchmarks of a local package

Usage:
    cargo bench [options] [BENCHNAME] [--] [<args>...]

Options:
    BENCHNAME                    If specified, only run benches containing this string in their names

Additionally, files correspond to Rust modules, and modules are part of the test name.

You can combine these two things together and use cargo bench my_file_slash_module_name to restrict the tests that are run. In your specific case:

cargo bench p002

If you happen to have other tests or modules that contain this same substring, you can sometimes get away by adding the colons:

cargo bench p002::
like image 104
Shepmaster Avatar answered Oct 21 '25 00:10

Shepmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!