summaryrefslogtreecommitdiffstats
path: root/third_party/rust/futures-util/benches/bilock.rs
blob: 013f3351e43df521dc5637ec72266bce8b3ab512 (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
#![feature(test)]
#![cfg(feature = "bilock")]

extern crate test;

use futures::task::Poll;
use futures_test::task::noop_context;
use futures_util::lock::BiLock;

use crate::test::Bencher;

#[bench]
fn contended(b: &mut Bencher) {
    let mut context = noop_context();

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

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

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

            drop(x_guard);

            let y_guard = match y.poll_lock(&mut context) {
                Poll::Ready(guard) => guard,
                _ => panic!(),
            };

            drop(y_guard);
        }
        (x, y)
    });
}

#[bench]
fn lock_unlock(b: &mut Bencher) {
    let mut context = noop_context();

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

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

            drop(x_guard);

            let y_guard = match y.poll_lock(&mut context) {
                Poll::Ready(guard) => guard,
                _ => panic!(),
            };

            drop(y_guard);
        }
        (x, y)
    })
}