Skip to content

Commit

Permalink
improve bloom filter benchmark #78
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Nov 17, 2024
1 parent 2e458e2 commit c2a24a2
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions benches/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use lsm_tree::bloom::BloomFilter;

fn filter_construction(c: &mut Criterion) {
let mut filter = BloomFilter::with_fp_rate(1_000_000, 0.001);
let mut filter = BloomFilter::with_fp_rate(1_000_000, 0.01);

c.bench_function("bloom filter add key", |b| {
b.iter(|| {
Expand All @@ -13,24 +13,48 @@ fn filter_construction(c: &mut Criterion) {
}

fn filter_contains(c: &mut Criterion) {
let mut filter = BloomFilter::with_fp_rate(10, 0.0001);

for key in [
b"item0", b"item1", b"item2", b"item3", b"item4", b"item5", b"item6", b"item7", b"item8",
b"item9",
] {
filter.set_with_hash(BloomFilter::get_hash(key));

assert!(!filter.contains(nanoid::nanoid!().as_bytes()));
for fpr in [0.01, 0.001, 0.0001, 0.00001] {
let mut filter = BloomFilter::with_fp_rate(100_000, fpr);

for key in [
b"item0", b"item1", b"item2", b"item3", b"item4", b"item5", b"item6", b"item7",
b"item8", b"item9",
] {
filter.set_with_hash(BloomFilter::get_hash(key));

assert!(!filter.contains(nanoid::nanoid!().as_bytes()));
}

c.bench_function(
&format!(
"bloom filter contains key, true positive ({}%)",
fpr * 100.0
),
|b| {
b.iter(|| {
// We hash once and then do 4 runs of bloom filter lookups
// to simulate hash sharing (https://fjall-rs.github.io/post/bloom-filter-hash-sharing/)
// and L0 having 4 segments + 1 check in L1
let hash = BloomFilter::get_hash(b"item4");

for _ in 0..5 {
assert!(filter.contains_hash(hash));
}
});
},
);

/*
c.bench_function(
&format!(
"bloom filter contains key, true negative ({}%)",
fpr * 100.0
),
|b| {
b.iter(|| filter.contains(b"sdfafdas"));
},
); */
}

c.bench_function("bloom filter contains key, true positive", |b| {
b.iter(|| filter.contains(b"item4"));
});

c.bench_function("bloom filter contains key, true negative", |b| {
b.iter(|| filter.contains(b"sdfafdas"));
});
}

criterion_group!(benches, filter_construction, filter_contains,);
Expand Down

0 comments on commit c2a24a2

Please sign in to comment.