forked from smol-rs/async-executor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.rs
132 lines (118 loc) · 3.99 KB
/
executor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::thread::available_parallelism;
use async_executor::Executor;
use criterion::{criterion_group, criterion_main, Criterion};
use futures_lite::{future, prelude::*};
const TASKS: usize = 300;
const STEPS: usize = 300;
const LIGHT_TASKS: usize = 25_000;
static EX: Executor<'_> = Executor::new();
fn run(f: impl FnOnce(), multithread: bool) {
let limit = if multithread {
available_parallelism().unwrap().get()
} else {
1
};
let (s, r) = async_channel::bounded::<()>(1);
easy_parallel::Parallel::new()
.each(0..limit, |_| future::block_on(EX.run(r.recv())))
.finish(move || {
let _s = s;
f()
});
}
fn create(c: &mut Criterion) {
c.bench_function("executor::create", |b| {
b.iter(|| {
let ex = Executor::new();
let task = ex.spawn(async {});
future::block_on(ex.run(task));
})
});
}
fn running_benches(c: &mut Criterion) {
for (group_name, multithread) in [("single_thread", false), ("multi_thread", true)].iter() {
let mut group = c.benchmark_group(group_name.to_string());
group.bench_function("executor::spawn_one", |b| {
run(
|| {
b.iter(|| {
future::block_on(async { EX.spawn(async {}).await });
});
},
*multithread,
);
});
group.bench_function("executor::spawn_many_local", |b| {
run(
|| {
b.iter(move || {
future::block_on(async {
let mut tasks = Vec::new();
for _ in 0..LIGHT_TASKS {
tasks.push(EX.spawn(async {}));
}
for task in tasks {
task.await;
}
});
});
},
*multithread,
);
});
group.bench_function("executor::spawn_recursively", |b| {
#[allow(clippy::manual_async_fn)]
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
async move {
if i != 0 {
EX.spawn(async move {
let fut = go(i - 1).boxed();
fut.await;
})
.await;
}
}
}
run(
|| {
b.iter(move || {
future::block_on(async {
let mut tasks = Vec::new();
for _ in 0..TASKS {
tasks.push(EX.spawn(go(STEPS)));
}
for task in tasks {
task.await;
}
});
});
},
*multithread,
);
});
group.bench_function("executor::yield_now", |b| {
run(
|| {
b.iter(move || {
future::block_on(async {
let mut tasks = Vec::new();
for _ in 0..TASKS {
tasks.push(EX.spawn(async move {
for _ in 0..STEPS {
future::yield_now().await;
}
}));
}
for task in tasks {
task.await;
}
});
});
},
*multithread,
);
});
}
}
criterion_group!(benches, create, running_benches);
criterion_main!(benches);