From 686793ae4950768591ba8f58783c92d9415dccbd Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 6 Oct 2022 21:29:44 -0400 Subject: [PATCH] Add a BufReader benchmark for reads smaller than the buffer --- collector/runtime-benchmarks/Cargo.lock | 14 ++++++++ collector/runtime-benchmarks/Cargo.toml | 2 +- .../runtime-benchmarks/bufreader/Cargo.toml | 10 ++++++ .../runtime-benchmarks/bufreader/src/main.rs | 34 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 collector/runtime-benchmarks/bufreader/Cargo.toml create mode 100644 collector/runtime-benchmarks/bufreader/src/main.rs diff --git a/collector/runtime-benchmarks/Cargo.lock b/collector/runtime-benchmarks/Cargo.lock index cf977603a..3e972a4b5 100644 --- a/collector/runtime-benchmarks/Cargo.lock +++ b/collector/runtime-benchmarks/Cargo.lock @@ -64,6 +64,14 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bufreader" +version = "0.1.0" +dependencies = [ + "benchlib", + "snap", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -351,6 +359,12 @@ dependencies = [ "serde", ] +[[package]] +name = "snap" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45456094d1983e2ee2a18fdfebce3189fa451699d0502cb8e3b49dba5ba41451" + [[package]] name = "strsim" version = "0.10.0" diff --git a/collector/runtime-benchmarks/Cargo.toml b/collector/runtime-benchmarks/Cargo.toml index 5ce6021d7..25ad0d3af 100644 --- a/collector/runtime-benchmarks/Cargo.toml +++ b/collector/runtime-benchmarks/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["hashmap"] +members = ["hashmap", "bufreader"] diff --git a/collector/runtime-benchmarks/bufreader/Cargo.toml b/collector/runtime-benchmarks/bufreader/Cargo.toml new file mode 100644 index 000000000..2466a19a4 --- /dev/null +++ b/collector/runtime-benchmarks/bufreader/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "bufreader" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +benchlib = { path = "../../benchlib" } +snap = "1.0.5" diff --git a/collector/runtime-benchmarks/bufreader/src/main.rs b/collector/runtime-benchmarks/bufreader/src/main.rs new file mode 100644 index 000000000..7a49591ce --- /dev/null +++ b/collector/runtime-benchmarks/bufreader/src/main.rs @@ -0,0 +1,34 @@ +use benchlib::benchmark::{black_box, BenchmarkSuite}; +use snap::{read::FrameDecoder, write::FrameEncoder}; +use std::io::{BufRead, BufReader, Write}; + +const BYTES: usize = 64 * 1024 * 1024; + +fn main() { + let mut suite = BenchmarkSuite::new(); + + // Inspired by https://github.com/rust-lang/rust/issues/102727 + // The pattern we want is a BufReader which wraps a Read impl where one Read::read call will + // never fill the whole BufReader buffer. + suite.register("bufreader-snappy", || { + let data = vec![0u8; BYTES]; + move || { + let mut compressed = Vec::new(); + FrameEncoder::new(&mut compressed) + .write_all(&data[..]) + .unwrap(); + let mut reader = BufReader::with_capacity(BYTES, FrameDecoder::new(&compressed[..])); + + while let Ok(buf) = reader.fill_buf() { + if buf.is_empty() { + break; + } + black_box(buf); + let len = buf.len(); + reader.consume(len); + } + } + }); + + suite.run().unwrap(); +}