Skip to content

Commit

Permalink
replace q460_lfu_cache's benches with cache_util's ones
Browse files Browse the repository at this point in the history
  • Loading branch information
drink7036290 committed Dec 19, 2024
1 parent ef2296a commit deaea06
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
# Output the JSON array to GitHub Actions output
echo "subcrates=$SUBCRATES_JSON" >> "$GITHUB_OUTPUT"
SUBCRATES_FOR_BENCHES=$(echo "$SUBCRATES" | grep q146 || true)
SUBCRATES_FOR_BENCHES=$(echo "$SUBCRATES" || true)
# Build a JSON array of objects
# Each object: { "subcrate": "xxx", "bench": "yyy" }
Expand Down
2 changes: 1 addition & 1 deletion implementations/q146_lru_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ paste = "1.0.15"
[lib]
# specially for Criterion
# without this option, cargo bench will fail by commands like this:
# cargo bench -p q146_lru_cache -- --verbose # error: Unrecognized option: 'verbose'
# cargo bench -p SUBCRATE -- --verbose # error: Unrecognized option: 'verbose'
bench = false
# https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use cache_util::*;

use q146_lru_cache::intrusive_two_hashmaps::LRUCache as CACHE;

define_benchmark!(q146_lru_cache, intrusive_two_hashmaps);
define_benchmark!(q146_lru_cache, intrusive_two_hashmaps, LRUCache);
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use cache_util::*;

use q146_lru_cache::priority_queue::LRUCache as CACHE;

define_benchmark!(q146_lru_cache, priority_queue);
define_benchmark!(q146_lru_cache, priority_queue, LRUCache);
4 changes: 1 addition & 3 deletions implementations/q146_lru_cache/benches/bench_two_hashmaps.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use cache_util::*;

use q146_lru_cache::two_hashmaps::LRUCache as CACHE;

define_benchmark!(q146_lru_cache, two_hashmaps);
define_benchmark!(q146_lru_cache, two_hashmaps, LRUCache);
4 changes: 1 addition & 3 deletions implementations/q146_lru_cache/benches/bench_vec_hashmap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use cache_util::*;

use q146_lru_cache::vec_hashmap::LRUCache as CACHE;

define_benchmark!(q146_lru_cache, vec_hashmap);
define_benchmark!(q146_lru_cache, vec_hashmap, LRUCache);
23 changes: 21 additions & 2 deletions implementations/q460_lfu_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ rand = "0.8.5"
criterion = "0.5.1"
paste = "1.0.15"

[lib]
# specially for Criterion
# without this option, cargo bench will fail by commands like this:
# cargo bench -p SUBCRATE -- --verbose # error: Unrecognized option: 'verbose'
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_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_all_impl"
harness = false # Disables the default Rust benchmarking harness so that Criterion can use its own.
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/q460_lfu_cache/benches/bench_all_impl.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use cache_util::*;

define_benchmark!(q460_lfu_cache, intrusive_two_hashmaps, LFUCache);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use cache_util::*;

define_benchmark!(q460_lfu_cache, priority_queue, LFUCache);
3 changes: 3 additions & 0 deletions implementations/q460_lfu_cache/benches/bench_two_hashmaps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use cache_util::*;

define_benchmark!(q460_lfu_cache, two_hashmaps, LFUCache);
3 changes: 3 additions & 0 deletions implementations/q460_lfu_cache/benches/bench_vec_hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use cache_util::*;

define_benchmark!(q460_lfu_cache, vec_hashmap, LFUCache);
12 changes: 7 additions & 5 deletions implementations/q460_lfu_cache/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod heap_node;
pub mod impl_v1;
pub mod impl_v2;
pub mod impl_v3;
pub mod impl_v4;
mod freq_aware_heap_node;
pub mod intrusive_two_hashmaps;
pub mod priority_queue;
pub mod two_hashmaps;
pub mod vec_hashmap;

pub use freq_aware_heap_node::*;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use priority_queue::PriorityQueue;
use std::cmp::Reverse;
use std::time::SystemTime;

use crate::heap_node::FreqAwareHeapNode;
use crate::FreqAwareHeapNode;

pub struct LFUCache {
pq: PriorityQueue<i32, Reverse<FreqAwareHeapNode>>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::heap_node::FreqAwareHeapNode;
use crate::FreqAwareHeapNode;
use std::collections::HashMap;

use std::cmp::Ordering;
Expand Down
8 changes: 4 additions & 4 deletions implementations/q460_lfu_cache/tests/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn operation_sequence_strategy() -> impl Strategy<Value = Vec<CacheOperation>> {
}

fn test_lfu_cache_with_operations(capacity: i32, operations: Vec<CacheOperation>) {
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;
use q460_lfu_cache::intrusive_two_hashmaps::LFUCache as LFUCache_v4;
use q460_lfu_cache::priority_queue::LFUCache as LFUCache_v1;
use q460_lfu_cache::two_hashmaps::LFUCache as LFUCache_v3;
use q460_lfu_cache::vec_hashmap::LFUCache as LFUCache_v2;

let mut cache_v1 = LFUCache_v1::new(capacity);
let mut cache_v2 = LFUCache_v2::new(capacity);
Expand Down
8 changes: 4 additions & 4 deletions implementations/q460_lfu_cache/tests/test_all_impl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use rstest::rstest;

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;
use q460_lfu_cache::intrusive_two_hashmaps::LFUCache as LFUCache_v4;
use q460_lfu_cache::priority_queue::LFUCache as LFUCache_v1;
use q460_lfu_cache::two_hashmaps::LFUCache as LFUCache_v3;
use q460_lfu_cache::vec_hashmap::LFUCache as LFUCache_v2;

#[rstest]
#[case(vec!["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"],
Expand Down
9 changes: 6 additions & 3 deletions utilities/cache_util/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ macro_rules! bench_cache {
cache_util::CacheOperation::Get { key } => {
::criterion::black_box(cache.get(::criterion::black_box(*key)));
}
_ => unreachable!(),
}
}
})
Expand All @@ -29,15 +30,17 @@ macro_rules! bench_cache {
#[macro_export]
// Define the custom macro using paste for identifier concatenation
macro_rules! define_benchmark {
($crate_path:path, $postfix:ident) => {
($crate_name:ident, $impl:ident, $cache_type:ty) => {
::paste::paste! {
use criterion::{criterion_group, criterion_main};

use $crate_name::$impl::$cache_type as CACHE;

// Generate a unique benchmark function name
bench_cache!([<$crate_path _ bench_ $postfix>], CACHE);
bench_cache!([<$crate_name _bench_ $impl>], CACHE);

// Collect the benchmark function into the group
criterion_group!(benches, [<$crate_path _ bench_ $postfix>]);
criterion_group!(benches, [<$crate_name _bench_ $impl>]);
criterion_main!(benches);
}
};
Expand Down

0 comments on commit deaea06

Please sign in to comment.