executors 0.10.0

A collection of high-performance task executors.
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Benchmark implementation details of the theaded scheduler. These benches are
//! intended to be used as a form of regression testing and not as a general
//! purpose benchmark demonstrating real-world performance.

use executors::futures_executor::*;

use criterion::{criterion_group, criterion_main, Criterion, Throughput};

const CHAINING_DEPTH: usize = 1000;
const SPAWN_NUM: usize = 1000;
const PINGS_NUM: usize = 1000;

pub fn chained_spawn_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("Chained Spawn");
    group.throughput(Throughput::Elements(CHAINING_DEPTH as u64));
    // CBWP
    group.bench_function("Local Function CBWP", chained_spawn::local_function_cbwp);
    group.bench_function(
        "No-Local Function CBWP",
        chained_spawn::no_local_function_cbwp,
    );
    group.bench_function("Async CBWP", chained_spawn::async_cbwp);
    // CBCP
    group.bench_function("Local Function CBCP", chained_spawn::local_function_cbcp);
    group.bench_function(
        "No-Local Function CBCP",
        chained_spawn::no_local_function_cbcp,
    );
    group.bench_function("Async CBCP", chained_spawn::async_cbcp);
    group.finish();
}

pub fn spawn_many_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("Spawn Many");
    group.throughput(Throughput::Elements(SPAWN_NUM as u64));
    // CBWP
    group.bench_function("Function CBWP", spawn_many::function_cbwp);
    group.bench_function("Async CBWP", spawn_many::async_cbwp);
    // CBCP
    group.bench_function("Function CBCP", spawn_many::function_cbcp);
    group.bench_function("Async CBCP", spawn_many::async_cbcp);
    group.finish();
}

pub fn ping_pong_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("Ping Pong");
    group.throughput(Throughput::Elements(PINGS_NUM as u64));
    // CBWP
    //group.bench_function("Local Function CBWP", spawn_many::function_cbwp);
    group.bench_function("Async CBWP", ping_pong::async_cbwp);
    // CBCP
    //group.bench_function("Local Function CBCP", spawn_many::function_cbcp);
    group.bench_function("Async CBCP", ping_pong::async_cbcp);
    group.finish();
}

mod chained_spawn {
    use super::*;
    use criterion::Bencher;
    use std::sync::mpsc;

    fn cbwp_rt() -> impl FuturesExecutor {
        executors::crossbeam_workstealing_pool::small_pool(4)
    }

    fn cbcp_rt() -> impl FuturesExecutor {
        executors::crossbeam_channel_pool::ThreadPool::new(4)
    }

    pub fn local_function_cbwp(b: &mut Bencher) {
        let rt = cbwp_rt();
        chained_spawn_fun(b, rt);
    }

    pub fn no_local_function_cbwp(b: &mut Bencher) {
        let rt = cbwp_rt();
        chained_spawn_no_local(b, rt);
    }

    pub fn async_cbwp(b: &mut Bencher) {
        let rt = cbwp_rt();
        chained_spawn_async(b, rt);
    }

    pub fn local_function_cbcp(b: &mut Bencher) {
        let rt = cbcp_rt();
        chained_spawn_fun(b, rt);
    }

    pub fn no_local_function_cbcp(b: &mut Bencher) {
        let rt = cbcp_rt();
        chained_spawn_no_local(b, rt);
    }

    pub fn async_cbcp(b: &mut Bencher) {
        let rt = cbcp_rt();
        chained_spawn_async(b, rt);
    }

    fn chained_spawn_fun(b: &mut Bencher, rt: impl FuturesExecutor) {
        fn iter(done_tx: mpsc::SyncSender<()>, n: usize) {
            if n == 0 {
                done_tx.send(()).unwrap();
            } else {
                let _ = executors::try_execute_locally(move || {
                    iter(done_tx, n - 1);
                });
            }
        }

        let (done_tx, done_rx) = mpsc::sync_channel(1);
        let rt_inner = rt.clone();
        b.iter(move || {
            let done_tx = done_tx.clone();
            rt_inner.execute(move || {
                iter(done_tx, CHAINING_DEPTH);
            });

            done_rx.recv().unwrap();
        });
        rt.shutdown().expect("shutdown");
    }

