summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-threadpool/benches/blocking.rs
blob: bc5121545a654393c84e278e8cb75c3d3a83da02 (plain)
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
#![feature(test)]

extern crate futures;
extern crate rand;
extern crate test;
extern crate threadpool;
extern crate tokio_threadpool;

const ITER: usize = 1_000;

mod blocking {
    use super::*;

    use futures::future::*;
    use tokio_threadpool::{blocking, Builder};

    #[bench]
    fn cpu_bound(b: &mut test::Bencher) {
        let pool = Builder::new().pool_size(2).max_blocking(20).build();

        b.iter(|| {
            let count_down = Arc::new(CountDown::new(::ITER));

            for _ in 0..::ITER {
                let count_down = count_down.clone();

                pool.spawn(lazy(move || {
                    poll_fn(|| blocking(|| perform_complex_computation()).map_err(|_| panic!()))
                        .and_then(move |_| {
                            // Do something with the value
                            count_down.dec();
                            Ok(())
                        })
                }));
            }

            count_down.wait();
        })
    }
}

mod message_passing {
    use super::*;

    use futures::future::*;
    use futures::sync::oneshot;
    use tokio_threadpool::Builder;

    #[bench]
    fn cpu_bound(b: &mut test::Bencher) {
        let pool = Builder::new().pool_size(2).max_blocking(20).build();

        let blocking = threadpool::ThreadPool::new(20);

        b.iter(|| {
            let count_down = Arc::new(CountDown::new(::ITER));

            for _ in 0..::ITER {
                let count_down = count_down.clone();
                let blocking = blocking.clone();

                pool.spawn(lazy(move || {
                    // Create a channel to receive the return value.
                    let (tx, rx) = oneshot::channel();

                    // Spawn a task on the blocking thread pool to process the
                    // computation.
                    blocking.execute(move || {
                        let res = perform_complex_computation();
                        tx.send(res).unwrap();
                    });

                    rx.and_then(move |_| {
                        count_down.dec();
                        Ok(())
                    })
                    .map_err(|_| panic!())
                }));
            }

            count_down.wait();
        })
    }
}

fn perform_complex_computation() -> usize {
    use rand::*;

    // Simulate a CPU heavy computation
    let mut rng = rand::thread_rng();
    rng.gen()
}

// Util for waiting until the tasks complete

use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::*;
use std::sync::*;

struct CountDown {
    rem: AtomicUsize,
    mutex: Mutex<()>,
    condvar: Condvar,
}

impl CountDown {
    fn new(rem: usize) -> Self {
        CountDown {
            rem: AtomicUsize::new(rem),
            mutex: Mutex::new(()),
            condvar: Condvar::new(),
        }
    }

    fn dec(&self) {
        let prev = self.rem.fetch_sub(1, AcqRel);

        if prev != 1 {
            return;
        }

        let _lock = self.mutex.lock().unwrap();
        self.condvar.notify_all();
    }

    fn wait(&self) {
        let mut lock = self.mutex.lock().unwrap();

        loop {
            if self.rem.load(Acquire) == 0 {
                return;
            }

            lock = self.condvar.wait(lock).unwrap();
        }
    }
}