pub struct Benchmark { /* private fields */ }Expand description
A single benchmark run.
Collects per-iteration duration samples. Call Benchmark::finish
to produce a BenchmarkResult.
§Example
use dev_bench::Benchmark;
let mut b = Benchmark::new("noop");
for _ in 0..10 {
b.iter(|| std::hint::black_box(42));
}
let r = b.finish();
assert_eq!(r.samples.len(), 10);Implementations§
Source§impl Benchmark
impl Benchmark
Sourcepub fn iter<F, R>(&mut self, f: F) -> Rwhere
F: FnOnce() -> R,
pub fn iter<F, R>(&mut self, f: F) -> Rwhere
F: FnOnce() -> R,
Run one iteration of the benchmark, capturing the duration.
Each call records exactly one sample.
§Example
use dev_bench::Benchmark;
let mut b = Benchmark::new("noop");
b.iter(|| std::hint::black_box(1 + 1));
let r = b.finish();
assert_eq!(r.samples.len(), 1);Sourcepub fn iter_with_count<F>(&mut self, n: u64, f: F)where
F: FnMut(),
pub fn iter_with_count<F>(&mut self, n: u64, f: F)where
F: FnMut(),
Run a closure n times and record ONE sample for the entire batch.
Use for sub-microsecond operations where per-iteration timing
would be dominated by Instant::now() overhead. The reported
per-iteration mean is batch_duration / n.
§Example
use dev_bench::Benchmark;
let mut b = Benchmark::new("hot");
b.iter_with_count(1000, || {
std::hint::black_box(40 + 2);
});
let r = b.finish();
assert_eq!(r.samples.len(), 1);
assert_eq!(r.iterations_recorded, 1000);Sourcepub fn run_for<F>(&mut self, budget: Duration, f: F)where
F: FnMut(),
pub fn run_for<F>(&mut self, budget: Duration, f: F)where
F: FnMut(),
Run a closure repeatedly for at most budget wall-clock time,
recording one sample per iteration.
Stops as soon as the elapsed time crosses budget. The
closure may run slightly past the budget (the in-flight
iteration completes); the recorded sample count reflects what
was actually executed.
Useful when you want a benchmark to run “for N seconds” rather than “for N iterations” — the per-iter cost is unknown and you just want a bounded run.
§Example
use dev_bench::Benchmark;
use std::time::Duration;
let mut b = Benchmark::new("hot");
b.run_for(Duration::from_millis(20), || {
std::hint::black_box(1 + 1);
});
let r = b.finish();
// At least one sample was collected.
assert!(!r.samples.is_empty());Sourcepub fn finish(self) -> BenchmarkResult
pub fn finish(self) -> BenchmarkResult
Finalize the benchmark and produce a BenchmarkResult.