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
|
use criterion::*;
use futures_micro::or;
use futures_lite::future::block_on;
use async_oneshot::oneshot;
pub fn create_destroy(c: &mut Criterion) {
c.bench_function(
"create_destroy",
|b| b.iter(|| oneshot::<usize>())
);
}
#[allow(unused_must_use)]
pub fn send(c: &mut Criterion) {
let mut group = c.benchmark_group("send");
group.bench_function(
"success",
|b| b.iter_batched(
|| oneshot::<usize>(),
|(mut send, recv)| { (send.send(1).unwrap(), recv) },
BatchSize::SmallInput
)
);
group.bench_function(
"closed",
|b| b.iter_batched(
|| oneshot::<usize>().0,
|mut send| send.send(1).unwrap_err(),
BatchSize::SmallInput
)
);
}
pub fn try_recv(c: &mut Criterion) {
let mut group = c.benchmark_group("try_recv");
group.bench_function(
"success",
|b| b.iter_batched(
|| {
let (mut send, recv) = oneshot::<usize>();
send.send(1).unwrap();
recv
},
|recv| recv.try_recv().unwrap(),
BatchSize::SmallInput
)
);
group.bench_function(
"empty",
|b| b.iter_batched(
|| oneshot::<usize>(),
|(send, recv)| (recv.try_recv().unwrap_err(), send),
BatchSize::SmallInput
)
);
group.bench_function(
"closed",
|b| b.iter_batched(
|| oneshot::<usize>().1,
|recv| recv.try_recv().unwrap_err(),
BatchSize::SmallInput
)
);
}
pub fn recv(c: &mut Criterion) {
let mut group = c.benchmark_group("async.recv");
group.bench_function(
"success",
|b| b.iter_batched(
|| {
let (mut send, recv) = oneshot::<usize>();
send.send(42).unwrap();
recv
},
|recv| block_on(recv).unwrap(),
BatchSize::SmallInput
)
);
group.bench_function(
"closed",
|b| b.iter_batched(
|| {
let (send, recv) = oneshot::<usize>();
send.close();
recv
},
|recv| block_on(recv).unwrap_err(),
BatchSize::SmallInput
)
);
}
pub fn wait(c: &mut Criterion) {
let mut group = c.benchmark_group("async.wait");
group.bench_function(
"success",
|b| b.iter_batched(
|| oneshot::<usize>(),
|(send, recv)| block_on(
or!(async { recv.await.unwrap(); 1 },
async { send.wait().await.unwrap(); 2 }
)
),
BatchSize::SmallInput
)
);
group.bench_function(
"closed",
|b| b.iter_batched(
|| oneshot::<usize>().0,
|send| block_on(send.wait()).unwrap_err(),
BatchSize::SmallInput
)
);
}
criterion_group!(
benches
, create_destroy
, send
, try_recv
, recv
, wait
);
criterion_main!(benches);
|