-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |