diff --git a/implementations/q146_lru_cache/Cargo.toml b/implementations/q146_lru_cache/Cargo.toml index e30a0d4..3749b52 100644 --- a/implementations/q146_lru_cache/Cargo.toml +++ b/implementations/q146_lru_cache/Cargo.toml @@ -22,5 +22,17 @@ bench = false # https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options [[bench]] -name = "bench_all_impl" +name = "bench_priority_queue" harness = false # Disables the default Rust benchmarking harness so that Criterion can use its own. + +[[bench]] +name = "bench_vec_hashmap" +harness = false # Disables the default Rust benchmarking harness so that Criterion can use its own. + +[[bench]] +name = "bench_two_hashmaps" +harness = false # Disables the default Rust benchmarking harness so that Criterion can use its own. + +[[bench]] +name = "bench_intrusive_two_hashmaps" +harness = false # Disables the default Rust benchmarking harness so that Criterion can use its own. \ No newline at end of file diff --git a/implementations/q146_lru_cache/benches/bench_all_impl.rs b/implementations/q146_lru_cache/benches/bench_all_impl.rs deleted file mode 100644 index d41b1d0..0000000 --- a/implementations/q146_lru_cache/benches/bench_all_impl.rs +++ /dev/null @@ -1,70 +0,0 @@ -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> = Lazy::new(|| { - // Generate random data here - let mut rng = StdRng::seed_from_u64(SEED); - - let num_operations = 10_000; // 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 q146_lru_cache::impl_v1::LRUCache as LRUCache_v1; -use q146_lru_cache::impl_v2::LRUCache as LRUCache_v2; -use q146_lru_cache::impl_v3::LRUCache as LRUCache_v3; -use q146_lru_cache::impl_v4::LRUCache as LRUCache_v4; - -macro_rules! bench_lru_cache { - ($bench_name:ident, $cache_type:ty) => { - fn $bench_name(c: &mut Criterion) { - // Define the capacity as a constant or parameter - const CAPACITY: i32 = 3_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_lru_cache!(q146_with_priority_queue, LRUCache_v1); -bench_lru_cache!(q146_with_vec_hashmap, LRUCache_v2); -bench_lru_cache!(q146_with_two_hashmaps, LRUCache_v3); -bench_lru_cache!(q146_with_intrusive_two_hashmaps, LRUCache_v4); - -criterion_group!( - benches, - q146_with_priority_queue, - q146_with_vec_hashmap, - q146_with_two_hashmaps, - q146_with_intrusive_two_hashmaps, -); -criterion_main!(benches); diff --git a/implementations/q146_lru_cache/benches/bench_intrusive_two_hashmaps.rs b/implementations/q146_lru_cache/benches/bench_intrusive_two_hashmaps.rs new file mode 100644 index 0000000..4b6308d --- /dev/null +++ b/implementations/q146_lru_cache/benches/bench_intrusive_two_hashmaps.rs @@ -0,0 +1,10 @@ +mod modules; + +use criterion::{criterion_group, criterion_main}; + +use q146_lru_cache::impl_v4::LRUCache; + +bench_lru_cache!(q146_with_intrusive_two_hashmaps, LRUCache); + +criterion_group!(benches, q146_with_intrusive_two_hashmaps); +criterion_main!(benches); diff --git a/implementations/q146_lru_cache/benches/bench_priority_queue.rs b/implementations/q146_lru_cache/benches/bench_priority_queue.rs new file mode 100644 index 0000000..bd61499 --- /dev/null +++ b/implementations/q146_lru_cache/benches/bench_priority_queue.rs @@ -0,0 +1,10 @@ +mod modules; + +use criterion::{criterion_group, criterion_main}; + +use q146_lru_cache::impl_v1::LRUCache; + +bench_lru_cache!(q146_with_priority_queue, LRUCache); + +criterion_group!(benches, q146_with_priority_queue); +criterion_main!(benches); diff --git a/implementations/q146_lru_cache/benches/bench_two_hashmaps.rs b/implementations/q146_lru_cache/benches/bench_two_hashmaps.rs new file mode 100644 index 0000000..948dad8 --- /dev/null +++ b/implementations/q146_lru_cache/benches/bench_two_hashmaps.rs @@ -0,0 +1,10 @@ +mod modules; + +use criterion::{criterion_group, criterion_main}; + +use q146_lru_cache::impl_v3::LRUCache; + +bench_lru_cache!(q146_with_two_hashmaps, LRUCache); + +criterion_group!(benches, q146_with_two_hashmaps); +criterion_main!(benches); diff --git a/implementations/q146_lru_cache/benches/bench_vec_hashmap.rs b/implementations/q146_lru_cache/benches/bench_vec_hashmap.rs new file mode 100644 index 0000000..c4c8526 --- /dev/null +++ b/implementations/q146_lru_cache/benches/bench_vec_hashmap.rs @@ -0,0 +1,10 @@ +mod modules; + +use criterion::{criterion_group, criterion_main}; + +use q146_lru_cache::impl_v2::LRUCache; + +bench_lru_cache!(q146_with_vec_hashmap, LRUCache); + +criterion_group!(benches, q146_with_vec_hashmap); +criterion_main!(benches); diff --git a/implementations/q146_lru_cache/benches/modules/bench_common.rs b/implementations/q146_lru_cache/benches/modules/bench_common.rs new file mode 100644 index 0000000..38f0c9c --- /dev/null +++ b/implementations/q146_lru_cache/benches/modules/bench_common.rs @@ -0,0 +1,66 @@ +use std::ops::RangeInclusive; + +use once_cell::sync::Lazy; +use rand::rngs::StdRng; +use rand::{Rng, SeedableRng}; + +const SEED: u64 = 12345; // Use a fixed seed for reproducibility + +const NUM_OPERATIONS: usize = 10_000; + +fn key_range() -> RangeInclusive { + 0..=100_000 +} + +fn value_range() -> RangeInclusive { + 0..=1_000_000_000 +} + +pub static OPERATIONS: Lazy> = Lazy::new(|| { + // Generate random data here + let mut rng = StdRng::seed_from_u64(SEED); + + 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(key_range()); + let value = rng.gen_range(value_range()); // Only needed for put operations + operations.push((op, key, value)); + } + + operations +}); + +#[macro_export] +macro_rules! bench_lru_cache { + ($bench_name:ident, $cache_type:ty) => { + fn $bench_name(c: &mut ::criterion::Criterion) { + // Define the capacity as a constant or parameter + const CAPACITY: i32 = 3_000i32; + + c.bench_function(stringify!($bench_name), |b| { + b.iter(|| { + let mut cache: $cache_type = + <$cache_type>::new(::criterion::black_box(CAPACITY)); + for &(op, key, value) in $crate::modules::bench_common::OPERATIONS.iter() { + match op { + 0 => { + // Put operation + cache.put( + ::criterion::black_box(key), + ::criterion::black_box(value), + ); + } + 1 => { + // Get operation + ::criterion::black_box(cache.get(::criterion::black_box(key))); + } + _ => unreachable!(), + } + } + }) + }); + } + }; +} diff --git a/implementations/q146_lru_cache/benches/modules/mod.rs b/implementations/q146_lru_cache/benches/modules/mod.rs new file mode 100644 index 0000000..1ef56e7 --- /dev/null +++ b/implementations/q146_lru_cache/benches/modules/mod.rs @@ -0,0 +1 @@ +pub mod bench_common; diff --git a/utilities/cleanup.sh b/utilities/cleanup.sh new file mode 100755 index 0000000..cee6126 --- /dev/null +++ b/utilities/cleanup.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -e # Exit on error +cargo clean +find . -type f \( -name "*.profraw" -o -name "lcov*.info" \) -exec rm {} \;