Skip to content

Commit

Permalink
bench for q460_lfu_cache to compare different implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
drink7036290 committed Dec 1, 2024
1 parent 392b89c commit 992305a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
9 changes: 8 additions & 1 deletion implementations/q460_lfu_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ intrusive-collections = "0.9.7"

[dev-dependencies]
rstest = "0.23.0"
proptest = "1.5.0"
proptest = "1.5.0"
once_cell = "1.20.2"
rand = "0.8.5"
criterion = "0.5.1"

[[bench]]
name = "bench_all_impl"
harness = false # Disables the default Rust benchmarking harness so that Criterion can use its own.
70 changes: 70 additions & 0 deletions implementations/q460_lfu_cache/benches/bench_all_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use once_cell::sync::Lazy;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};

const SEED: u64 = 12345; // Use a fixed seed for reproducibility

static OPERATIONS: Lazy<Vec<(u8, i32, i32)>> = Lazy::new(|| {
// Generate random data here
let mut rng = StdRng::seed_from_u64(SEED);

let num_operations = 2000; // Set the number of operations
let mut operations = Vec::with_capacity(num_operations);

for _ in 0..num_operations {
let op = rng.gen_range(0..2) as u8; // 0 for put, 1 for get
let key = rng.gen_range(0..=100_000i32);
let value = rng.gen_range(0..=1_000_000_000i32); // Only needed for put operations
operations.push((op, key, value));
}

operations
});

use q460_lfu_cache::impl_v1::LFUCache as LFUCache_v1;
use q460_lfu_cache::impl_v2::LFUCache as LFUCache_v2;
use q460_lfu_cache::impl_v3::LFUCache as LFUCache_v3;
use q460_lfu_cache::impl_v4::LFUCache as LFUCache_v4;

macro_rules! bench_lfu_cache {
($bench_name:ident, $cache_type:ty) => {
fn $bench_name(c: &mut Criterion) {
// Define the capacity as a constant or parameter
const CAPACITY: i32 = 10_000i32;

c.bench_function(stringify!($bench_name), |b| {
b.iter(|| {
let mut cache: $cache_type = <$cache_type>::new(black_box(CAPACITY));
for &(op, key, value) in OPERATIONS.iter() {
match op {
0 => {
// Put operation
cache.put(black_box(key), black_box(value));
}
1 => {
// Get operation
black_box(cache.get(black_box(key)));
}
_ => unreachable!(),
}
}
})
});
}
};
}

bench_lfu_cache!(bench_lfu_cache_v1, LFUCache_v1);
bench_lfu_cache!(bench_lfu_cache_v2, LFUCache_v2);
bench_lfu_cache!(bench_lfu_cache_v3, LFUCache_v3);
bench_lfu_cache!(bench_lfu_cache_v4, LFUCache_v4);

criterion_group!(
benches,
bench_lfu_cache_v1,
bench_lfu_cache_v2,
bench_lfu_cache_v3,
bench_lfu_cache_v4,
);
criterion_main!(benches);

0 comments on commit 992305a

Please sign in to comment.