Skip to content

Commit

Permalink
chore: re-write benches
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Zak <richard.j.zak@gmail.com>
  • Loading branch information
rjzak committed Aug 24, 2023
1 parent e3ed5b7 commit 7a236ac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 64 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ path = "src/lib.rs"

[dev-dependencies]
rand = "0.8"
criterion = "0.5.0"

[[bench]]
name = "murmurhash3"
harness = false
48 changes: 48 additions & 0 deletions benches/murmurhash3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use rand::RngCore;
use std::iter::FromIterator;

use murmurhash3::{murmurhash3_x64_128, murmurhash3_x86_32};

fn run_bench(c: &mut Criterion, size: u64) {
c.bench_function("murmurhash 256k", |b| {
let mut data: Vec<u8> = FromIterator::from_iter((0..size).map(|_| 0u8));
rand::thread_rng().fill_bytes(&mut data);

b.iter(|| {
black_box(murmurhash3_x86_32(&data, 0));
});
});
}

fn run_bench128(c: &mut Criterion, size: u64) {
c.bench_function("murmurhash128 256k", |b| {
let mut data: Vec<u8> = FromIterator::from_iter((0..size).map(|_| 0u8));
rand::thread_rng().fill_bytes(&mut data);

b.iter(|| {
black_box(murmurhash3_x64_128(&data, 0));
});
});
}

fn bench_random_256k(c: &mut Criterion) {
run_bench(c, 256 * 1024);
}

fn bench_random_16b(c: &mut Criterion) {
run_bench(c, 16);
}

fn bench_random128_256k(c: &mut Criterion) {
run_bench128(c, 256 * 1024);
}

fn bench_random128_16b(c: &mut Criterion) {
run_bench128(c, 16);
}

criterion_group!(benches, bench_random_16b, bench_random_256k);
criterion_group!(benches128, bench_random128_16b, bench_random128_256k);
criterion_main!(benches);
32 changes: 0 additions & 32 deletions src/mmh3_128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,36 +202,4 @@ mod test {
fn test_large_data() {
assert_eq!(murmurhash3_x64_128("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0), (9455322759164802692, 17863277201603478371));
}

#[cfg(feature = "nightly")]
mod bench {
extern crate rand;
extern crate test;

use self::rand::Rng;
use self::test::{black_box, Bencher};
use std::iter::FromIterator;

use super::super::murmurhash3_x64_128;

fn run_bench(b: &mut Bencher, size: u64) {
let mut data: Vec<u8> = FromIterator::from_iter((0..size).map(|_| 0u8));
rand::thread_rng().fill_bytes(&mut data);

b.bytes = size;
b.iter(|| {
black_box(murmurhash3_x64_128(&data, 0));
});
}

#[bench]
fn bench_random_256k(b: &mut Bencher) {
run_bench(b, 256 * 1024);
}

#[bench]
fn bench_random_16b(b: &mut Bencher) {
run_bench(b, 16);
}
}
}
32 changes: 0 additions & 32 deletions src/mmh3_32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,4 @@ mod test {
fn test_large_data() {
assert_eq!(murmurhash3_x86_32("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0), 1004899618);
}

#[cfg(feature = "nightly")]
mod bench {
extern crate rand;
extern crate test;

use self::rand::Rng;
use self::test::{black_box, Bencher};
use std::iter::FromIterator;

use super::super::murmurhash3_x86_32;

fn run_bench(b: &mut Bencher, size: u64) {
let mut data: Vec<u8> = FromIterator::from_iter((0..size).map(|_| 0u8));
rand::thread_rng().fill_bytes(&mut data);

b.bytes = size;
b.iter(|| {
black_box(murmurhash3_x86_32(&data, 0));
});
}

#[bench]
fn bench_random_256k(b: &mut Bencher) {
run_bench(b, 256 * 1024);
}

#[bench]
fn bench_random_16b(b: &mut Bencher) {
run_bench(b, 16);
}
}
}

0 comments on commit 7a236ac

Please sign in to comment.