summaryrefslogtreecommitdiffstats
path: root/third_party/rust/oneshot-uniffi/benches/benches.rs
blob: 438d46a498c48c718c4b0d2ca1befc9ecddc762c (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
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::mem;
use std::time::{Duration, Instant};

criterion_group!(benches, bench);
criterion_main!(benches);

macro_rules! bench_send_and_recv {
    ($c:expr, $($type:ty => $value:expr);+) => {
        // Sanity check that all $values are of $type.
        $(let _: $type = $value;)*
        {
            let mut group = $c.benchmark_group("create_channel");
            $(group.bench_function(stringify!($type), |b| {
                b.iter(oneshot::channel::<$type>)
            });)*
            group.finish();
        }
        {
            let mut group = $c.benchmark_group("create_and_send");
            $(group.bench_function(stringify!($type), |b| {
                b.iter(|| {
                    let (sender, _receiver) = oneshot::channel();
                    sender.send(black_box($value)).unwrap()
                });
            });)*
            group.finish();
        }
        {
            let mut group = $c.benchmark_group("create_and_send_on_closed");
            $(group.bench_function(stringify!($type), |b| {
                b.iter(|| {
                    let (sender, _) = oneshot::channel();
                    sender.send(black_box($value)).unwrap_err()
                });
            });)*
            group.finish();
        }
        {
            let mut group = $c.benchmark_group("create_send_and_recv");
            $(group.bench_function(stringify!($type), |b| {
                b.iter(|| {
                    let (sender, receiver) = oneshot::channel();
                    sender.send(black_box($value)).unwrap();
                    receiver.recv().unwrap()
                });
            });)*
            group.finish();
        }
        {
            let mut group = $c.benchmark_group("create_send_and_recv_ref");
            $(group.bench_function(stringify!($type), |b| {
                b.iter(|| {
                    let (sender, receiver) = oneshot::channel();
                    sender.send(black_box($value)).unwrap();
                    receiver.recv_ref().unwrap()
                });
            });)*
            group.finish();
        }
    };
}

fn bench(c: &mut Criterion) {
    bench_send_and_recv!(c,
        () => ();
        u8 => 7u8;
        usize => 9876usize;
        u128 => 1234567u128;
        [u8; 64] => [0b10101010u8; 64];
        [u8; 4096] => [0b10101010u8; 4096]
    );

    bench_try_recv(c);
    bench_recv_deadline_now(c);
    bench_recv_timeout_zero(c);
}

fn bench_try_recv(c: &mut Criterion) {
    let (sender, receiver) = oneshot::channel::<u128>();
    c.bench_function("try_recv_empty", |b| {
        b.iter(|| receiver.try_recv().unwrap_err())
    });
    mem::drop(sender);
    c.bench_function("try_recv_empty_closed", |b| {
        b.iter(|| receiver.try_recv().unwrap_err())
    });
}

fn bench_recv_deadline_now(c: &mut Criterion) {
    let now = Instant::now();
    {
        let (_sender, receiver) = oneshot::channel::<u128>();
        c.bench_function("recv_deadline_now", |b| {
            b.iter(|| receiver.recv_deadline(now).unwrap_err())
        });
    }
    {
        let (sender, receiver) = oneshot::channel::<u128>();
        mem::drop(sender);
        c.bench_function("recv_deadline_now_closed", |b| {
            b.iter(|| receiver.recv_deadline(now).unwrap_err())
        });
    }
}

fn bench_recv_timeout_zero(c: &mut Criterion) {
    let zero = Duration::from_nanos(0);
    {
        let (_sender, receiver) = oneshot::channel::<u128>();
        c.bench_function("recv_timeout_zero", |b| {
            b.iter(|| receiver.recv_timeout(zero).unwrap_err())
        });
    }
    {
        let (sender, receiver) = oneshot::channel::<u128>();
        mem::drop(sender);
        c.bench_function("recv_timeout_zero_closed", |b| {
            b.iter(|| receiver.recv_timeout(zero).unwrap_err())
        });
    }
}