Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Softsapiens/6de5291cb39426440400fe53ba4608d5 to your computer and use it in GitHub Desktop.
Save Softsapiens/6de5291cb39426440400fe53ba4608d5 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