-
Notifications
You must be signed in to change notification settings - Fork 5
/
throughput.rs
156 lines (127 loc) · 4.92 KB
/
throughput.rs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use criterion::measurement::{Measurement, WallTime};
use criterion::{criterion_group, criterion_main, BenchmarkGroup, Criterion, Throughput};
use futures::future::{try_join, try_join_all};
use futures::prelude::*;
use ring_channel::{ring_channel, RingReceiver, RingSender, TryRecvError};
use std::{collections::BTreeSet, fmt::Debug, hint, iter, mem::size_of, thread, time::Duration};
use tokio::runtime::Runtime;
use tokio::task::{self, JoinHandle};
#[cfg(feature = "futures_api")]
use futures::{stream::unfold, SinkExt};
trait Routine<T> {
fn produce(tx: RingSender<T>, limit: usize) -> JoinHandle<usize>;
fn consume(rx: RingReceiver<T>, limit: usize) -> JoinHandle<usize>;
}
async fn run<R, const N: usize>(msgs: usize, cap: usize, p: usize, c: usize) -> Duration
where
R: Routine<[char; N]>,
{
let (tx, rx) = ring_channel(cap.try_into().unwrap());
let producer = try_join_all(iter::repeat(tx).take(p).map(|tx| R::produce(tx, msgs / p)));
let consumer = try_join_all(iter::repeat(rx).take(c).map(|rx| R::consume(rx, msgs / c)));
let checkpoint = WallTime.start();
let (produced, consumed) = try_join(producer, consumer).await.unwrap();
let elapsed = WallTime.end(checkpoint);
elapsed.div_f64(consumed.iter().sum::<usize>() as f64 / produced.iter().sum::<usize>() as f64)
}
fn bench<R, const N: usize>(group: &mut BenchmarkGroup<WallTime>, p: usize, c: usize)
where
R: Debug + Default + Routine<[char; N]>,
{
let prefix = format!("{}x{}/{:?}/{}B", p, c, R::default(), size_of::<[char; N]>());
let messages_per_iter = 128 * p;
group.throughput(Throughput::Elements(messages_per_iter as u64));
for cap in BTreeSet::from([1, 2, p + c]) {
group.bench_function(format!("{prefix}/{cap}"), |b| {
b.to_async(Runtime::new().unwrap())
.iter_custom(|iters| run::<R, N>(iters as usize * messages_per_iter, cap, p, c));
});
}
}
#[cfg(feature = "futures_api")]
#[derive(Debug, Default)]
struct Async;
#[cfg(feature = "futures_api")]
impl<T: 'static + Send + Default> Routine<T> for Async {
fn produce(tx: RingSender<T>, limit: usize) -> JoinHandle<usize> {
let producer = unfold(tx, |mut tx| async move {
<_ as SinkExt<_>>::send(&mut tx, T::default()).await.ok()?;
Some(((), tx))
});
task::spawn(producer.take(limit).count())
}
fn consume(rx: RingReceiver<T>, limit: usize) -> JoinHandle<usize> {
task::spawn(rx.take(limit).count())
}
}
#[derive(Debug, Default)]
struct Block;
impl<T: 'static + Send + Default> Routine<T> for Block {
fn produce(tx: RingSender<T>, limit: usize) -> JoinHandle<usize> {
let producer = iter::from_fn(move || tx.send(T::default()).ok());
task::spawn_blocking(move || producer.take(limit).count())
}
fn consume(rx: RingReceiver<T>, limit: usize) -> JoinHandle<usize> {
let consumer = iter::from_fn(move || loop {
match rx.try_recv() {
Ok(m) => return Some(m),
Err(TryRecvError::Disconnected) => return None,
Err(TryRecvError::Empty) => hint::spin_loop(),
}
});
task::spawn_blocking(move || consumer.take(limit).count())
}
}
fn mpmc(c: &mut Criterion) {
let mut group = c.benchmark_group("mpmc");
let concurrency = (thread::available_parallelism().unwrap().get() / 2).max(2);
#[cfg(feature = "futures_api")]
{
bench::<Async, 1>(&mut group, concurrency, concurrency);
bench::<Async, 8>(&mut group, concurrency, concurrency);
}
{
bench::<Block, 1>(&mut group, concurrency, concurrency);
bench::<Block, 8>(&mut group, concurrency, concurrency);
}
}
fn mpsc(c: &mut Criterion) {
let mut group = c.benchmark_group("mpsc");
let concurrency = (thread::available_parallelism().unwrap().get() - 1).max(2);
#[cfg(feature = "futures_api")]
{
bench::<Async, 1>(&mut group, concurrency, 1);
bench::<Async, 8>(&mut group, concurrency, 1);
}
{
bench::<Block, 1>(&mut group, concurrency, 1);
bench::<Block, 8>(&mut group, concurrency, 1);
}
}
fn spmc(c: &mut Criterion) {
let mut group = c.benchmark_group("spmc");
let concurrency = (thread::available_parallelism().unwrap().get() - 1).max(2);
#[cfg(feature = "futures_api")]
{
bench::<Async, 1>(&mut group, 1, concurrency);
bench::<Async, 8>(&mut group, 1, concurrency);
}
{
bench::<Block, 1>(&mut group, 1, concurrency);
bench::<Block, 8>(&mut group, 1, concurrency);
}
}
fn spsc(c: &mut Criterion) {
let mut group = c.benchmark_group("spsc");
#[cfg(feature = "futures_api")]
{
bench::<Async, 1>(&mut group, 1, 1);
bench::<Async, 8>(&mut group, 1, 1);
}
{
bench::<Block, 1>(&mut group, 1, 1);
bench::<Block, 8>(&mut group, 1, 1);
}
}
criterion_group!(benches, mpmc, mpsc, spmc, spsc);
criterion_main!(benches);