Skip to content

Commit 248eee6

Browse files
committed
bench spawn, spawn_blocking, unblock
Signed-off-by: Andrew Duffy <[email protected]>
1 parent 7d786bd commit 248eee6

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ async-trait = "0.1.89"
8181
bindgen = "0.72.0"
8282
bit-vec = "0.8.0"
8383
bitvec = "1.0.1"
84+
blocking = "1"
8485
bytes = "1.10"
8586
bzip2 = "0.6.0"
8687
cbindgen = "0.29.0"

vortex-io/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async-compat = { workspace = true }
2121
async-fs = { workspace = true }
2222
async-stream = { workspace = true }
2323
async-trait = { workspace = true }
24+
blocking = { workspace = true }
2425
bytes = { workspace = true }
2526
cfg-if = { workspace = true }
2627
futures = { workspace = true, features = ["std", "executor"] }
@@ -50,6 +51,7 @@ wasm-bindgen-futures = { workspace = true }
5051

5152
[dev-dependencies]
5253
anyhow = { workspace = true }
54+
divan = { workspace = true }
5355
itertools = { workspace = true }
5456
rstest = { workspace = true }
5557
tempfile = { workspace = true }
@@ -62,3 +64,7 @@ tokio = ["tokio/fs", "tokio/rt-multi-thread"]
6264

6365
[lints]
6466
workspace = true
67+
68+
[[bench]]
69+
name = "spawn"
70+
harness = false

vortex-io/benches/spawn.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)