-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reduce the lock contention in task spawn.
The origin version: spawn_tasks_current_thread time: [654.87 ns 664.26 ns 672.13 ns] change: [-4.5814% -1.2511% +2.1604%] (p = 0.48 > 0.05) No change in performance detected. spawn_tasks_current_thread_parallel time: [537.55 ns 540.00 ns 542.20 ns] change: [-1.0603% -0.2591% +0.6283%] (p = 0.58 > 0.05) No change in performance detected. Found 1 outliers among 100 measurements (1.00%) 1 (1.00%) high severe spawn_tasks time: [1.0447 µs 1.0533 µs 1.0610 µs] change: [+0.9379% +2.2768% +3.7191%] (p = 0.00 < 0.05) Change within noise threshold. Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) low severe 2 (2.00%) low mild spawn_tasks_parallel time: [992.38 ns 1.0002 µs 1.0073 µs] change: [+0.1973% +1.4194% +2.5819%] (p = 0.02 < 0.05) Change within noise threshold. Found 6 outliers among 100 measurements (6.00%) 1 (1.00%) low severe 2 (2.00%) low mild 1 (1.00%) high mild 2 (2.00%) high severe This version: spawn_tasks_current_thread time: [705.92 ns 724.31 ns 739.78 ns] spawn_tasks_current_thread_parallel time: [529.33 ns 531.00 ns 532.61 ns] Found 2 outliers among 100 measurements (2.00%) 1 (1.00%) high mild 1 (1.00%) high severe spawn_tasks time: [881.56 ns 892.21 ns 902.10 ns] Found 3 outliers among 100 measurements (3.00%) 2 (2.00%) low mild 1 (1.00%) high mild spawn_tasks_parallel time: [815.00 ns 819.87 ns 824.60 ns] Found 4 outliers among 100 measurements (4.00%) 2 (2.00%) high mild 2 (2.00%) high severe
- Loading branch information
1 parent
a6be73e
commit 895958f
Showing
8 changed files
with
187 additions
and
108 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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::time::Instant; | ||
|
||
use criterion::*; | ||
|
||
fn spawn_tasks_current_thread(c: &mut Criterion) { | ||
let runtime = tokio::runtime::Builder::new_current_thread() | ||
.build() | ||
.unwrap(); | ||
|
||
c.bench_function("spawn_tasks_current_thread", 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 spawn_tasks_current_thread_parallel(c: &mut Criterion) { | ||
let runtime = tokio::runtime::Builder::new_current_thread() | ||
.build() | ||
.unwrap(); | ||
|
||
c.bench_function("spawn_tasks_current_thread_parallel", move |b| { | ||
b.iter_custom(|iters| { | ||
let start = Instant::now(); | ||
runtime.block_on(async { | ||
black_box(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(); | ||
|
||
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 spawn_tasks_parallel(c: &mut Criterion) { | ||
let runtime = tokio::runtime::Builder::new_multi_thread().build().unwrap(); | ||
c.bench_function("spawn_tasks_parallel", move |b| { | ||
b.iter_custom(|iters| { | ||
let start = Instant::now(); | ||
runtime.block_on(async { | ||
black_box(job(iters as usize, num_cpus::get_physical()).await); | ||
}); | ||
start.elapsed() | ||
}) | ||
}); | ||
} | ||
|
||
async fn job(iters: usize, procs: usize) { | ||
for _ in 0..procs { | ||
let mut threads_handles = Vec::with_capacity(procs); | ||
threads_handles.push(tokio::spawn(async move { | ||
let mut thread_handles = Vec::with_capacity(iters / procs); | ||
for _ in 0..iters / procs { | ||
thread_handles.push(tokio::spawn(async { | ||
let val = 1 + 1; | ||
tokio::task::yield_now().await; | ||
black_box(val) | ||
})); | ||
} | ||
for handle in thread_handles { | ||
handle.await.unwrap(); | ||
} | ||
})); | ||
for handle in threads_handles { | ||
handle.await.unwrap(); | ||
} | ||
} | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
spawn_tasks_current_thread, | ||
spawn_tasks_current_thread_parallel, | ||
spawn_tasks, | ||
spawn_tasks_parallel | ||
); | ||
criterion_main!(benches); |
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
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
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
Oops, something went wrong.