|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::thread; |
| 5 | +use std::time::Duration; |
| 6 | + |
| 7 | +use divan::Bencher; |
| 8 | +use futures::future::join_all; |
| 9 | + |
| 10 | +#[divan::bench(args = [1, 10, 100], threads = false, sample_count = 1, sample_size = 1)] |
| 11 | +fn tokio_spawn(b: Bencher, work_ms: u64) { |
| 12 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 13 | + |
| 14 | + let handle = rt.handle().clone(); |
| 15 | + b.bench_local(|| { |
| 16 | + // Spawn 1000 tasks that all do 1MS of CPU-blocking work. |
| 17 | + let join_handles: Vec<_> = (0..1000) |
| 18 | + .map(|_| handle.spawn(async move { thread::sleep(Duration::from_millis(work_ms)) })) |
| 19 | + .collect(); |
| 20 | + |
| 21 | + rt.block_on(join_all(join_handles)); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +#[divan::bench(args = [1, 10, 100], threads = false, sample_count = 1, sample_size = 1)] |
| 26 | +fn tokio_spawn_blocking(b: Bencher, work_ms: u64) { |
| 27 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 28 | + |
| 29 | + let handle = rt.handle().clone(); |
| 30 | + b.bench_local(|| { |
| 31 | + let work_ms = work_ms; |
| 32 | + // Spawn 1000 tasks that all do 1MS of CPU-blocking work. |
| 33 | + let join_handles: Vec<_> = (0..1000) |
| 34 | + .map(|_| handle.spawn_blocking(move || thread::sleep(Duration::from_millis(work_ms)))) |
| 35 | + .collect(); |
| 36 | + |
| 37 | + rt.block_on(join_all(join_handles)); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +#[divan::bench(args = [1, 10, 100], threads = false, sample_count = 1, sample_size = 1)] |
| 42 | +fn tokio_unblock(b: Bencher, work_ms: u64) { |
| 43 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 44 | + |
| 45 | + let handle = rt.handle().clone(); |
| 46 | + b.bench_local(|| { |
| 47 | + // Spawn 1000 tasks that all do 1MS of CPU-blocking work. |
| 48 | + let work_ms = work_ms; |
| 49 | + let join_handles: Vec<_> = (0..1000) |
| 50 | + .map(|_| { |
| 51 | + handle.spawn(blocking::unblock(move || { |
| 52 | + thread::sleep(Duration::from_millis(work_ms.clone())) |
| 53 | + })) |
| 54 | + }) |
| 55 | + .collect(); |
| 56 | + |
| 57 | + rt.block_on(join_all(join_handles)); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +#[divan::bench(args = [1, 10, 100], threads = false, sample_size = 1, sample_count = 1)] |
| 62 | +fn smol_unblock(b: Bencher, work_ms: u64) { |
| 63 | + let exec = smol::Executor::new(); |
| 64 | + |
| 65 | + b.bench_local(|| { |
| 66 | + // Spawn 1000 tasks that all do 1MS of CPU-blocking work. |
| 67 | + let work_ms = work_ms; |
| 68 | + let mut join_handles = Vec::with_capacity(1000); |
| 69 | + exec.spawn_many( |
| 70 | + (0..1000).map(|_| { |
| 71 | + blocking::unblock(move || thread::sleep(Duration::from_millis(work_ms.clone()))) |
| 72 | + }), |
| 73 | + &mut join_handles, |
| 74 | + ); |
| 75 | + // run the executor to unblock shit |
| 76 | + // We need to do this all on the current thread, so this is bad |
| 77 | + smol::block_on(exec.run(async move { |
| 78 | + for handle in join_handles { |
| 79 | + handle.await; |
| 80 | + } |
| 81 | + })); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +fn main() { |
| 86 | + divan::main(); |
| 87 | +} |
0 commit comments