diff --git a/Cargo.toml b/Cargo.toml index 90ef5aa..d80b66c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,12 @@ optional = true version = "0.5.0" [dev-dependencies] -input_buffer = "0.5.0" +criterion = "0.3.4" env_logger = "0.8.1" +input_buffer = "0.5.0" net2 = "0.2.33" +rand = "0.8.4" + +[[bench]] +name = "buffer" +harness = false diff --git a/benches/buffer.rs b/benches/buffer.rs new file mode 100644 index 0000000..eaf4e53 --- /dev/null +++ b/benches/buffer.rs @@ -0,0 +1,36 @@ +use std::io::{Cursor, Read}; + +use criterion::*; +use input_buffer::InputBuffer; +use tungstenite::buffer::ReadBuffer; + +const CHUNK_SIZE: usize = 4096; + +#[inline] +fn current_input_buffer(mut stream: impl Read) { + let mut buffer = InputBuffer::with_capacity(CHUNK_SIZE); + while buffer.read_from(&mut stream).unwrap() != 0 {} +} + +#[inline] +fn fast_input_buffer(mut stream: impl Read) { + let mut buffer = ReadBuffer::::new(); + while buffer.read_from(&mut stream).unwrap() != 0 {} +} + +fn benchmark(c: &mut Criterion) { + const STREAM_SIZE: usize = 1024 * 1024 * 4; + let data: Vec = (0..STREAM_SIZE).map(|_| rand::random()).collect(); + let stream = Cursor::new(data); + + let mut group = c.benchmark_group("buffers"); + group.throughput(Throughput::Bytes(STREAM_SIZE as u64)); + group.bench_function("InputBuffer", |b| { + b.iter(|| current_input_buffer(black_box(stream.clone()))) + }); + group.bench_function("ReadBuffer", |b| b.iter(|| fast_input_buffer(black_box(stream.clone())))); + group.finish(); +} + +criterion_group!(benches, benchmark); +criterion_main!(benches); diff --git a/src/lib.rs b/src/lib.rs index 4ac56d7..c21958e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ pub use http; -mod buffer; +pub mod buffer; pub mod client; pub mod error; pub mod handshake;