From 1bb0938ff60e5ec7b59a41bf7f6c50d77639a0f2 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 11 Jul 2024 22:19:41 +0200 Subject: [PATCH] Switch from criterion to divan --- eyeball/Cargo.toml | 4 +- eyeball/benches/set_a_lot.rs | 77 +++++++++++++++--------------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/eyeball/Cargo.toml b/eyeball/Cargo.toml index a3f3ac0..0b609c9 100644 --- a/eyeball/Cargo.toml +++ b/eyeball/Cargo.toml @@ -22,7 +22,7 @@ tokio = { workspace = true, optional = true } tokio-util = { version = "0.7.8", optional = true } # for benchmarking -criterion = { version = "0.5.1", optional = true } +divan = { version = "0.1.14", optional = true } [dev-dependencies] futures-util.workspace = true @@ -32,7 +32,7 @@ tokio = { workspace = true, features = ["macros", "rt"] } [features] async-lock = ["dep:readlock-tokio", "dep:tokio", "dep:tokio-util"] -__bench = ["dep:criterion", "dep:tokio", "tokio?/rt-multi-thread"] +__bench = ["dep:divan", "dep:tokio", "tokio?/rt-multi-thread"] [[bench]] name = "set_a_lot" diff --git a/eyeball/benches/set_a_lot.rs b/eyeball/benches/set_a_lot.rs index e15d84d..4631381 100644 --- a/eyeball/benches/set_a_lot.rs +++ b/eyeball/benches/set_a_lot.rs @@ -1,72 +1,59 @@ #![allow(missing_docs)] -use criterion::{black_box, criterion_group, criterion_main, BatchSize, Bencher, Criterion}; +use divan::{black_box, main, Bencher}; use eyeball::Observable; use tokio::task::JoinSet; -fn baseline(b: &mut Bencher<'_>) { - let mut x = Box::new([0; 256]); - b.iter(|| { +#[divan::bench] +fn baseline(b: Bencher<'_, '_>) { + b.with_inputs(|| Box::new([0; 256])).bench_refs(|x| { for i in 1..=256 { - black_box(&x); - x = black_box(Box::new([i; 256])); + *x = black_box(Box::new([i; 256])); } }); } -fn no_subscribers(b: &mut Bencher<'_>) { - let mut ob = Observable::new(Box::new([0; 256])); - b.iter(|| { +#[divan::bench] +fn no_subscribers(b: Bencher<'_, '_>) { + b.with_inputs(|| Observable::new(Box::new([0; 256]))).bench_refs(|ob| { for i in 1..=256 { - black_box(&*ob); - Observable::set(&mut ob, black_box(Box::new([i; 256]))); + Observable::set(ob, black_box(Box::new([i; 256]))); } }); } -#[tokio::main] -async fn n_subscribers(n: usize, b: &mut Bencher<'_>) { - b.iter_batched( - || { - let ob = Observable::new(Box::new([0; 256])); - let mut join_set = JoinSet::new(); - for _ in 0..n { - let mut subscriber = Observable::subscribe(&ob); +#[divan::bench(args = [1, 2, 4, 16, 64])] +fn n_subscribers(b: Bencher<'_, '_>, n: usize) { + b.with_inputs(|| { + let tokio_rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("Failed to build tokio runtime"); + + let ob = Observable::new(Box::new([0; 256])); + let mut join_set = JoinSet::new(); + for _ in 0..n { + let mut subscriber = Observable::subscribe(&ob); + tokio_rt.block_on(async { join_set.spawn(async move { while let Some(value) = subscriber.next().await { black_box(&value); } }); - } - (ob, join_set) - }, - |(mut ob, mut join_set)| { + }); + } + (tokio_rt, ob, join_set) + }) + .bench_values(|(tokio_rt, mut ob, mut join_set)| { + tokio_rt.block_on(async move { for i in 1..=256 { Observable::set(&mut ob, black_box(Box::new([i; 256]))); } drop(ob); - tokio::task::block_in_place(|| { - let tokio_handle = tokio::runtime::Handle::current(); - tokio_handle.block_on(async move { - // wait for all tasks to finish - while join_set.join_next().await.is_some() {} - }); - }); - }, - BatchSize::LargeInput, - ); -} -fn set_a_lot(c: &mut Criterion) { - c.bench_function("baseline", baseline); - c.bench_function("no_subscribers", no_subscribers); - c.bench_function("one_subscriber", |b| n_subscribers(1, b)); - c.bench_function("two_subscribers", |b| n_subscribers(2, b)); - c.bench_function("four_subscribers", |b| n_subscribers(4, b)); - c.bench_function("sixteen_subscribers", |b| n_subscribers(16, b)); - c.bench_function("sixtyfour_subscribers", |b| n_subscribers(64, b)); + // wait for all tasks to finish + while join_set.join_next().await.is_some() {} + }); + }); } - -criterion_group!(benches, set_a_lot); -criterion_main!(benches);