You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.1 KiB
37 lines
1.1 KiB
4 years ago
|
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);
|