summaryrefslogtreecommitdiffstats
path: root/benches/sync_mpsc.rs
blob: 3f7e3fcaa27204c082b5a3edd52f0e679927b47c (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
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
use bencher::{black_box, Bencher};
use tokio::sync::mpsc;

type Medium = [usize; 64];
type Large = [Medium; 64];

fn rt() -> tokio::runtime::Runtime {
    tokio::runtime::Builder::new_multi_thread()
        .worker_threads(6)
        .build()
        .unwrap()
}

fn create_1_medium(b: &mut Bencher) {
    b.iter(|| {
        black_box(&mpsc::channel::<Medium>(1));
    });
}

fn create_100_medium(b: &mut Bencher) {
    b.iter(|| {
        black_box(&mpsc::channel::<Medium>(100));
    });
}

fn create_100_000_medium(b: &mut Bencher) {
    b.iter(|| {
        black_box(&mpsc::channel::<Medium>(100_000));
    });
}

fn send_medium(b: &mut Bencher) {
    b.iter(|| {
        let (tx, mut rx) = mpsc::channel::<Medium>(1000);

        let _ = tx.try_send([0; 64]);

        rx.try_recv().unwrap();
    });
}

fn send_large(b: &mut Bencher) {
    b.iter(|| {
        let (tx, mut rx) = mpsc::channel::<Large>(1000);

        let _ = tx.try_send([[0; 64]; 64]);

        rx.try_recv().unwrap();
    });
}

fn contention_bounded(b: &mut Bencher) {
    let rt = rt();

    b.iter(|| {
        rt.block_on(async move {
            let (tx, mut rx) = mpsc::channel::<usize>(1_000_000);

            for _ in 0..5 {
                let tx = tx.clone();
                tokio::spawn(async move {
                    for i in 0..1000 {
                        tx.send(i).await.unwrap();
                    }
                });
            }

            for _ in 0..1_000 * 5 {
                let _ = rx.recv().await;
            }
        })
    });
}

fn contention_bounded_full(b: &mut Bencher) {
    let rt = rt();

    b.iter(|| {
        rt.block_on(async move {
            let (tx, mut rx) = mpsc::channel::<usize>(100);

            for _ in 0..5 {
                let tx = tx.clone();
                tokio::spawn(async move {
                    for i in 0..1000 {
                        tx.send(i).await.unwrap();
                    }
                });
            }

            for _ in 0..1_000 * 5 {
                let _ = rx.recv().await;
            }
        })
    });
}

fn contention_unbounded(b: &mut Bencher) {
    let rt = rt();

    b.iter(|| {
        rt.block_on(async move {
            let (tx, mut rx) = mpsc::unbounded_channel::<usize>();

            for _ in 0..5 {
                let tx = tx.clone();
                tokio::spawn(async move {
                    for i in 0..1000 {
                        tx.send(i).unwrap();
                    }
                });
            }

            for _ in 0..1_000 * 5 {
                let _ = rx.recv().await;
            }
        })
    });
}

fn uncontented_bounded(b: &mut Bencher) {
    let rt = rt();

    b.iter(|| {
        rt.block_on(async move {
            let (tx, mut rx) = mpsc::channel::<usize>(1_000_000);

            for i in 0..5000 {
                tx.send(i).await.unwrap();
            }

            for _ in 0..5_000 {
                let _ = rx.recv().await;
            }
        })
    });
}

fn uncontented_unbounded(b: &mut Bencher) {
    let rt = rt();

    b.iter(|| {
        rt.block_on(async move {
            let (tx, mut rx) = mpsc::unbounded_channel::<usize>();

            for i in 0..5000 {
                tx.send(i).unwrap();
            }

            for _ in 0..5_000 {
                let _ = rx.recv().await;
            }
        })
    });
}

bencher::benchmark_group!(
    create,
    create_1_medium,
    create_100_medium,
    create_100_000_medium
);

bencher::benchmark_group!(send, send_medium, send_large);

bencher::benchmark_group!(
    contention,
    contention_bounded,
    contention_bounded_full,
    contention_unbounded,
    uncontented_bounded,
    uncontented_unbounded
);

bencher::benchmark_main!(create, send, contention);