-
-
Save Mon-ius/99a876e3770f6fe6610b399d6c0b88b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/dev/urandom").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 | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment