Skip to content

Instantly share code, notes, and snippets.

@sirupsen
Last active February 2, 2020 17:45
Embed
What would you like to do?
use std::{fs::OpenOptions, io::Result};
use std::io::{Read, Write, Seek, SeekFrom};
use std::slice;
use std::time::Instant;
const BUF_SIZE: usize = 1024 * 32;
const FILE_SIZE: usize = 1024 * 1024 * 512;
const QUEUE_DEPTH: usize = 32;
fn main() -> Result<()> {
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open("file")
.unwrap();
// do this without loading it all in memory
let buf: Vec<u8> = vec![1; FILE_SIZE];
file.write_all(&buf).unwrap();
file.sync_data().unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let mut buffer: [u8; BUF_SIZE] = [0; BUF_SIZE];
let mut sum: u64 = 0;
let mut instant = Instant::now();
loop {
let n = file.read(&mut buffer).unwrap();
for i in &buffer[0..n] {
sum += *i as u64;
}
if n < BUF_SIZE {
break
}
}
println!("read(2) done in {}ms, checksum: {}", instant.elapsed().as_millis(), sum);
let mut buffers: Vec<Vec<u8>> = vec![vec![0; BUF_SIZE]; QUEUE_DEPTH];
let ring = rio::new().unwrap();
let mut bytes_left: usize = FILE_SIZE;
let mut offset: usize = 0;
let mut done: bool = false;
instant = Instant::now();
sum = 0;
while !done {
let ptr = buffers.as_mut_ptr();
let mut completions = vec![];
for i in 0..QUEUE_DEPTH {
unsafe {
let buf = &slice::from_raw_parts_mut(ptr.offset(i as isize), 1)[0];
completions.push(ring.read_at(&file, buf, offset as u64))
}
if bytes_left > BUF_SIZE {
bytes_left -= BUF_SIZE;
offset += BUF_SIZE;
} else {
done = true;
break
}
}
for completion in completions.into_iter() {
completion.wait().unwrap();
}
for buf in &buffers {
for i in buf {
sum += *i as u64;
}
}
}
println!("io_uring(2) done in {}ms, checksum: {}", instant.elapsed().as_millis(), sum);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment