aboutsummaryrefslogtreecommitdiff
path: root/benches_disabled/bilock.rs
blob: 417f75d31e7222b49ec3b036a578aa2676139c00 (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
#![feature(test)]

#[cfg(feature = "bilock")]
mod bench {
    use futures::executor::LocalPool;
    use futures::task::{Context, Waker};
    use futures_util::lock::BiLock;
    use futures_util::lock::BiLockAcquire;
    use futures_util::lock::BiLockAcquired;
    use futures_util::task::ArcWake;

    use std::sync::Arc;
    use test::Bencher;

    fn notify_noop() -> Waker {
        struct Noop;

        impl ArcWake for Noop {
            fn wake(_: &Arc<Self>) {}
        }

        ArcWake::into_waker(Arc::new(Noop))
    }

    /// Pseudo-stream which simply calls `lock.poll()` on `poll`
    struct LockStream {
        lock: BiLockAcquire<u32>,
    }

    impl LockStream {
        fn new(lock: BiLock<u32>) -> Self {
            Self { lock: lock.lock() }
        }

        /// Release a lock after it was acquired in `poll`,
        /// so `poll` could be called again.
        fn release_lock(&mut self, guard: BiLockAcquired<u32>) {
            self.lock = guard.unlock().lock()
        }
    }

    impl Stream for LockStream {
        type Item = BiLockAcquired<u32>;
        type Error = ();

        fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>, Self::Error> {
            self.lock.poll(cx).map(|a| a.map(Some))
        }
    }

    #[bench]
    fn contended(b: &mut Bencher) {
        let pool = LocalPool::new();
        let mut exec = pool.executor();
        let waker = notify_noop();
        let mut map = task::LocalMap::new();
        let mut waker = task::Context::new(&mut map, &waker, &mut exec);

        b.iter(|| {
            let (x, y) = BiLock::new(1);

            let mut x = LockStream::new(x);
            let mut y = LockStream::new(y);

            for _ in 0..1000 {
                let x_guard = match x.poll_next(&mut waker) {
                    Ok(Poll::Ready(Some(guard))) => guard,
                    _ => panic!(),
                };

                // Try poll second lock while first lock still holds the lock
                match y.poll_next(&mut waker) {
                    Ok(Poll::Pending) => (),
                    _ => panic!(),
                };

                x.release_lock(x_guard);

                let y_guard = match y.poll_next(&mut waker) {
                    Ok(Poll::Ready(Some(guard))) => guard,
                    _ => panic!(),
                };

                y.release_lock(y_guard);
            }
            (x, y)
        });
    }

    #[bench]
    fn lock_unlock(b: &mut Bencher) {
        let pool = LocalPool::new();
        let mut exec = pool.executor();
        let waker = notify_noop();
        let mut map = task::LocalMap::new();
        let mut waker = task::Context::new(&mut map, &waker, &mut exec);

        b.iter(|| {
            let (x, y) = BiLock::new(1);

            let mut x = LockStream::new(x);
            let mut y = LockStream::new(y);

            for _ in 0..1000 {
                let x_guard = match x.poll_next(&mut waker) {
                    Ok(Poll::Ready(Some(guard))) => guard,
                    _ => panic!(),
                };

                x.release_lock(x_guard);

                let y_guard = match y.poll_next(&mut waker) {
                    Ok(Poll::Ready(Some(guard))) => guard,
                    _ => panic!(),
                };

                y.release_lock(y_guard);
            }
            (x, y)
        })
    }
}