Skip to content

Commit

Permalink
split bench_all_impl into multiple individual benches
Browse files Browse the repository at this point in the history
  • Loading branch information
drink7036290 committed Dec 8, 2024
1 parent 5bc9d1c commit 6976d81
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 71 deletions.
14 changes: 13 additions & 1 deletion implementations/q146_lru_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
70 changes: 0 additions & 70 deletions implementations/q146_lru_cache/benches/bench_all_impl.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions implementations/q146_lru_cache/benches/bench_priority_queue.rs
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions implementations/q146_lru_cache/benches/bench_two_hashmaps.rs
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions implementations/q146_lru_cache/benches/bench_vec_hashmap.rs
Original file line number Diff line number Diff line change
@@ -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);
66 changes: 66 additions & 0 deletions implementations/q146_lru_cache/benches/modules/bench_common.rs
Original file line number Diff line number Diff line change
@@ -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<i32> {
0..=100_000
}

fn value_range() -> RangeInclusive<i32> {
0..=1_000_000_000
}

pub static OPERATIONS: Lazy<Vec<(u8, i32, i32)>> = 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!(),
}
}
})
});
}
};
}
1 change: 1 addition & 0 deletions implementations/q146_lru_cache/benches/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bench_common;
5 changes: 5 additions & 0 deletions utilities/cleanup.sh
Original file line number Diff line number Diff line change
@@ -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 {} \;

0 comments on commit 6976d81

Please sign in to comment.