Skip to content

Commit

Permalink
Use criterion for benchmarks. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
twittner authored Feb 11, 2019
1 parent 2fe0e98 commit 881f02d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ tokio-codec = { version = "0.1", optional = true }

[dev-dependencies]
bytes = "0.4"
criterion = "0.2"
hex = "0.3"
quickcheck = "0.7"
quickcheck = "0.8"
tokio-codec = "0.1"

[[bench]]
name = "benchmark"
harness = false
45 changes: 20 additions & 25 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,28 @@
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#![feature(test)]

extern crate test;
extern crate unsigned_varint;

#[cfg(feature = "codec")]
extern crate bytes;
#[cfg(feature = "codec")]
extern crate tokio_codec;

use criterion::{criterion_group, criterion_main, Criterion};
use std::u64;
use test::Bencher;
use unsigned_varint::{decode, encode};

#[bench]
fn bench_decode(b: &mut Bencher) {
fn bench_decode(c: &mut Criterion) {
let mut buf = [0; 10];
let bytes = encode::u64(u64::MAX, &mut buf);
b.iter(|| {
assert_eq!(u64::MAX, decode::u64(bytes).unwrap().0)
});
let len = encode::u64(u64::MAX, &mut buf).len();
c.bench_function("decode", move |b| b.iter(|| {
assert_eq!(u64::MAX, decode::u64(&buf[.. len]).unwrap().0)
}));
}

#[bench]
fn bench_encode(b: &mut Bencher) {
fn bench_encode(c: &mut Criterion) {
let mut buf = [0; 10];
let encoded = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 1];
b.iter(|| {
c.bench_function("encode", move |b| b.iter(|| {
assert_eq!(&encoded, encode::u64(u64::MAX, &mut buf));
});
}));
}

#[cfg(feature = "codec")]
#[bench]
fn bench_codec(b: &mut Bencher) {
fn bench_codec(c: &mut Criterion) {
use bytes::{Bytes, BytesMut};
use tokio_codec::{Decoder, Encoder};
use unsigned_varint::codec::UviBytes;
Expand All @@ -60,9 +47,17 @@ fn bench_codec(b: &mut Bencher) {
let mut bytes = BytesMut::with_capacity(9000);
let mut uvi_bytes = UviBytes::default();

b.iter(move || {
c.bench_function("codec", move |b| b.iter(|| {
uvi_bytes.encode(data.clone(), &mut bytes).unwrap();
assert_eq!(data, uvi_bytes.decode(&mut bytes.take()).unwrap().unwrap())
});
}));
}

#[cfg(feature = "codec")]
criterion_group!(benches, bench_encode, bench_decode, bench_codec);

#[cfg(not(feature = "codec"))]
criterion_group!(benches, bench_encode, bench_decode);

criterion_main!(benches);

0 comments on commit 881f02d

Please sign in to comment.