Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip setup for filtered out benchmarks #777

Open
xzfc opened this issue May 2, 2024 · 0 comments
Open

Skip setup for filtered out benchmarks #777

xzfc opened this issue May 2, 2024 · 0 comments

Comments

@xzfc
Copy link

xzfc commented May 2, 2024

Currently, it is possible to specify which benchmark using a filter (i.e. cargo bench -- <filter>). This selects which of bench_function to run.

However, I see no way to prevent setup code from running when the corresponding benchmark is not selected. Suppose I have a set of benchmarks with a very long setup time, e.g. indexing a large dataset that could take a minute or two. I'd like to avoid running the setup code for filtered out benchmarks.

Example

use criterion::{criterion_group, criterion_main, Criterion};

struct Benchee;

impl Benchee {
    pub fn very_long_setup(name: &str) -> Self {
        eprint!("init {}...", name);
        std::thread::sleep(std::time::Duration::from_secs(30));
        eprintln!(" done");
        Benchee
    }
    pub fn run(&self) {}
}

pub fn criterion_benchmark(c: &mut Criterion) {
    let one = Benchee::very_long_setup("one");
    c.bench_function("one", |b| b.iter(|| one.run()));

    let two = Benchee::very_long_setup("two");
    c.bench_function("two", |b| b.iter(|| two.run()));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
$ cargo bench one

init one... done
one                     time:   [0.0000 ps 0.0000 ps 0.0000 ps]                         
                        change: [-51.749% -6.0500% +73.757%] (p = 0.86 > 0.05)
                        No change in performance detected.
Found 12 outliers among 100 measurements (12.00%)
  4 (4.00%) high mild
  8 (8.00%) high severe

init two... done           ← ⚠️ I'd like to avoid this ⚠️
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant