Skip to content

Commit

Permalink
feat: add benchmarks for spawn_concurrency_level
Browse files Browse the repository at this point in the history
  • Loading branch information
wathenjiang committed Sep 21, 2023
1 parent b90101a commit 68af71a
Showing 1 changed file with 77 additions and 57 deletions.
134 changes: 77 additions & 57 deletions benches/spawn_concurrent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Instant;

use criterion::*;
use criterion::{measurement::WallTime, *};

fn spawn_tasks_current_thread(c: &mut Criterion) {
let runtime = tokio::runtime::Builder::new_current_thread()
Expand All @@ -11,7 +11,7 @@ fn spawn_tasks_current_thread(c: &mut Criterion) {
b.iter_custom(|iters| {
let start = Instant::now();
runtime.block_on(async {
black_box(job(iters as usize, 1).await);
black_box(spawn_job(iters as usize, 1).await);
});
start.elapsed()
})
Expand All @@ -27,44 +27,70 @@ fn spawn_tasks_current_thread_parallel(c: &mut Criterion) {
b.iter_custom(|iters| {
let start = Instant::now();
runtime.block_on(async {
black_box(job(iters as usize, num_cpus::get_physical() * 2).await);
black_box(spawn_job(iters as usize, num_cpus::get_physical() * 2).await);
});
start.elapsed()
})
});
}

fn spawn_tasks(c: &mut Criterion) {
let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap();
fn bench_create_runtime_multi_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("create_runtime_multi_thread");
create_multi_thread_runtime::<1>(&mut group);
create_multi_thread_runtime::<8>(&mut group);
create_multi_thread_runtime::<32>(&mut group);
create_multi_thread_runtime::<64>(&mut group);
create_multi_thread_runtime::<128>(&mut group);
create_multi_thread_runtime::<256>(&mut group);
create_multi_thread_runtime::<512>(&mut group);
create_multi_thread_runtime::<1024>(&mut group);
create_multi_thread_runtime::<2048>(&mut group);
create_multi_thread_runtime::<4096>(&mut group);
}

c.bench_function("spawn_tasks", move |b| {
b.iter_custom(|iters| {
let start = Instant::now();
runtime.block_on(async {
black_box(job(iters as usize, 1).await);
});
start.elapsed()
fn create_multi_thread_runtime<const S: usize>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_function(format!("{:04}", S), |b| {
b.iter(|| {
let runtime = tokio::runtime::Builder::new_multi_thread()
.spawn_concurrency_level(black_box(S))
.build()
.unwrap();
drop(runtime);
})
});
}

fn spawn_tasks_parallel<const S: usize>(c: &mut Criterion) {
fn bench_parallel_spawn_multi_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("spawn_parallel_multi_thread");
spawn_tasks_parallel_multi_thread::<1>(&mut group);
spawn_tasks_parallel_multi_thread::<8>(&mut group);
spawn_tasks_parallel_multi_thread::<32>(&mut group);
spawn_tasks_parallel_multi_thread::<64>(&mut group);
spawn_tasks_parallel_multi_thread::<128>(&mut group);
spawn_tasks_parallel_multi_thread::<256>(&mut group);
spawn_tasks_parallel_multi_thread::<512>(&mut group);
spawn_tasks_parallel_multi_thread::<1024>(&mut group);
spawn_tasks_parallel_multi_thread::<2048>(&mut group);
spawn_tasks_parallel_multi_thread::<4096>(&mut group);
}

fn spawn_tasks_parallel_multi_thread<const S: usize>(g: &mut BenchmarkGroup<WallTime>) {
let runtime = tokio::runtime::Builder::new_multi_thread()
.spawn_concurrency_level(S)
.spawn_concurrency_level(black_box(S))
.build()
.unwrap();
c.bench_function(format!("spawn_tasks_parallel {}", S).as_str(), move |b| {
g.bench_function(format!("{:04}", S), |b| {
b.iter_custom(|iters| {
let start = Instant::now();
runtime.block_on(async {
black_box(job(iters as usize, num_cpus::get_physical()).await);
black_box(spawn_job(iters as usize, num_cpus::get_physical()).await);
});
start.elapsed()
})
});
}

async fn job(iters: usize, procs: usize) {
async fn spawn_job(iters: usize, procs: usize) {
for _ in 0..procs {
let mut threads_handles = Vec::with_capacity(procs);
threads_handles.push(tokio::spawn(async move {
Expand All @@ -86,25 +112,37 @@ 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()
})
},
);
fn bench_shutdown_parallel_multi_thread(c: &mut Criterion) {
let mut group = c.benchmark_group("shutdown_parallel_multi_thread");
shutdown_tasks_parallel::<1>(&mut group);
shutdown_tasks_parallel::<8>(&mut group);
shutdown_tasks_parallel::<32>(&mut group);
shutdown_tasks_parallel::<64>(&mut group);
shutdown_tasks_parallel::<128>(&mut group);
shutdown_tasks_parallel::<256>(&mut group);
shutdown_tasks_parallel::<512>(&mut group);
shutdown_tasks_parallel::<1024>(&mut group);
shutdown_tasks_parallel::<2048>(&mut group);
shutdown_tasks_parallel::<4096>(&mut group);
group.finish();
}

fn shutdown_tasks_parallel<const S: usize>(g: &mut BenchmarkGroup<WallTime>) {
g.bench_function(format!("{:04}", S), |b| {
b.iter_custom(|iters| {
let runtime = tokio::runtime::Builder::new_multi_thread()
.spawn_concurrency_level(black_box(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) {
Expand Down Expand Up @@ -132,26 +170,8 @@ criterion_group!(
benches,
spawn_tasks_current_thread,
spawn_tasks_current_thread_parallel,
spawn_tasks,
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>,
bench_create_runtime_multi_thread,
bench_parallel_spawn_multi_thread,
bench_shutdown_parallel_multi_thread,
);
criterion_main!(benches);

0 comments on commit 68af71a

Please sign in to comment.