-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
106 lines (83 loc) · 2.67 KB
/
main.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
use std::time::{Duration, Instant};
use futures::stream::{FuturesOrdered, StreamExt};
use futures::Future;
use tokio_util::task::LocalPoolHandle;
async fn run_test<Fut>(f: fn() -> Fut)
where
Fut: 'static + Send + Future<Output = ()>,
{
let pool = LocalPoolHandle::new(num_cpus::get());
for round in 1..=5 {
println!(" Round: {}", round);
// Futures Ordered.
{
let start_time = Instant::now();
let mut furs = FuturesOrdered::new();
for _ in 0..1_000_000 {
furs.push(f());
}
while furs.next().await.is_some() {}
println!(
" futures ordered: {}ms",
start_time.elapsed().as_millis()
);
}
// spawn_local
{
let start_time = Instant::now();
pool.spawn_pinned(move || async move {
let mut handles = Vec::with_capacity(1_000_000);
for _ in 0..1_000_000 {
handles.push(tokio::task::spawn_local(f()));
}
for handle in handles.into_iter() {
handle.await.expect("task failed.");
}
})
.await
.expect("failed to complete.");
println!(
" spawn local: {}ms",
start_time.elapsed().as_millis()
);
}
// spawn_pinned to local pool
{
let start_time = Instant::now();
let mut handles = Vec::with_capacity(1_000_000);
for _ in 0..1_000_000 {
handles.push(pool.spawn_pinned(f));
}
for handle in handles.into_iter() {
handle.await.expect("task failed.");
}
println!(
" spawn pinned: {}ms",
start_time.elapsed().as_millis()
);
}
// Spawn with default runtime.
{
let start_time = Instant::now();
let mut handles = Vec::with_capacity(1_000_000);
for _ in 0..1_000_000 {
handles.push(tokio::task::spawn(f()));
}
for handle in handles.into_iter() {
handle.await.expect("task failed.");
}
println!(
" spawn: {}ms",
start_time.elapsed().as_millis()
);
}
println!();
}
}
#[tokio::main]
async fn main() {
println!("Testing Ready Future...");
run_test(|| async {}).await;
println!("Testing sleep(Duration::ZERO) Future...");
run_test(|| tokio::time::sleep(Duration::ZERO)).await;
}