bench: Add benchmarks for lower thread counts (#38)

This commit is contained in:
John Nunley 2023-03-10 19:18:48 -08:00 committed by GitHub
parent b8885f9578
commit 6aba704efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 86 additions and 81 deletions

View File

@ -10,10 +10,12 @@ const LIGHT_TASKS: usize = 25_000;
static EX: Executor<'_> = Executor::new(); static EX: Executor<'_> = Executor::new();
fn run(f: impl FnOnce()) { fn run(f: impl FnOnce(), multithread: bool) {
let limit = if multithread { num_cpus::get() } else { 1 };
let (s, r) = async_channel::bounded::<()>(1); let (s, r) = async_channel::bounded::<()>(1);
easy_parallel::Parallel::new() easy_parallel::Parallel::new()
.each(0..num_cpus::get(), |_| future::block_on(EX.run(r.recv()))) .each(0..limit, |_| future::block_on(EX.run(r.recv())))
.finish(move || { .finish(move || {
let _s = s; let _s = s;
f() f()
@ -30,19 +32,24 @@ fn create(c: &mut Criterion) {
}); });
} }
fn spawn_one(c: &mut Criterion) { fn running_benches(c: &mut Criterion) {
c.bench_function("executor::spawn_one", |b| { for (group_name, multithread) in [("single_thread", false), ("multi_thread", true)].iter() {
run(|| { let mut group = c.benchmark_group(group_name.to_string());
group.bench_function("executor::spawn_one", |b| {
run(
|| {
b.iter(|| { b.iter(|| {
future::block_on(async { EX.spawn(async {}).await }); future::block_on(async { EX.spawn(async {}).await });
}); });
},
*multithread,
);
}); });
});
}
fn spawn_many(c: &mut Criterion) { group.bench_function("executor::spawn_many_local", |b| {
c.bench_function("executor::spawn_many_local", |b| { run(
run(|| { || {
b.iter(move || { b.iter(move || {
future::block_on(async { future::block_on(async {
let mut tasks = Vec::new(); let mut tasks = Vec::new();
@ -54,12 +61,12 @@ fn spawn_many(c: &mut Criterion) {
} }
}); });
}); });
},
*multithread,
);
}); });
});
}
fn spawn_recursively(c: &mut Criterion) { group.bench_function("executor::spawn_recursively", |b| {
c.bench_function("executor::spawn_recursively", |b| {
#[allow(clippy::manual_async_fn)] #[allow(clippy::manual_async_fn)]
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static { fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
async move { async move {
@ -73,7 +80,8 @@ fn spawn_recursively(c: &mut Criterion) {
} }
} }
run(|| { run(
|| {
b.iter(move || { b.iter(move || {
future::block_on(async { future::block_on(async {
let mut tasks = Vec::new(); let mut tasks = Vec::new();
@ -85,13 +93,14 @@ fn spawn_recursively(c: &mut Criterion) {
} }
}); });
}); });
},
*multithread,
);
}); });
});
}
fn yield_now(c: &mut Criterion) { group.bench_function("executor::yield_now", |b| {
c.bench_function("executor::yield_now", |b| { run(
run(|| { || {
b.iter(move || { b.iter(move || {
future::block_on(async { future::block_on(async {
let mut tasks = Vec::new(); let mut tasks = Vec::new();
@ -107,17 +116,13 @@ fn yield_now(c: &mut Criterion) {
} }
}); });
}); });
}); },
*multithread,
);
}); });
} }
}
criterion_group!( criterion_group!(benches, create, running_benches);
benches,
create,
spawn_one,
spawn_many,
spawn_recursively,
yield_now,
);
criterion_main!(benches); criterion_main!(benches);