From 70ee87ea297f0b8d7c0c146564de1260b3c71f42 Mon Sep 17 00:00:00 2001 From: Carl Lerche <me@carllerche.com> Date: Mon, 22 May 2017 12:02:51 -0700 Subject: [PATCH] Fix benchmarks --- benches/bytes.rs | 128 +++++++++++++++++++++++++ benches/vs_easy_buf.rs | 210 ----------------------------------------- 2 files changed, 128 insertions(+), 210 deletions(-) create mode 100644 benches/bytes.rs delete mode 100644 benches/vs_easy_buf.rs diff --git a/benches/bytes.rs b/benches/bytes.rs new file mode 100644 index 0000000..4232ced --- /dev/null +++ b/benches/bytes.rs @@ -0,0 +1,128 @@ +#![feature(test)] + +extern crate bytes; +extern crate test; + +use test::Bencher; +use bytes::{BytesMut, BufMut}; + +#[bench] +fn alloc_small(b: &mut Bencher) { + b.iter(|| { + for _ in 0..1024 { + test::black_box(BytesMut::with_capacity(12)); + } + }) +} + +#[bench] +fn alloc_mid(b: &mut Bencher) { + b.iter(|| { + test::black_box(BytesMut::with_capacity(128)); + }) +} + +#[bench] +fn alloc_big(b: &mut Bencher) { + b.iter(|| { + test::black_box(BytesMut::with_capacity(4096)); + }) +} + +#[bench] +fn deref_unique(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(4096); + buf.put(&[0u8; 1024][..]); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_unique_unroll(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(4096); + buf.put(&[0u8; 1024][..]); + + b.iter(|| { + for _ in 0..128 { + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_shared(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(4096); + buf.put(&[0u8; 1024][..]); + let _b2 = buf.split_off(1024); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_inline(b: &mut Bencher) { + let mut buf = BytesMut::with_capacity(8); + buf.put(&[0u8; 8][..]); + + b.iter(|| { + for _ in 0..1024 { + test::black_box(&buf[..]); + } + }) +} + +#[bench] +fn deref_two(b: &mut Bencher) { + let mut buf1 = BytesMut::with_capacity(8); + buf1.put(&[0u8; 8][..]); + + let mut buf2 = BytesMut::with_capacity(4096); + buf2.put(&[0u8; 1024][..]); + + b.iter(|| { + for _ in 0..512 { + test::black_box(&buf1[..]); + test::black_box(&buf2[..]); + } + }) +} + +#[bench] +fn alloc_write_split_to_mid(b: &mut Bencher) { + b.iter(|| { + let mut buf = BytesMut::with_capacity(128); + buf.put_slice(&[0u8; 64]); + test::black_box(buf.split_to(64)); + }) +} + +#[bench] +fn drain_write_drain(b: &mut Bencher) { + let data = [0u8; 128]; + + b.iter(|| { + let mut buf = BytesMut::with_capacity(1024); + let mut parts = Vec::with_capacity(8); + + for _ in 0..8 { + buf.put(&data[..]); + parts.push(buf.split_to(128)); + } + + test::black_box(parts); + }) +} diff --git a/benches/vs_easy_buf.rs b/benches/vs_easy_buf.rs deleted file mode 100644 index 063218a..0000000 --- a/benches/vs_easy_buf.rs +++ /dev/null @@ -1,210 +0,0 @@ -#![feature(test)] - -extern crate tokio_core; -extern crate bytes; -extern crate test; - -mod bench_easy_buf { - use test::{self, Bencher}; - use tokio_core::io::EasyBuf; - - #[bench] - fn alloc_small(b: &mut Bencher) { - b.iter(|| { - for _ in 0..1024 { - test::black_box(EasyBuf::with_capacity(12)); - } - }) - } - - #[bench] - fn alloc_mid(b: &mut Bencher) { - b.iter(|| { - test::black_box(EasyBuf::with_capacity(128)); - }) - } - - #[bench] - fn alloc_big(b: &mut Bencher) { - b.iter(|| { - test::black_box(EasyBuf::with_capacity(4096)); - }) - } - - #[bench] - fn deref_front(b: &mut Bencher) { - let mut buf = EasyBuf::with_capacity(4096); - buf.get_mut().extend_from_slice(&[0; 1024][..]); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(buf.as_slice()); - } - }) - } - - #[bench] - fn deref_mid(b: &mut Bencher) { - let mut buf = EasyBuf::with_capacity(4096); - buf.get_mut().extend_from_slice(&[0; 1024][..]); - let _a = buf.drain_to(512); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(buf.as_slice()); - } - }) - } - - #[bench] - fn alloc_write_drain_to_mid(b: &mut Bencher) { - b.iter(|| { - let mut buf = EasyBuf::with_capacity(128); - buf.get_mut().extend_from_slice(&[0u8; 64]); - test::black_box(buf.drain_to(64)); - }) - } - - #[bench] - fn drain_write_drain(b: &mut Bencher) { - let data = [0u8; 128]; - - b.iter(|| { - let mut buf = EasyBuf::with_capacity(1024); - let mut parts = Vec::with_capacity(8); - - for _ in 0..8 { - buf.get_mut().extend_from_slice(&data[..]); - parts.push(buf.drain_to(128)); - } - - test::black_box(parts); - }) - } -} - -mod bench_bytes { - use test::{self, Bencher}; - use bytes::{BytesMut, BufMut}; - - #[bench] - fn alloc_small(b: &mut Bencher) { - b.iter(|| { - for _ in 0..1024 { - test::black_box(BytesMut::with_capacity(12)); - } - }) - } - - #[bench] - fn alloc_mid(b: &mut Bencher) { - b.iter(|| { - test::black_box(BytesMut::with_capacity(128)); - }) - } - - #[bench] - fn alloc_big(b: &mut Bencher) { - b.iter(|| { - test::black_box(BytesMut::with_capacity(4096)); - }) - } - - #[bench] - fn deref_unique(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(4096); - buf.put(&[0u8; 1024][..]); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&buf[..]); - } - }) - } - - #[bench] - fn deref_unique_unroll(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(4096); - buf.put(&[0u8; 1024][..]); - - b.iter(|| { - for _ in 0..128 { - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - test::black_box(&buf[..]); - } - }) - } - - #[bench] - fn deref_shared(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(4096); - buf.put(&[0u8; 1024][..]); - let _b2 = buf.split_off(1024); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&buf[..]); - } - }) - } - - #[bench] - fn deref_inline(b: &mut Bencher) { - let mut buf = BytesMut::with_capacity(8); - buf.put(&[0u8; 8][..]); - - b.iter(|| { - for _ in 0..1024 { - test::black_box(&buf[..]); - } - }) - } - - #[bench] - fn deref_two(b: &mut Bencher) { - let mut buf1 = BytesMut::with_capacity(8); - buf1.put(&[0u8; 8][..]); - - let mut buf2 = BytesMut::with_capacity(4096); - buf2.put(&[0u8; 1024][..]); - - b.iter(|| { - for _ in 0..512 { - test::black_box(&buf1[..]); - test::black_box(&buf2[..]); - } - }) - } - - #[bench] - fn alloc_write_drain_to_mid(b: &mut Bencher) { - b.iter(|| { - let mut buf = BytesMut::with_capacity(128); - buf.put_slice(&[0u8; 64]); - test::black_box(buf.drain_to(64)); - }) - } - - #[bench] - fn drain_write_drain(b: &mut Bencher) { - let data = [0u8; 128]; - - b.iter(|| { - let mut buf = BytesMut::with_capacity(1024); - let mut parts = Vec::with_capacity(8); - - for _ in 0..8 { - buf.put(&data[..]); - parts.push(buf.drain_to(128)); - } - - test::black_box(parts); - }) - } -} -- GitLab