use super::*;
impl BenchMetric {
#[must_use]
pub fn from_elapsed(name: &str, iterations: usize, elapsed: Duration, detail: String) -> Self {
let iters: usize = iterations.max(1);
let total_nanos: f64 = elapsed.as_nanos().max(1) as f64;
let ns_per_op: f64 = total_nanos / (iters as f64);
let ops_per_sec: f64 = 1_000_000_000.0 / ns_per_op.max(1.0);
Self::new(
name.to_string(),
iters,
elapsed.as_micros() as u64,
ns_per_op,
ops_per_sec,
detail,
)
}
#[must_use]
pub fn with_allocations(mut self, snapshot: AllocationSnapshot) -> Self {
let Self {
iterations,
alloc_count,
bytes_allocated,
allocs_per_op,
bytes_per_op,
..
} = &mut self;
let denom: f64 = (*iterations).max(1) as f64;
*alloc_count = snapshot.get_alloc_count();
*bytes_allocated = snapshot.get_bytes_allocated();
*allocs_per_op = (snapshot.get_alloc_count() as f64) / denom;
*bytes_per_op = (snapshot.get_bytes_allocated() as f64) / denom;
self
}
}