|
| 1 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 2 | + |
| 3 | +use ticked_async_executor::TickedAsyncExecutor; |
| 4 | + |
| 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 | + #[cfg(feature = "timer_registration")] |
| 12 | + timer_from_timer_registration_benchmark(c); |
| 13 | +} |
| 14 | + |
| 15 | +fn spawn_tasks_benchmark(c: &mut Criterion) { |
| 16 | + c.bench_function("Spawn 1 task", |b| { |
| 17 | + b.iter_with_large_drop(|| { |
| 18 | + let mut executor = TickedAsyncExecutor::default(); |
| 19 | + executor.spawn_local("empty", async move {}).detach(); |
| 20 | + executor.tick(0.1, None); |
| 21 | + assert_eq!(executor.num_tasks(), 0); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + c.bench_function("Spawn 2 tasks", |b| { |
| 26 | + b.iter_with_large_drop(|| { |
| 27 | + let mut executor = TickedAsyncExecutor::default(); |
| 28 | + executor.spawn_local("empty1", async move {}).detach(); |
| 29 | + executor.spawn_local("empty2", async move {}).detach(); |
| 30 | + |
| 31 | + executor.tick(0.1, None); |
| 32 | + assert_eq!(executor.num_tasks(), 0); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + c.bench_function("Spawn 100 tasks", |b| { |
| 37 | + b.iter_with_large_drop(|| { |
| 38 | + let mut executor = TickedAsyncExecutor::default(); |
| 39 | + for _ in 0..100 { |
| 40 | + executor.spawn_local("_", async move {}).detach(); |
| 41 | + } |
| 42 | + |
| 43 | + executor.tick(0.1, None); |
| 44 | + assert_eq!(executor.num_tasks(), 0); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + c.bench_function("Spawn 1000 tasks", |b| { |
| 49 | + b.iter_with_large_drop(|| { |
| 50 | + let mut executor = TickedAsyncExecutor::default(); |
| 51 | + for _ in 0..1000 { |
| 52 | + executor.spawn_local("_", async move {}).detach(); |
| 53 | + } |
| 54 | + |
| 55 | + executor.tick(0.1, None); |
| 56 | + assert_eq!(executor.num_tasks(), 0); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + c.bench_function("Spawn 10000 tasks", |b| { |
| 61 | + b.iter_with_large_drop(|| { |
| 62 | + let mut executor = TickedAsyncExecutor::default(); |
| 63 | + for _ in 0..10000 { |
| 64 | + executor.spawn_local("_", async move {}).detach(); |
| 65 | + } |
| 66 | + |
| 67 | + executor.tick(0.1, None); |
| 68 | + assert_eq!(executor.num_tasks(), 0); |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(feature = "tick_event")] |
| 74 | +fn timer_from_tick_event_benchmark(c: &mut Criterion) { |
| 75 | + c.bench_function("Spawn 1 timer from tick event", |b| { |
| 76 | + b.iter_with_large_drop(|| { |
| 77 | + let mut executor = TickedAsyncExecutor::default(); |
| 78 | + |
| 79 | + let timer = executor.create_timer_from_tick_event(); |
| 80 | + executor |
| 81 | + .spawn_local("empty", async move { |
| 82 | + timer.sleep_for(1.0).await; |
| 83 | + }) |
| 84 | + .detach(); |
| 85 | + |
| 86 | + executor.wait_till_completed(0.1); |
| 87 | + assert_eq!(executor.num_tasks(), 0); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + c.bench_function("Spawn 1000 timers from tick event", |b| { |
| 92 | + b.iter_with_large_drop(|| { |
| 93 | + let mut executor = TickedAsyncExecutor::default(); |
| 94 | + |
| 95 | + for _ in 0..1000 { |
| 96 | + let timer = executor.create_timer_from_tick_event(); |
| 97 | + executor |
| 98 | + .spawn_local("empty", async move { |
| 99 | + timer.sleep_for(1.0).await; |
| 100 | + }) |
| 101 | + .detach(); |
| 102 | + } |
| 103 | + |
| 104 | + executor.wait_till_completed(0.1); |
| 105 | + assert_eq!(executor.num_tasks(), 0); |
| 106 | + }); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +#[cfg(feature = "timer_registration")] |
| 111 | +fn timer_from_timer_registration_benchmark(c: &mut Criterion) { |
| 112 | + c.bench_function("Spawn 1 timer from timer registration", |b| { |
| 113 | + b.iter_with_large_drop(|| { |
| 114 | + let mut executor = TickedAsyncExecutor::default(); |
| 115 | + |
| 116 | + let timer = executor.create_timer_from_timer_registration(); |
| 117 | + executor |
| 118 | + .spawn_local("empty", async move { |
| 119 | + timer.sleep_for(1.0).await; |
| 120 | + }) |
| 121 | + .detach(); |
| 122 | + |
| 123 | + executor.wait_till_completed(0.1); |
| 124 | + assert_eq!(executor.num_tasks(), 0); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + c.bench_function("Spawn 1000 timers from timer registration", |b| { |
| 129 | + b.iter_with_large_drop(|| { |
| 130 | + let mut executor = TickedAsyncExecutor::default(); |
| 131 | + |
| 132 | + for _ in 0..1000 { |
| 133 | + let timer = executor.create_timer_from_timer_registration(); |
| 134 | + executor |
| 135 | + .spawn_local("empty", async move { |
| 136 | + timer.sleep_for(1.0).await; |
| 137 | + }) |
| 138 | + .detach(); |
| 139 | + } |
| 140 | + |
| 141 | + executor.wait_till_completed(0.1); |
| 142 | + assert_eq!(executor.num_tasks(), 0); |
| 143 | + }); |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +criterion_group!(benches, ticked_async_executor_benchmark); |
| 148 | +criterion_main!(benches); |
0 commit comments