    fn chained_spawn_no_local(b: &mut Bencher, rt: impl FuturesExecutor) {
        fn iter(rt: impl FuturesExecutor, done_tx: mpsc::SyncSender<()>, n: usize) {
            if n == 0 {
                done_tx.send(()).unwrap();
            } else {
                let rt_new = rt.clone();
                rt.execute(move || {
                    iter(rt_new, done_tx, n - 1);
                });
            }
        }

        let (done_tx, done_rx) = mpsc::sync_channel(1);
        let rt_inner = rt.clone();
        b.iter(move || {
            let done_tx = done_tx.clone();
            let rt_new = rt_inner.clone();
            rt_inner.execute(move || {
                iter(rt_new, done_tx, CHAINING_DEPTH);
            });

            done_rx.recv().unwrap();
        });
        rt.shutdown().expect("shutdown");
    }

    fn chained_spawn_async(b: &mut Bencher, rt: impl FuturesExecutor) {
        fn iter(rt: impl FuturesExecutor, done_tx: mpsc::SyncSender<()>, n: usize) {
            if n == 0 {
                done_tx.send(()).unwrap();
            } else {
                let rt_new = rt.clone();
                rt.spawn(async move {
                    iter(rt_new, done_tx, n - 1);
                })
                .detach();
            }
        }

        let (done_tx, done_rx) = mpsc::sync_channel(1);
        let rt_inner = rt.clone();
        b.iter(move || {
            let done_tx = done_tx.clone();
            futures::executor::block_on(async {
                let rt_new = rt_inner.clone();
                rt_inner
                    .spawn(async move {
                        iter(rt_new, done_tx, CHAINING_DEPTH);
                    })
                    .detach();

                done_rx.recv().unwrap();
            });
        });
        rt.shutdown().expect("shutdown");
    }
}

mod spawn_many {
    use super::*;
    use criterion::Bencher;
    use std::sync::{
        atomic::{AtomicUsize, Ordering::Relaxed},
        mpsc,
        Arc,
    };

    fn cbwp_rt() -> impl FuturesExecutor {
        executors::crossbeam_workstealing_pool::small_pool(8)
    }

    fn cbcp_rt() -> impl FuturesExecutor {
        executors::crossbeam_channel_pool::ThreadPool::new(8)
    }

    pub fn function_cbwp(b: &mut Bencher) {
        let rt = cbwp_rt();
        spawn_many_function(b, rt);
    }

    pub fn async_cbwp(b: &mut Bencher) {
        let rt = cbwp_rt();
        spawn_many_async(b, rt);
    }

    pub fn function_cbcp(b: &mut Bencher) {
        let rt = cbcp_rt();
        spawn_many_function(b, rt);
    }

    pub fn async_cbcp(b: &mut Bencher) {
        let rt = cbcp_rt();
        spawn_many_async(b, rt);
    }

    fn spawn_many_function(b: &mut Bencher, rt: impl FuturesExecutor) {
        let (tx, rx) = mpsc::sync_channel(1);
        let rem = Arc::new(AtomicUsize::new(0));

        b.iter(|| {
            rem.store(SPAWN_NUM, Relaxed);

            for _ in 0..SPAWN_NUM {
                let tx = tx.clone();
                let rem = rem.clone();

                rt.execute(move || {
                    if 1 == rem.fetch_sub(1, Relaxed) {
                        tx.send(()).unwrap();
                    }
                });
            }

            rx.recv().unwrap();
        });

        rt.shutdown().expect("shutdown");
    }

    fn spawn_many_async(b: &mut Bencher, rt: impl FuturesExecutor) {
        let (tx, rx) = mpsc::sync_channel(1);
        let rem = Arc::new(AtomicUsize::new(0));

        b.iter(|| {
            rem.store(SPAWN_NUM, Relaxed);

            futures::executor::block_on(async {
                for _ in 0..SPAWN_NUM {
                    let tx = tx.clone();
                    let rem = rem.clone();

                    rt.spawn(async move {
                        if 1 == rem.fetch_sub(1, Relaxed) {
                            tx.send(()).unwrap();
                        }
                    })
                    .detach();
                }

                rx.recv().unwrap();
            });
        });

        rt.shutdown().expect("shutdown");
    }
}

