Skip to content

Commit 9b9dda4

Browse files
committed
Added criterion benchmark
1 parent f6fb0bb commit 9b9dda4

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

benches/benchmark.rs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ use criterion::{criterion_group, criterion_main, Criterion};
22

33
use ticked_async_executor::TickedAsyncExecutor;
44

5+
fn ticked_async_executor_benchmark(c: &mut Criterion) {
6+
spawn_tasks_benchmark(c);
7+
8+
#[cfg(feature = "tick_event")]
9+
timer_from_tick_event_benchmark(c);
10+
}
11+
512
fn spawn_tasks_benchmark(c: &mut Criterion) {
6-
c.bench_function("1 task", |b| {
13+
c.bench_function("Spawn 1 task", |b| {
714
b.iter_with_large_drop(|| {
815
let mut executor = TickedAsyncExecutor::default();
916
executor.spawn_local("empty", async move {}).detach();
@@ -12,7 +19,7 @@ fn spawn_tasks_benchmark(c: &mut Criterion) {
1219
});
1320
});
1421

15-
c.bench_function("2 tasks", |b| {
22+
c.bench_function("Spawn 2 tasks", |b| {
1623
b.iter_with_large_drop(|| {
1724
let mut executor = TickedAsyncExecutor::default();
1825
executor.spawn_local("empty1", async move {}).detach();
@@ -22,7 +29,82 @@ fn spawn_tasks_benchmark(c: &mut Criterion) {
2229
assert_eq!(executor.num_tasks(), 0);
2330
});
2431
});
32+
33+
c.bench_function("Spawn 100 tasks", |b| {
34+
b.iter_with_large_drop(|| {
35+
let mut executor = TickedAsyncExecutor::default();
36+
for _ in 0..100 {
37+
executor.spawn_local("_", async move {}).detach();
38+
}
39+
40+
executor.tick(0.1, None);
41+
assert_eq!(executor.num_tasks(), 0);
42+
});
43+
});
44+
45+
c.bench_function("Spawn 1000 tasks", |b| {
46+
b.iter_with_large_drop(|| {
47+
let mut executor = TickedAsyncExecutor::default();
48+
for _ in 0..1000 {
49+
executor.spawn_local("_", async move {}).detach();
50+
}
51+
52+
executor.tick(0.1, None);
53+
assert_eq!(executor.num_tasks(), 0);
54+
});
55+
});
56+
57+
c.bench_function("Spawn 10000 tasks", |b| {
58+
b.iter_with_large_drop(|| {
59+
let mut executor = TickedAsyncExecutor::default();
60+
for _ in 0..10000 {
61+
executor.spawn_local("_", async move {}).detach();
62+
}
63+
64+
executor.tick(0.1, None);
65+
assert_eq!(executor.num_tasks(), 0);
66+
});
67+
});
68+
}
69+
70+
#[cfg(feature = "tick_event")]
71+
fn timer_from_tick_event_benchmark(c: &mut Criterion) {
72+
c.bench_function("Spawn 1 timer from tick event", |b| {
73+
b.iter_with_large_drop(|| {
74+
let mut executor = TickedAsyncExecutor::default();
75+
let timer = executor.create_timer_from_tick_event();
76+
77+
executor
78+
.spawn_local("empty", async move {
79+
timer.sleep_for(1.0).await;
80+
})
81+
.detach();
82+
83+
executor.wait_till_completed(0.1);
84+
assert_eq!(executor.num_tasks(), 0);
85+
});
86+
});
87+
88+
c.bench_function("Spawn 1000 timers from tick event", |b| {
89+
b.iter_with_large_drop(|| {
90+
let mut executor = TickedAsyncExecutor::default();
91+
92+
for _ in 0..1000 {
93+
let timer = executor.create_timer_from_tick_event();
94+
executor
95+
.spawn_local("empty", async move {
96+
timer.sleep_for(1.0).await;
97+
})
98+
.detach();
99+
}
100+
101+
for _ in 0..11 {
102+
executor.tick(0.1, None);
103+
}
104+
assert_eq!(executor.num_tasks(), 0);
105+
});
106+
});
25107
}
26108

27-
criterion_group!(benches, spawn_tasks_benchmark);
109+
criterion_group!(benches, ticked_async_executor_benchmark);
28110
criterion_main!(benches);

0 commit comments

Comments
 (0)