Skip to content

Commit

Permalink
Revert "use a macro to reduce duplication"
Browse files Browse the repository at this point in the history
This reverts commit 35d2fa2.
  • Loading branch information
brayniac committed Sep 1, 2023
1 parent 09614ad commit 26d3936
Showing 1 changed file with 54 additions and 21 deletions.
75 changes: 54 additions & 21 deletions histogram/benches/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,62 @@
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) {
// millisecond resolution

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) {
Expand All @@ -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!(
Expand Down

0 comments on commit 26d3936

Please sign in to comment.