Skip to content

Instantly share code, notes, and snippets.

@miquels
Created February 9, 2021 15:09
Show Gist options
  • Save miquels/8576d1394d3b26c6811f4fc1e7886a1c to your computer and use it in GitHub Desktop.
Save miquels/8576d1394d3b26c6811f4fc1e7886a1c to your computer and use it in GitHub Desktop.
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