Skip to content

Make it possible to stop running #[bench] functions multiple times #20142

Closed
@nagisa

Description

@nagisa

#[bench] functions are run multiple times. Some benchmark functions might depend on some runtime initialisation which may fail. For example:

#[bench]
fn bench_this(b : &mut Bencher) {
    if let Ok(val) = WillFailOnEvenHour::new() {
        b.iter(||{ var.current_hour_is_not_even() });
    } else {
        // nothing to bench here? Maybe print an error?
        println!("Benchmarks on even hours are forbidden!");
    }
}

Will print “Benchmarks on even hours are forbidden!” a lot of times. If one opts to not report set-up failure, the benchmark function just keeps processor hot for a while without actually benchmarking anything (and reporting 0ns/iter anyway).

Would it be sensible to (one of possible solutions):

  • Not run the same #[bench] function multiple times, since iteration is done by .iter;
  • Not run the same #[bench] again if Bencher::iter is not called at least once;
  • Provide a method on Bencher to cancel all subsequent runs of the #[bench] function?

Or maybe my approach is incorrect?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions