-
Notifications
You must be signed in to change notification settings - Fork 0
/
rust_block_in_place.rs
47 lines (43 loc) · 1.12 KB
/
rust_block_in_place.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
use std::{
fs::File,
io::{Read, Write},
time::Instant,
};
use tokio::task::{self, JoinHandle};
async fn compute() {
let handles: Vec<JoinHandle<_>> = (0..1000)
.map(|_| {
tokio::spawn(async move {
let mut buffer = [0; 10];
{
task::block_in_place(move || {
let mut dev_urandom = File::open("10-bytes").unwrap();
dev_urandom.read(&mut buffer).unwrap();
});
}
task::block_in_place(move || {
let mut dev_null = File::create("/dev/null").unwrap();
dev_null.write(&mut buffer).unwrap();
});
})
})
.collect();
for handle in handles {
handle.await.unwrap();
}
}
#[tokio::main]
async fn main() {
// warmup
compute().await;
let before = Instant::now();
for _ in 0usize..1000 {
compute().await;
}
let elapsed = before.elapsed();
println!(
"{:?} total, {:?} avg per iteration",
elapsed,
elapsed / 1000
);
}