// fn yield_many(b: &mut Bencher) {
//     const NUM_YIELD: usize = 1_000;
//     const TASKS: usize = 200;

//     let rt = rt();

//     let (tx, rx) = mpsc::sync_channel(TASKS);

//     b.iter(move || {
//         for _ in 0..TASKS {
//             let tx = tx.clone();

//             rt.spawn(async move {
//                 for _ in 0..NUM_YIELD {
//                     tokio::task::yield_now().await;
//                 }

//                 tx.send(()).unwrap();
//             });
//         }

//         for _ in 0..TASKS {
//             let _ = rx.recv().unwrap();
//         }
//     });
// }

mod ping_pong {

    use super::*;
    use criterion::Bencher;
    use futures::channel::oneshot;
    use std::{
        sync::{
            atomic::{AtomicUsize, Ordering::SeqCst},
            mpsc,
            Arc,
        },
        time::Duration,
    };

    fn cbwp_rt() -> impl FuturesExecutor {
        executors::crossbeam_workstealing_pool::small_pool(4)
    }

    fn cbcp_rt() -> impl FuturesExecutor {
        executors::crossbeam_channel_pool::ThreadPool::new(4)
    }

    // pub fn local_function_cbwp(b: &mut Bencher) {
    //     let rt = cbwp_rt();
    //     chained_spawn_fun(b, rt);
    // }

    // pub fn no_local_function_cbwp(b: &mut Bencher) {
    //     let rt = cbwp_rt();
    //     chained_spawn_no_local(b, rt);
    // }

    pub fn async_cbwp(b: &mut Bencher) {
        let rt = cbwp_rt();
        ping_pong_async(b, rt);
    }

    // pub fn no_local_function_cbcp(b: &mut Bencher) {
    //     let rt = cbcp_rt();
    //     chained_spawn_no_local(b, rt);
    // }

    pub fn async_cbcp(b: &mut Bencher) {
        let rt = cbcp_rt();
        ping_pong_async(b, rt);
    }

    fn ping_pong_async(b: &mut Bencher, rt: impl FuturesExecutor) {
        let (done_tx, done_rx) = mpsc::sync_channel(1);
        let rem = Arc::new(AtomicUsize::new(0));

        let mut count = 0u64;

        b.iter(|| {
            let done_tx = done_tx.clone();
            let rem = rem.clone();
            let outer_rem = rem.clone();
            rem.store(PINGS_NUM, SeqCst);
            count += 1u64;
            let rt_new = rt.clone();
            rt.spawn(async move {
                for _i in 0..PINGS_NUM {
                    let rem = rem.clone();
                    let done_tx = done_tx.clone();
                    let rt_new2 = rt_new.clone();
                    rt_new
                        .spawn(async move {
                            let (tx1, rx1) = oneshot::channel();
                            let (tx2, rx2) = oneshot::channel();

                            rt_new2
                                .spawn(async move {
                                    rx1.await.unwrap();
                                    tx2.send(()).unwrap();
                                })
                                .detach();

                            tx1.send(()).unwrap();
                            rx2.await.unwrap();

                            let res = rem.fetch_sub(1, SeqCst);
                            if 1 == res {
                                done_tx.try_send(()).expect("done should have sent");
                            }
                        })
                        .detach();
                }
            })
            .detach();

            let res = done_rx.recv_timeout(Duration::from_millis(5000));
            assert!(
                res.is_ok(),
                "done_rx timeouted within 5s. Remaining={}",
                outer_rem.load(SeqCst)
            );
        });
        rt.shutdown().expect("shutdown");
    }
}

criterion_group!(
    scheduler,
    chained_spawn_benchmark,
    spawn_many_benchmark,
    ping_pong_benchmark
);
criterion_main!(scheduler);