Debian Codegraph @

benches/basic.rs

raw symbols SCIP
1 #[macro_use]
2 extern crate criterion;
3
4 use std::{
5 sync::mpsc,
6 thread,
7 fmt::Debug,
8 };
9 use criterion::{Criterion, Bencher, black_box};
10 use std::time::Instant;
11
12 trait Sender: Clone + Send + Sized + 'static {
13 type Item: Debug + Default;
14 type BoundedSender: Sender<Item=Self::Item>;
15 type Receiver: Receiver<Item=Self::Item>;
16
17 fn unbounded() -> (Self, Self::Receiver);
18 fn bounded(n: usize) -> (Self::BoundedSender, Self::Receiver);
19 fn send(&self, msg: Self::Item);
20 }
21
22 trait Receiver: Send + Sized + 'static {
23 type Item: Default;
24 fn recv(&self) -> Self::Item;
25 fn iter(&self) -> Box<dyn Iterator<Item=Self::Item> + '_>;
26 }
27
28 impl<T: Send + Debug + Default + 'static> Sender for flume::Sender<T> {
29 type Item = T;
30 type BoundedSender = Self;
31 type Receiver = flume::Receiver<T>;
32
33 fn unbounded() -> (Self, Self::Receiver) {
34 flume::unbounded()
35 }
36
37 fn bounded(n: usize) -> (Self::BoundedSender, Self::Receiver) {
38 flume::bounded(n)
39 }
40
41 fn send(&self, msg: T) {
42 flume::Sender::send(self, msg).unwrap();
43 }
44 }
45
46 impl<T: Send + Default + 'static> Receiver for flume::Receiver<T> {
47 type Item = T;
48
49 fn recv(&self) -> Self::Item {
50 flume::Receiver::recv(self).unwrap()
51 }
52
53 fn iter(&self) -> Box<dyn Iterator<Item=T> + '_> {
54 Box::new(std::iter::from_fn(move || flume::Receiver::recv(self).ok()))
55 }
56 }
57
58 impl<T: Send + Debug + Default + 'static> Sender for crossbeam_channel::Sender<T> {
59 type Item = T;
60 type BoundedSender = Self;
61 type Receiver = crossbeam_channel::Receiver<T>;
62
63 fn unbounded() -> (Self, Self::Receiver) {
64 crossbeam_channel::unbounded()
65 }
66
67 fn bounded(n: usize) -> (Self::BoundedSender, Self::Receiver) {
68 crossbeam_channel::bounded(n)
69 }
70
71 fn send(&self, msg: T) {
72 crossbeam_channel::Sender::send(self, msg).unwrap();
73 }
74 }
75
76 impl<T: Send + Default + 'static> Receiver for crossbeam_channel::Receiver<T> {
77 type Item = T;
78
79 fn recv(&self) -> Self::Item {
80 crossbeam_channel::Receiver::recv(self).unwrap()
81 }
82
83 fn iter(&self) -> Box<dyn Iterator<Item=T> + '_> {
84 Box::new(crossbeam_channel::Receiver::iter(self))
85 }
86 }
87
88 impl<T: Send + Debug + Default + 'static> Sender for mpsc::Sender<T> {
89 type Item = T;
90 type BoundedSender = mpsc::SyncSender<T>;
91 type Receiver = mpsc::Receiver<T>;
92
93 fn unbounded() -> (Self, Self::Receiver) {
94 mpsc::channel()
95 }
96
97 fn bounded(n: usize) -> (Self::BoundedSender, Self::Receiver) {
98 mpsc::sync_channel(n)
99 }
100
101 fn send(&self, msg: T) {
102 mpsc::Sender::send(self, msg).unwrap();
103 }
104 }
105
106 impl<T: Send + Debug + Default + 'static> Sender for mpsc::SyncSender<T> {
107 type Item = T;
108 type BoundedSender = Self;
109 type Receiver = mpsc::Receiver<T>;
110
111 fn unbounded() -> (Self, Self::Receiver) { unimplemented!() }
112 fn bounded(_: usize) -> (Self::BoundedSender, Self::Receiver) { unimplemented!() }
113
114 fn send(&self, msg: T) {
115 mpsc::SyncSender::send(self, msg).unwrap();
116 }
117 }
118
119 impl<T: Send + Default + 'static> Receiver for mpsc::Receiver<T> {
120 type Item = T;
121
122 fn recv(&self) -> Self::Item {
123 mpsc::Receiver::recv(self).unwrap()
124 }
125
126 fn iter(&self) -> Box<dyn Iterator<Item=T> + '_> {
127 Box::new(mpsc::Receiver::iter(self))
128 }
129 }
130
131 fn test_create<S: Sender>(b: &mut Bencher) {
132 b.iter(|| S::unbounded());
133 }
134
135 fn test_oneshot<S: Sender>(b: &mut Bencher) {
136 b.iter(|| {
137 let (tx, rx) = S::unbounded();
138 tx.send(Default::default());
139 black_box(rx.recv());
140 });
141 }
142
143 fn test_inout<S: Sender>(b: &mut Bencher) {
144 let (tx, rx) = S::unbounded();
145 b.iter(|| {
146 tx.send(Default::default());
147 black_box(rx.recv());
148 });
149 }
150
151 fn test_hydra<S: Sender>(b: &mut Bencher, thread_num: usize, msg_num: usize) {
152 let (main_tx, main_rx) = S::unbounded();
153
154 let mut txs = Vec::new();
155 for _ in 0..thread_num {
156 let main_tx = main_tx.clone();
157 let (tx, rx) = S::unbounded();
158 txs.push(tx);
159
160 thread::spawn(move || {
161 for msg in rx.iter() {
162 main_tx.send(msg);
163 }
164 });
165 }
166
167 drop(main_tx);
168
169 b.iter(|| {
170 for tx in &txs {
171 for _ in 0..msg_num {
172 tx.send(Default::default());
173 }
174 }
175
176 for _ in 0..thread_num {
177 for _ in 0..msg_num {
178 black_box(main_rx.recv());
179 }
180 }
181 });
182 }
183
184 fn test_kitsune<S: Sender>(b: &mut Bencher, thread_num: usize, msg_num: usize)
185 where S::Receiver: Clone
186 {
187 let (out_tx, out_rx) = S::unbounded();
188 let (in_tx, in_rx) = S::unbounded();
189
190 for _ in 0..thread_num {
191 let in_tx = in_tx.clone();
192 let out_rx = out_rx.clone();
193
194 thread::spawn(move || {
195 for msg in out_rx.iter() {
196 in_tx.send(msg);
197 }
198 });
199 }
200
201 b.iter(|| {
202 for _ in 0..thread_num {
203 for _ in 0..msg_num {
204 out_tx.send(Default::default());
205 }
206 }
207
208 for _ in 0..thread_num {
209 for _ in 0..msg_num {
210 black_box(in_rx.recv());
211 }
212 }
213 });
214 }
215
216 fn test_robin_u<S: Sender>(b: &mut Bencher, thread_num: usize, msg_num: usize) {
217 let (mut main_tx, main_rx) = S::unbounded();
218
219 for _ in 0..thread_num {
220 let (mut tx, rx) = S::unbounded();
221 std::mem::swap(&mut tx, &mut main_tx);
222
223 thread::spawn(move || {
224 for msg in rx.iter() {
225 tx.send(msg);
226 }
227 });
228 }
229
230 b.iter(|| {
231 for _ in 0..msg_num {
232 main_tx.send(Default::default());
233 }
234
235 for _ in 0..msg_num {
236 black_box(main_rx.recv());
237 }
238 });
239 }
240
241 fn test_robin_b<S: Sender>(b: &mut Bencher, thread_num: usize, msg_num: usize) {
242 let (mut main_tx, main_rx) = S::bounded(1);
243
244 for _ in 0..thread_num {
245 let (mut tx, rx) = S::bounded(1);
246 std::mem::swap(&mut tx, &mut main_tx);
247
248 thread::spawn(move || {
249 for msg in rx.iter() {
250 tx.send(msg);
251 }
252 });
253 }
254
255 b.iter(|| {
256 let main_tx = main_tx.clone();
257 thread::spawn(move || {
258 for _ in 0..msg_num {
259 main_tx.send(Default::default());
260 }
261 });
262
263 for _ in 0..msg_num {
264 black_box(main_rx.recv());
265 }
266 });
267 }
268
269 fn test_mpsc_bounded_no_wait<S: Sender>(b: &mut Bencher, thread_num: u64) {
270 b.iter_custom(|iters| {
271 let iters = iters * 1000;
272 let (tx, rx) = S::bounded(iters as usize);
273 let start = Instant::now();
274
275 crossbeam_utils::thread::scope(|scope| {
276 for _ in 0..thread_num {
277 let tx = tx.clone();
278 scope.spawn(move |_| {
279 for _ in 0..iters / thread_num {
280 tx.send(Default::default());
281 }
282 });
283 }
284
285 for _ in 0..iters - ((iters / thread_num) * thread_num) {
286 tx.send(Default::default());
287 }
288
289 for _ in 0..iters {
290 black_box(rx.recv());
291 }
292 })
293 .unwrap();
294
295 start.elapsed()
296 })
297 }
298
299 fn test_mpsc_bounded<S: Sender>(b: &mut Bencher, bound: usize, thread_num: usize) {
300 b.iter_custom(|iters| {
301 let (tx, rx) = S::bounded(bound);
302 let start = Instant::now();
303
304 crossbeam_utils::thread::scope(|scope| {
305 let msgs = iters as usize * bound.max(1);
306
307 for _ in 0..thread_num {
308 let tx = tx.clone();
309 scope.spawn(move |_| {
310 for _ in 0..msgs / thread_num as usize {
311 tx.send(Default::default());
312 }
313 });
314 }
315
316 scope.spawn(move |_| {
317 // Remainder
318 for _ in 0..msgs - (msgs / thread_num as usize * thread_num) {
319 tx.send(Default::default());
320 }
321 });
322
323 for _ in 0..msgs {
324 black_box(rx.recv());
325 }
326 })
327 .unwrap();
328
329 start.elapsed()
330 })
331 }
332
333 fn create(b: &mut Criterion) {
334 b.bench_function("create-flume", |b| test_create::<flume::Sender<u32>>(b));
335 b.bench_function("create-crossbeam", |b| test_create::<crossbeam_channel::Sender<u32>>(b));
336 b.bench_function("create-std", |b| test_create::<mpsc::Sender<u32>>(b));
337 }
338
339 fn oneshot(b: &mut Criterion) {
340 b.bench_function("oneshot-flume", |b| test_oneshot::<flume::Sender<u32>>(b));
341 b.bench_function("oneshot-crossbeam", |b| test_oneshot::<crossbeam_channel::Sender<u32>>(b));
342 b.bench_function("oneshot-std", |b| test_oneshot::<mpsc::Sender<u32>>(b));
343 }
344
345 fn inout(b: &mut Criterion) {
346 b.bench_function("inout-flume", |b| test_inout::<flume::Sender<u32>>(b));
347 b.bench_function("inout-crossbeam", |b| test_inout::<crossbeam_channel::Sender<u32>>(b));
348 b.bench_function("inout-std", |b| test_inout::<mpsc::Sender<u32>>(b));
349 }
350
351 fn hydra_32t_1m(b: &mut Criterion) {
352 b.bench_function("hydra-32t-1m-flume", |b| test_hydra::<flume::Sender<u32>>(b, 32, 1));
353 b.bench_function("hydra-32t-1m-crossbeam", |b| test_hydra::<crossbeam_channel::Sender<u32>>(b, 32, 1));
354 b.bench_function("hydra-32t-1m-std", |b| test_hydra::<mpsc::Sender<u32>>(b, 32, 1));
355 }
356
357 fn hydra_32t_1000m(b: &mut Criterion) {
358 b.bench_function("hydra-32t-1000m-flume", |b| test_hydra::<flume::Sender<u32>>(b, 32, 1000));
359 b.bench_function("hydra-32t-1000m-crossbeam", |b| test_hydra::<crossbeam_channel::Sender<u32>>(b, 32, 1000));
360 b.bench_function("hydra-32t-1000m-std", |b| test_hydra::<mpsc::Sender<u32>>(b, 32, 1000));
361 }
362
363 fn hydra_256t_1m(b: &mut Criterion) {
364 b.bench_function("hydra-256t-1m-flume", |b| test_hydra::<flume::Sender<u32>>(b, 256, 1));
365 b.bench_function("hydra-256t-1m-crossbeam", |b| test_hydra::<crossbeam_channel::Sender<u32>>(b, 256, 1));
366 b.bench_function("hydra-256t-1m-std", |b| test_hydra::<mpsc::Sender<u32>>(b, 256, 1));
367 }
368
369 fn hydra_1t_1000m(b: &mut Criterion) {
370 b.bench_function("hydra-1t-1000m-flume", |b| test_hydra::<flume::Sender<u32>>(b, 1, 1000));
371 b.bench_function("hydra-1t-1000m-crossbeam", |b| test_hydra::<crossbeam_channel::Sender<u32>>(b, 1, 1000));
372 b.bench_function("hydra-1t-1000m-std", |b| test_hydra::<mpsc::Sender<u32>>(b, 1, 1000));
373 }
374
375 fn hydra_4t_10000m(b: &mut Criterion) {
376 b.bench_function("hydra-4t-10000m-flume", |b| test_hydra::<flume::Sender<u32>>(b, 4, 10000));
377 b.bench_function("hydra-4t-10000m-crossbeam", |b| test_hydra::<crossbeam_channel::Sender<u32>>(b, 4, 10000));
378 b.bench_function("hydra-4t-10000m-std", |b| test_hydra::<mpsc::Sender<u32>>(b, 4, 10000));
379 }
380
381 fn kitsune_32t_1m(b: &mut Criterion) {
382 b.bench_function("kitsune-32t-1m-flume", |b| test_kitsune::<flume::Sender<u32>>(b, 32, 1));
383 b.bench_function("kitsune-32t-1m-crossbeam", |b| test_kitsune::<crossbeam_channel::Sender<u32>>(b, 32, 1));
384 //b.bench_function("kitsune-32t-1m-std", |b| test_kitsune::<mpsc::Sender<u32>>(b, 32, 1));
385 }
386
387 fn kitsune_32t_1000m(b: &mut Criterion) {
388 b.bench_function("kitsune-32t-1000m-flume", |b| test_kitsune::<flume::Sender<u32>>(b, 32, 1000));
389 b.bench_function("kitsune-32t-1000m-crossbeam", |b| test_kitsune::<crossbeam_channel::Sender<u32>>(b, 32, 1000));
390 //b.bench_function("kitsune-32t-1000m-std", |b| test_kitsune::<mpsc::Sender<u32>>(b, 32, 1000));
391 }
392
393 fn kitsune_256t_1m(b: &mut Criterion) {
394 b.bench_function("kitsune-256t-1m-flume", |b| test_kitsune::<flume::Sender<u32>>(b, 256, 1));
395 b.bench_function("kitsune-256t-1m-crossbeam", |b| test_kitsune::<crossbeam_channel::Sender<u32>>(b, 256, 1));
396 //b.bench_function("kitsune-256t-1m-std", |b| test_kitsune::<mpsc::Sender<u32>>(b, 256, 1));
397 }
398
399 fn kitsune_1t_1000m(b: &mut Criterion) {
400 b.bench_function("kitsune-1t-1000m-flume", |b| test_kitsune::<flume::Sender<u32>>(b, 1, 1000));
401 b.bench_function("kitsune-1t-1000m-crossbeam", |b| test_kitsune::<crossbeam_channel::Sender<u32>>(b, 1, 1000));
402 //b.bench_function("kitsune-1t-1000m-std", |b| test_kitsune::<mpsc::Sender<u32>>(b, 1, 1000));
403 }
404
405 fn kitsune_4t_10000m(b: &mut Criterion) {
406 b.bench_function("kitsune-4t-10000m-flume", |b| test_kitsune::<flume::Sender<u32>>(b, 4, 10000));
407 b.bench_function("kitsune-4t-10000m-crossbeam", |b| test_kitsune::<crossbeam_channel::Sender<u32>>(b, 4, 10000));
408 //b.bench_function("kitsune-4t-10000m-std", |b| test_kitsune::<mpsc::Sender<u32>>(b, 4, 10000));
409 }
410
411 fn robin_u_32t_1m(b: &mut Criterion) {
412 b.bench_function("robin-u-32t-1m-flume", |b| test_robin_u::<flume::Sender<u32>>(b, 32, 1));
413 b.bench_function("robin-u-32t-1m-crossbeam", |b| test_robin_u::<crossbeam_channel::Sender<u32>>(b, 32, 1));
414 b.bench_function("robin-u-32t-1m-std", |b| test_robin_u::<mpsc::Sender<u32>>(b, 32, 1));
415 }
416
417 fn robin_u_4t_1000m(b: &mut Criterion) {
418 b.bench_function("robin-u-4t-1000m-flume", |b| test_robin_u::<flume::Sender<u32>>(b, 4, 1000));
419 b.bench_function("robin-u-4t-1000m-crossbeam", |b| test_robin_u::<crossbeam_channel::Sender<u32>>(b, 4, 1000));
420 b.bench_function("robin-u-4t-1000m-std", |b| test_robin_u::<mpsc::Sender<u32>>(b, 4, 1000));
421 }
422
423 fn robin_b_32t_16m(b: &mut Criterion) {
424 b.bench_function("robin-b-32t-16m-flume", |b| test_robin_b::<flume::Sender<u32>>(b, 32, 16));
425 b.bench_function("robin-b-32t-16m-crossbeam", |b| test_robin_b::<crossbeam_channel::Sender<u32>>(b, 32, 16));
426 b.bench_function("robin-b-32t-16m-std", |b| test_robin_b::<mpsc::Sender<u32>>(b, 32, 16));
427 }
428
429 fn robin_b_4t_1000m(b: &mut Criterion) {
430 b.bench_function("robin-b-4t-1000m-flume", |b| test_robin_b::<flume::Sender<u32>>(b, 4, 1000));
431 b.bench_function("robin-b-4t-1000m-crossbeam", |b| test_robin_b::<crossbeam_channel::Sender<u32>>(b, 4, 1000));
432 b.bench_function("robin-b-4t-1000m-std", |b| test_robin_b::<mpsc::Sender<u32>>(b, 4, 1000));
433 }
434
435 fn mpsc_bounded_no_wait_4t(b: &mut Criterion) {
436 b.bench_function("mpsc-bounded-no-wait-4t-flume", |b| test_mpsc_bounded_no_wait::<flume::Sender<u32>>(b, 4));
437 b.bench_function("mpsc-bounded-no-wait-4t-crossbeam", |b| test_mpsc_bounded_no_wait::<crossbeam_channel::Sender<u32>>(b, 4));
438 b.bench_function("mpsc-bounded-no-wait-4t-std", |b| test_mpsc_bounded_no_wait::<mpsc::Sender<u32>>(b, 4));
439 }
440
441 fn mpsc_bounded_4t(b: &mut Criterion) {
442 for bound in &[0, 1, 10, 50, 10_000] {
443 let text = format!("mpsc-bounded-small-4t-{}m-", bound);
444 let bound = *bound;
445
446 b.bench_function(&format!("{}{}", text, "flume"), |b| test_mpsc_bounded::<flume::Sender<u32>>(b, bound, 4));
447 b.bench_function(&format!("{}{}", text, "crossbeam"), |b| test_mpsc_bounded::<crossbeam_channel::Sender<u32>>(b, bound, 4));
448 b.bench_function(&format!("{}{}", text, "std"), |b| test_mpsc_bounded::<mpsc::Sender<u32>>(b, bound, 4));
449 }
450 }
451
452 criterion_group!(
453 compare,
454 create,
455 oneshot,
456 inout,
457 hydra_32t_1m,
458 hydra_32t_1000m,
459 hydra_256t_1m,
460 hydra_1t_1000m,
461 hydra_4t_10000m,
462 robin_b_32t_16m,
463 robin_b_4t_1000m,
464 robin_u_32t_1m,
465 robin_u_4t_1000m,
466 mpsc_bounded_no_wait_4t,
467 mpsc_bounded_4t,
468 kitsune_32t_1m,
469 kitsune_32t_1000m,
470 kitsune_256t_1m,
471 kitsune_1t_1000m,
472 kitsune_4t_10000m,
473 );
474 criterion_main!(compare);