bench: update spawn benchmarks (#3927)

This commit is contained in:
Alice Ryhl
2021-07-07 10:52:53 +02:00
committed by GitHub
parent ae233c1e9f
commit 51fad066e2
+57 -37
View File
@@ -2,63 +2,83 @@
//! This essentially measure the time to enqueue a task in the local and remote //! This essentially measure the time to enqueue a task in the local and remote
//! case. //! case.
#[macro_use]
extern crate bencher;
use bencher::{black_box, Bencher}; use bencher::{black_box, Bencher};
async fn work() -> usize { async fn work() -> usize {
let val = 1 + 1; let val = 1 + 1;
tokio::task::yield_now().await;
black_box(val) black_box(val)
} }
fn basic_scheduler_local_spawn(bench: &mut Bencher) { fn basic_scheduler_spawn(bench: &mut Bencher) {
let runtime = tokio::runtime::Builder::new_current_thread() let runtime = tokio::runtime::Builder::new_current_thread()
.build() .build()
.unwrap(); .unwrap();
runtime.block_on(async {
bench.iter(|| {
let h = tokio::spawn(work());
black_box(h);
})
});
}
fn threaded_scheduler_local_spawn(bench: &mut Bencher) {
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
runtime.block_on(async {
bench.iter(|| {
let h = tokio::spawn(work());
black_box(h);
})
});
}
fn basic_scheduler_remote_spawn(bench: &mut Bencher) {
let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
bench.iter(|| { bench.iter(|| {
let h = runtime.spawn(work()); runtime.block_on(async {
black_box(h); let h = tokio::spawn(work());
assert_eq!(h.await.unwrap(), 2);
});
}); });
} }
fn threaded_scheduler_remote_spawn(bench: &mut Bencher) { fn basic_scheduler_spawn10(bench: &mut Bencher) {
let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap(); let runtime = tokio::runtime::Builder::new_current_thread()
.build()
.unwrap();
bench.iter(|| { bench.iter(|| {
let h = runtime.spawn(work()); runtime.block_on(async {
black_box(h); let mut handles = Vec::with_capacity(10);
for _ in 0..10 {
handles.push(tokio::spawn(work()));
}
for handle in handles {
assert_eq!(handle.await.unwrap(), 2);
}
});
});
}
fn threaded_scheduler_spawn(bench: &mut Bencher) {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.build()
.unwrap();
bench.iter(|| {
runtime.block_on(async {
let h = tokio::spawn(work());
assert_eq!(h.await.unwrap(), 2);
});
});
}
fn threaded_scheduler_spawn10(bench: &mut Bencher) {
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(1)
.build()
.unwrap();
bench.iter(|| {
runtime.block_on(async {
let mut handles = Vec::with_capacity(10);
for _ in 0..10 {
handles.push(tokio::spawn(work()));
}
for handle in handles {
assert_eq!(handle.await.unwrap(), 2);
}
});
}); });
} }
bencher::benchmark_group!( bencher::benchmark_group!(
spawn, spawn,
basic_scheduler_local_spawn, basic_scheduler_spawn,
threaded_scheduler_local_spawn, basic_scheduler_spawn10,
basic_scheduler_remote_spawn, threaded_scheduler_spawn,
threaded_scheduler_remote_spawn threaded_scheduler_spawn10,
); );
bencher::benchmark_main!(spawn); bencher::benchmark_main!(spawn);