1use tokio::{
2 runtime::Runtime,
3 sync::{mpsc, oneshot},
4};
5
6use criterion::{criterion_group, criterion_main, Criterion};
7
8fn request_reply_current_thread(c: &mut Criterion) {
9 let rt = tokio::runtime::Builder::new_current_thread()
10 .build()
11 .unwrap();
12
13 request_reply(c, rt);
14}
15
16fn request_reply_multi_threaded(c: &mut Criterion) {
17 let rt = tokio::runtime::Builder::new_multi_thread()
18 .worker_threads(1)
19 .build()
20 .unwrap();
21
22 request_reply(c, rt);
23}
24
25fn request_reply(b: &mut Criterion, rt: Runtime) {
26 let tx = rt.block_on(async move {
27 let (tx, mut rx) = mpsc::channel::<oneshot::Sender<()>>(10);
28 tokio::spawn(async move {
29 while let Some(reply) = rx.recv().await {
30 reply.send(()).unwrap();
31 }
32 });
33 tx
34 });
35
36 b.bench_function("request_reply", |b| {
37 b.iter(|| {
38 let task_tx = tx.clone();
39 rt.block_on(async move {
40 for _ in 0..1_000 {
41 let (o_tx, o_rx) = oneshot::channel();
42 task_tx.send(o_tx).await.unwrap();
43 let _ = o_rx.await;
44 }
45 })
46 })
47 });
48}
49
50criterion_group!(
51 sync_mpsc_oneshot_group,
52 request_reply_current_thread,
53 request_reply_multi_threaded,
54);
55
56criterion_main!(sync_mpsc_oneshot_group);
57