Skip to content

Commit

Permalink
feat: update benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
wathenjiang committed Sep 21, 2023
1 parent 5422895 commit b90101a
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions benches/spawn_concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ fn spawn_tasks(c: &mut Criterion) {
});
}

fn spawn_tasks_parallel(c: &mut Criterion) {
let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
c.bench_function("spawn_tasks_parallel", move |b| {
fn spawn_tasks_parallel<const S: usize>(c: &mut Criterion) {
let runtime = tokio::runtime::Builder::new_multi_thread()
.spawn_concurrency_level(S)
.build()
.unwrap();
c.bench_function(format!("spawn_tasks_parallel {}", S).as_str(), move |b| {
b.iter_custom(|iters| {
let start = Instant::now();
runtime.block_on(async {
Expand Down Expand Up @@ -83,11 +86,72 @@ async fn job(iters: usize, procs: usize) {
}
}

fn shutdown_tasks_parallel<const S: usize>(c: &mut Criterion) {
c.bench_function(
format!("showdown_tasks_parallel {}", S).as_str(),
move |b| {
b.iter_custom(|iters| {
let runtime = tokio::runtime::Builder::new_multi_thread()
.spawn_concurrency_level(S)
.enable_time()
.build()
.unwrap();
runtime.block_on(async {
black_box(job_shutdown(iters as usize, num_cpus::get_physical()).await);
});
let start = Instant::now();
drop(runtime);
start.elapsed()
})
},
);
}

async fn job_shutdown(iters: usize, procs: usize) {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
for _ in 0..procs {
let tx = tx.clone();
tokio::spawn(async move {
for _ in 0..iters / procs {
let tx = tx.clone();
tokio::spawn(async move {
let val = 1 + 1;
tx.send(()).unwrap();
tokio::time::sleep(tokio::time::Duration::from_secs(1000)).await; // it will never return
black_box(val)
});
}
});
}
for _ in 0..(iters / procs) * procs {
rx.recv().await;
}
}

criterion_group!(
benches,
spawn_tasks_current_thread,
spawn_tasks_current_thread_parallel,
spawn_tasks,
spawn_tasks_parallel
spawn_tasks_parallel<1>,
spawn_tasks_parallel<4>,
spawn_tasks_parallel<8>,
spawn_tasks_parallel<16>,
spawn_tasks_parallel<32>,
spawn_tasks_parallel<64>,
spawn_tasks_parallel<128>,
spawn_tasks_parallel<256>,
spawn_tasks_parallel<512>,
spawn_tasks_parallel<1024>,
shutdown_tasks_parallel<1>,
shutdown_tasks_parallel<4>,
shutdown_tasks_parallel<8>,
shutdown_tasks_parallel<16>,
shutdown_tasks_parallel<32>,
shutdown_tasks_parallel<64>,
shutdown_tasks_parallel<128>,
shutdown_tasks_parallel<256>,
shutdown_tasks_parallel<512>,
shutdown_tasks_parallel<1024>,
);
criterion_main!(benches);

0 comments on commit b90101a

Please sign in to comment.