From 177522cd43a150d215e4c0b8c31fdfb2cf957ed8 Mon Sep 17 00:00:00 2001 From: Stefan Sydow Date: Wed, 5 May 2021 21:49:00 +0200 Subject: [PATCH] benchmark: add file reading benchmarks (#3013) --- benches/Cargo.toml | 10 ++++- benches/fs.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 benches/fs.rs diff --git a/benches/Cargo.toml b/benches/Cargo.toml index de6c6c33b..34760eabc 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -5,9 +5,12 @@ publish = false edition = "2018" [dependencies] -tokio = { version = "1.0.0", path = "../tokio", features = ["full"] } +tokio = { version = "1.5.0", path = "../tokio", features = ["full"] } bencher = "0.1.5" +[dev-dependencies] +tokio-util = { version = "0.6.6", path = "../tokio-util", features = ["full"] } + [target.'cfg(unix)'.dependencies] libc = "0.2.42" @@ -41,3 +44,8 @@ harness = false name = "signal" path = "signal.rs" harness = false + +[[bench]] +name = "fs" +path = "fs.rs" +harness = false diff --git a/benches/fs.rs b/benches/fs.rs new file mode 100644 index 000000000..f095d5516 --- /dev/null +++ b/benches/fs.rs @@ -0,0 +1,103 @@ +#![cfg(unix)] + +use tokio::stream::StreamExt; + +use tokio::fs::File; +use tokio::io::AsyncReadExt; +use tokio_util::codec::{BytesCodec, FramedRead /*FramedWrite*/}; + +use bencher::{benchmark_group, benchmark_main, Bencher}; + +use std::fs::File as StdFile; +use std::io::Read as StdRead; + +fn rt() -> tokio::runtime::Runtime { + tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .build() + .unwrap() +} + +const BLOCK_COUNT: usize = 1_000; + +const BUFFER_SIZE: usize = 4096; +const DEV_ZERO: &'static str = "/dev/zero"; + +fn async_read_codec(b: &mut Bencher) { + let rt = rt(); + + b.iter(|| { + let task = || async { + let file = File::open(DEV_ZERO).await.unwrap(); + let mut input_stream = FramedRead::with_capacity(file, BytesCodec::new(), BUFFER_SIZE); + + for _i in 0..BLOCK_COUNT { + let _bytes = input_stream.next().await.unwrap(); + } + }; + + rt.block_on(task()); + }); +} + +fn async_read_buf(b: &mut Bencher) { + let rt = rt(); + + b.iter(|| { + let task = || async { + let mut file = File::open(DEV_ZERO).await.unwrap(); + let mut buffer = [0u8; BUFFER_SIZE]; + + for _i in 0..BLOCK_COUNT { + let count = file.read(&mut buffer).await.unwrap(); + if count == 0 { + break; + } + } + }; + + rt.block_on(task()); + }); +} + +fn async_read_std_file(b: &mut Bencher) { + let rt = rt(); + + let task = || async { + let mut file = tokio::task::block_in_place(|| Box::pin(StdFile::open(DEV_ZERO).unwrap())); + + for _i in 0..BLOCK_COUNT { + let mut buffer = [0u8; BUFFER_SIZE]; + let mut file_ref = file.as_mut(); + + tokio::task::block_in_place(move || { + file_ref.read_exact(&mut buffer).unwrap(); + }); + } + }; + + b.iter(|| { + rt.block_on(task()); + }); +} + +fn sync_read(b: &mut Bencher) { + b.iter(|| { + let mut file = StdFile::open(DEV_ZERO).unwrap(); + let mut buffer = [0u8; BUFFER_SIZE]; + + for _i in 0..BLOCK_COUNT { + file.read_exact(&mut buffer).unwrap(); + } + }); +} + +benchmark_group!( + file, + async_read_std_file, + async_read_buf, + async_read_codec, + sync_read +); + +benchmark_main!(file);