Implement benchmarks for buffers

pull/214/head
Daniel Abramov 4 years ago
parent 2787031c2f
commit ddddd791ef
  1. 8
      Cargo.toml
  2. 36
      benches/buffer.rs
  3. 2
      src/lib.rs

@ -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

@ -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::<CHUNK_SIZE>::new();
while buffer.read_from(&mut stream).unwrap() != 0 {}
}
fn benchmark(c: &mut Criterion) {
const STREAM_SIZE: usize = 1024 * 1024 * 4;
let data: Vec<u8> = (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);

@ -14,7 +14,7 @@
pub use http;
mod buffer;
pub mod buffer;
pub mod client;
pub mod error;
pub mod handshake;

Loading…
Cancel
Save