use std::hint::black_box;
use std::io;
use std::time::Duration;
use criterion::measurement::WallTime;
use criterion::{BenchmarkGroup, Criterion, Throughput};
use super::sample::Sample;
pub(crate) fn quick() -> Criterion {
Criterion::default()
.sample_size(20)
.warm_up_time(Duration::from_millis(200))
.measurement_time(Duration::from_millis(500))
}
pub(crate) fn group<'a>(c: &'a mut Criterion, name: &str) -> BenchmarkGroup<'a, WallTime> {
c.benchmark_group(name)
}
pub(crate) fn per_sample<R>(
c: &mut Criterion,
name: &str,
samples: &[Sample],
bytes: impl Fn(&Sample) -> usize,
mut f: impl FnMut(&Sample) -> R,
) {
let mut g = group(c, name);
for s in samples {
g.throughput(Throughput::Bytes(bytes(s) as u64));
g.bench_function(&s.name, |b| b.iter(|| black_box(f(black_box(s)))));
}
g.finish();
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Discard;
impl io::Write for Discard {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Ok(black_box(buf).len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}