|
| 1 | +#![feature(test)] |
| 2 | + |
| 3 | +extern crate test; |
| 4 | + |
| 5 | +use std::future::Future; |
| 6 | + |
| 7 | +use async_executor::Executor; |
| 8 | +use futures_lite::future::{self, FutureExt}; |
| 9 | + |
| 10 | +const TASKS: usize = 300; |
| 11 | +const STEPS: usize = 300; |
| 12 | +const LIGHT_TASKS: usize = 25_000; |
| 13 | + |
| 14 | +static EX: Executor<'_> = Executor::new(); |
| 15 | + |
| 16 | +fn run(f: impl FnOnce()) { |
| 17 | + let (s, r) = async_channel::bounded::<()>(1); |
| 18 | + easy_parallel::Parallel::new() |
| 19 | + .each(0..num_cpus::get(), |_| future::block_on(EX.run(r.recv()))) |
| 20 | + .finish(move || { |
| 21 | + let _s = s; |
| 22 | + f() |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +#[bench] |
| 27 | +fn create(b: &mut test::Bencher) { |
| 28 | + b.iter(move || { |
| 29 | + let ex = Executor::new(); |
| 30 | + let task = ex.spawn(async {}); |
| 31 | + future::block_on(ex.run(task)); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +#[bench] |
| 36 | +fn spawn_one(b: &mut test::Bencher) { |
| 37 | + run(|| { |
| 38 | + b.iter(move || { |
| 39 | + future::block_on(async { EX.spawn(async {}).await }); |
| 40 | + }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +#[bench] |
| 45 | +fn spawn_many(b: &mut test::Bencher) { |
| 46 | + run(|| { |
| 47 | + b.iter(move || { |
| 48 | + future::block_on(async { |
| 49 | + let mut tasks = Vec::new(); |
| 50 | + for _ in 0..LIGHT_TASKS { |
| 51 | + tasks.push(EX.spawn(async {})); |
| 52 | + } |
| 53 | + for task in tasks { |
| 54 | + task.await; |
| 55 | + } |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +#[bench] |
| 62 | +fn spawn_recursively(b: &mut test::Bencher) { |
| 63 | + fn go(i: usize) -> impl Future<Output = ()> + Send + 'static { |
| 64 | + async move { |
| 65 | + if i != 0 { |
| 66 | + EX.spawn(async move { |
| 67 | + let fut = go(i - 1).boxed(); |
| 68 | + fut.await; |
| 69 | + }) |
| 70 | + .await; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + run(|| { |
| 76 | + b.iter(move || { |
| 77 | + future::block_on(async { |
| 78 | + let mut tasks = Vec::new(); |
| 79 | + for _ in 0..TASKS { |
| 80 | + tasks.push(EX.spawn(go(STEPS))); |
| 81 | + } |
| 82 | + for task in tasks { |
| 83 | + task.await; |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +#[bench] |
| 91 | +fn yield_now(b: &mut test::Bencher) { |
| 92 | + run(|| { |
| 93 | + b.iter(move || { |
| 94 | + future::block_on(async { |
| 95 | + let mut tasks = Vec::new(); |
| 96 | + for _ in 0..TASKS { |
| 97 | + tasks.push(EX.spawn(async move { |
| 98 | + for _ in 0..STEPS { |
| 99 | + future::yield_now().await; |
| 100 | + } |
| 101 | + })); |
| 102 | + } |
| 103 | + for task in tasks { |
| 104 | + task.await; |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | + }); |
| 109 | +} |
0 commit comments