diff --git a/histogram/benches/histogram.rs b/histogram/benches/histogram.rs index 2f614ef..a30a80d 100644 --- a/histogram/benches/histogram.rs +++ b/histogram/benches/histogram.rs @@ -1,29 +1,34 @@ use core::time::Duration; use criterion::{criterion_group, criterion_main, Criterion, Throughput}; -// To reduce duplication, we use this macro. It only works because the API for -// all the histogram types is roughly the same for some operations. -macro_rules! benchmark { - ($name:tt, $histogram:ident, $c:ident) => { - let mut group = $c.benchmark_group($name); - group.throughput(Throughput::Elements(1)); - group.bench_function("increment/1", |b| b.iter(|| $histogram.increment(1))); - group.bench_function("increment/max", |b| { - b.iter(|| $histogram.increment(u64::MAX)) - }); - - group.finish(); - } -} - fn histogram(c: &mut Criterion) { let mut histogram = histogram::Histogram::new(0, 7, 64).unwrap(); - benchmark!("histogram", histogram, c); + + let mut group = c.benchmark_group("histogram"); + group.throughput(Throughput::Elements(1)); + group.bench_function("increment/1", |b| b.iter(|| histogram.increment(1))); + + histogram.clear(); + group.bench_function("increment/max", |b| { + b.iter(|| histogram.increment(u64::MAX)) + }); + + group.finish(); } fn histogram_atomic(c: &mut Criterion) { let histogram = histogram::atomic::Histogram::new(0, 7, 64).unwrap(); - benchmark!("histogram::atomic", histogram, c); + + let mut group = c.benchmark_group("histogram::atomic"); + group.throughput(Throughput::Elements(1)); + group.bench_function("increment/1", |b| b.iter(|| histogram.increment(1))); + + histogram.clear(); + group.bench_function("increment/max", |b| { + b.iter(|| histogram.increment(u64::MAX)) + }); + + group.finish(); } fn sliding_window(c: &mut Criterion) { @@ -31,13 +36,27 @@ fn sliding_window(c: &mut Criterion) { let mut histogram = histogram::sliding_window::Histogram::new(0, 7, 64, Duration::from_millis(1), 100).unwrap(); - benchmark!("histogram::sliding_window/milliseconds", histogram, c); + + let mut group = c.benchmark_group("histogram::sliding_window/milliseconds"); + group.throughput(Throughput::Elements(1)); + group.bench_function("increment/1", |b| b.iter(|| histogram.increment(1))); + group.bench_function("increment/max", |b| { + b.iter(|| histogram.increment(u64::MAX)) + }); + group.finish(); // second resolution let mut histogram = histogram::sliding_window::Histogram::new(0, 7, 64, Duration::from_secs(1), 100).unwrap(); - benchmark!("histogram::sliding_window/seconds", histogram, c); + + let mut group = c.benchmark_group("histogram::sliding_window/seconds"); + group.throughput(Throughput::Elements(1)); + group.bench_function("increment/1", |b| b.iter(|| histogram.increment(1))); + group.bench_function("increment/max", |b| { + b.iter(|| histogram.increment(u64::MAX)) + }); + group.finish(); } fn sliding_window_atomic(c: &mut Criterion) { @@ -46,14 +65,28 @@ fn sliding_window_atomic(c: &mut Criterion) { let histogram = histogram::sliding_window::atomic::Histogram::new(0, 7, 64, Duration::from_millis(1), 100) .unwrap(); - benchmark!("histogram::sliding_window::atomic/milliseconds", histogram, c); + + let mut group = c.benchmark_group("histogram::sliding_window::atomic/milliseconds"); + group.throughput(Throughput::Elements(1)); + group.bench_function("increment/1", |b| b.iter(|| histogram.increment(1))); + group.bench_function("increment/max", |b| { + b.iter(|| histogram.increment(u64::MAX)) + }); + group.finish(); // second resolution let histogram = histogram::sliding_window::atomic::Histogram::new(0, 7, 64, Duration::from_secs(1), 100) .unwrap(); - benchmark!("histogram::sliding_window::atomic/seconds", histogram, c); + + let mut group = c.benchmark_group("histogram::sliding_window::atomic/seconds"); + group.throughput(Throughput::Elements(1)); + group.bench_function("increment/1", |b| b.iter(|| histogram.increment(1))); + group.bench_function("increment/max", |b| { + b.iter(|| histogram.increment(u64::MAX)) + }); + group.finish(); } criterion_group!(