nvzqz / divan

Fast and simple benchmarking for Rust projects
https://nikolaivazquez.com/blog/divan/
Apache License 2.0
924 stars 26 forks source link

RFC: Introduce `BenchmarkContext` argument for closure passed to benchmark function #33

Open vlovich opened 10 months ago

vlovich commented 10 months ago

For the bench function on divan::Bencher, I'd like to introduce a BenchmarkContext (naming open to bikeshedding). The motivating use-case is to expose a function called something like inject_simulated_delay which can be used to add simulated overhead to the benchmark without needing to actually do it. An example of this is for example benchmarking a caching algorithm that would normally do expensive I/O on a miss. In practice, it's more convenient in a microbenchmark to simulate performance by just simulating how long I think the I/O would take.

You can hang other state off the context too (e.g. what sample/iteration the current iteration is on in case that matters for some applications).

Not sure if there's any better alternative to make this work since bench functions consume self which is intrinsic to the design.

Happy to actually implement it but was curious about thoughts first.