Skip to content

Commit

Permalink
Switch from criterion to divan
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Jul 11, 2024
1 parent 9517fa4 commit 1bb0938
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
4 changes: 2 additions & 2 deletions eyeball/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
77 changes: 32 additions & 45 deletions eyeball/benches/set_a_lot.rs
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 1bb0938

Please sign in to comment.