-
Notifications
You must be signed in to change notification settings - Fork 9
/
mpsc.rs
148 lines (117 loc) · 4.08 KB
/
mpsc.rs
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
use std::sync::mpsc;
std::include!("z_types.rs");
std::include!("settings.rs");
std::include!("z_run.rs");
fn seq_async<T: BenchType>() {
let (tx, rx) = mpsc::channel();
for i in 1..MESSAGES + 1 {
tx.send(T::new(i)).unwrap();
}
for _ in 0..MESSAGES {
rx.recv().unwrap().test();
}
}
fn seq_sync<T: BenchType>(cap: usize) {
let (tx, rx) = mpsc::sync_channel(cap);
for i in 1..MESSAGES + 1 {
tx.send(T::new(i)).unwrap();
}
for _ in 0..MESSAGES {
rx.recv().unwrap().test();
}
}
fn spsc_async<T: BenchType>() {
let (tx, rx) = mpsc::channel();
crossbeam::scope(|scope| {
scope.spawn(move |_| {
for i in 1..MESSAGES + 1 {
tx.send(T::new(i)).unwrap();
}
});
for _ in 0..MESSAGES {
rx.recv().unwrap().test();
}
})
.unwrap();
}
fn spsc_sync<T: BenchType>(cap: usize) {
let (tx, rx) = mpsc::sync_channel(cap);
crossbeam::scope(|scope| {
scope.spawn(move |_| {
for i in 1..MESSAGES + 1 {
tx.send(T::new(i)).unwrap();
}
});
for _ in 0..MESSAGES {
rx.recv().unwrap().test();
}
})
.unwrap();
}
fn mpsc_async<T: BenchType>() {
let (tx, rx) = mpsc::channel();
crossbeam::scope(|scope| {
for _ in 0..THREADS {
let tx = tx.clone();
scope.spawn(move |_| {
for i in 1..MESSAGES / THREADS + 1 {
tx.send(T::new(i)).unwrap();
}
});
}
for _ in 0..MESSAGES {
rx.recv().unwrap().test();
}
})
.unwrap();
}
fn mpsc_sync<T: BenchType>(cap: usize) {
let (tx, rx) = mpsc::sync_channel(cap);
crossbeam::scope(|scope| {
for _ in 0..THREADS {
let tx = tx.clone();
scope.spawn(move |_| {
for i in 1..MESSAGES / THREADS + 1 {
tx.send(T::new(i)).unwrap();
}
});
}
for _ in 0..MESSAGES {
rx.recv().unwrap().test();
}
})
.unwrap();
}
fn main() {
println!("std::mpsc");
run!("bounded0_mpsc(empty)", mpsc_sync::<BenchEmpty>(0));
run!("bounded0_spsc(empty)", spsc_sync::<BenchEmpty>(0));
run!("bounded1_mpsc(empty)", mpsc_sync::<BenchEmpty>(1));
run!("bounded1_spsc(empty)", spsc_sync::<BenchEmpty>(1));
run!("bounded_mpsc(empty)", mpsc_sync::<BenchEmpty>(MESSAGES));
run!("bounded_seq(empty)", seq_sync::<BenchEmpty>(MESSAGES));
run!("bounded_spsc(empty)", spsc_sync::<BenchEmpty>(MESSAGES));
run!("unbounded_mpsc(empty)", mpsc_async::<BenchEmpty>());
run!("unbounded_seq(empty)", seq_async::<BenchEmpty>());
run!("unbounded_spsc(empty)", spsc_async::<BenchEmpty>());
run!("bounded0_mpsc(usize)", mpsc_sync::<BenchUsize>(0));
run!("bounded0_spsc(usize)", spsc_sync::<BenchUsize>(0));
run!("bounded1_mpsc(usize)", mpsc_sync::<BenchUsize>(1));
run!("bounded1_spsc(usize)", spsc_sync::<BenchUsize>(1));
run!("bounded_mpsc(usize)", mpsc_sync::<BenchUsize>(MESSAGES));
run!("bounded_seq(usize)", seq_sync::<BenchUsize>(MESSAGES));
run!("bounded_spsc(usize)", spsc_sync::<BenchUsize>(MESSAGES));
run!("unbounded_mpsc(usize)", mpsc_async::<BenchUsize>());
run!("unbounded_seq(usize)", seq_async::<BenchUsize>());
run!("unbounded_spsc(usize)", spsc_async::<BenchUsize>());
run!("bounded0_mpsc(big)", mpsc_sync::<BenchFixedArray>(0));
run!("bounded0_spsc(big)", spsc_sync::<BenchFixedArray>(0));
run!("bounded1_mpsc(big)", mpsc_sync::<BenchFixedArray>(1));
run!("bounded1_spsc(big)", spsc_sync::<BenchFixedArray>(1));
run!("bounded_mpsc(big)", mpsc_sync::<BenchFixedArray>(MESSAGES));
run!("bounded_seq(big)", seq_sync::<BenchFixedArray>(MESSAGES));
run!("bounded_spsc(big)", spsc_sync::<BenchFixedArray>(MESSAGES));
run!("unbounded_mpsc(big)", mpsc_async::<BenchFixedArray>());
run!("unbounded_seq(big)", seq_async::<BenchFixedArray>());
run!("unbounded_spsc(big)", spsc_async::<BenchFixedArray>());
}