-
Notifications
You must be signed in to change notification settings - Fork 9
/
z_async_all.rs
86 lines (73 loc) · 1.94 KB
/
z_async_all.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
async fn mpmc<T: BenchType + 'static>(cap: Option<usize>) {
let (tx, rx) = new(cap);
let mut list = Vec::new();
for _ in 0..THREADS {
let tx = tx.clone();
let h = tokio::spawn(async move {
for i in 1..MESSAGES / THREADS + 1 {
tx.send(T::new(i)).await.unwrap();
}
});
list.push(h);
}
for _ in 0..THREADS {
let rx = rx.clone();
let h = tokio::spawn(async move {
for _ in 0..MESSAGES / THREADS {
rx.recv().await.unwrap().test()
}
});
list.push(h);
}
for h in list {
h.await.unwrap();
}
}
async fn mpsc<T: BenchType + 'static>(cap: Option<usize>) {
let (tx, rx) = new(cap);
let mut list = Vec::new();
for _ in 0..THREADS {
let tx = tx.clone();
let h = tokio::spawn(async move {
for i in 1..MESSAGES / THREADS + 1 {
tx.send(T::new(i)).await.unwrap();
}
});
list.push(h);
}
list.push(tokio::spawn(async move {
for _ in 0..MESSAGES {
rx.recv().await.unwrap().test()
}
}));
for h in list {
h.await.unwrap();
}
}
async fn seq<T: BenchType + 'static>(cap: Option<usize>) {
let (tx, rx) = new(cap);
let h = tokio::spawn(async move {
for i in 1..MESSAGES + 1 {
tx.send(T::new(i)).await.unwrap();
}
for _ in 0..MESSAGES {
rx.recv().await.unwrap().test()
}
});
h.await.unwrap();
}
async fn spsc<T: BenchType + 'static>(cap: Option<usize>) {
let (tx, rx) = new(cap);
let htx = tokio::spawn(async move {
for i in 1..MESSAGES + 1 {
tx.send(T::new(i)).await.unwrap();
}
});
let hrx = tokio::spawn(async move {
for _ in 0..MESSAGES {
rx.recv().await.unwrap().test()
}
});
htx.await.unwrap();
hrx.await.unwrap();
}