-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlru_k.rs
More file actions
102 lines (89 loc) · 3.05 KB
/
lru_k.rs
File metadata and controls
102 lines (89 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! LRU-K specific benchmarks for operations unique to LRU-K policy.
//!
//! Run with: `cargo bench --bench policy_lru_k`
use std::sync::Arc;
use cachekit::policy::lru_k::LrukCache;
use cachekit::traits::Cache;
use criterion::{BatchSize, Criterion, Throughput, criterion_group, criterion_main};
// ============================================================================
// LRU-K specific operations
// ============================================================================
/// Benchmark pop_lru_k - removing based on K-th access time.
fn bench_pop_lru_k(c: &mut Criterion) {
let mut group = c.benchmark_group("lru_k_specific");
group.throughput(Throughput::Elements(1024));
group.bench_function("pop_lru_k", |b| {
b.iter_batched(
|| {
let mut cache = LrukCache::with_k(1024, 2);
for i in 0..1024u64 {
cache.insert(i, Arc::new(i));
}
cache
},
|mut cache| {
for _ in 0..1024u64 {
let _ = std::hint::black_box(cache.pop_lru_k());
}
},
BatchSize::SmallInput,
)
});
group.finish();
}
/// Benchmark touch - updating access history.
fn bench_touch(c: &mut Criterion) {
let mut group = c.benchmark_group("lru_k_specific");
group.throughput(Throughput::Elements(4096));
group.bench_function("touch_hotset", |b| {
b.iter_batched(
|| {
let mut cache = LrukCache::with_k(4096, 2);
for i in 0..4096u64 {
cache.insert(i, Arc::new(i));
}
cache
},
|mut cache| {
for i in 0..4096u64 {
let _ = std::hint::black_box(cache.touch(&std::hint::black_box(i)));
}
},
BatchSize::SmallInput,
)
});
group.finish();
}
/// Benchmark with different K values.
fn bench_k_values(c: &mut Criterion) {
let mut group = c.benchmark_group("lru_k_values");
let capacity = 4096;
let ops = 4096;
group.throughput(Throughput::Elements(ops));
for k in [1, 2, 3, 4] {
group.bench_with_input(
criterion::BenchmarkId::from_parameter(format!("k={}", k)),
&k,
|b, &k| {
b.iter_batched(
|| {
let mut cache = LrukCache::with_k(capacity, k);
for i in 0..capacity as u64 {
cache.insert(i, Arc::new(i));
}
cache
},
|mut cache| {
for i in 0..ops {
let _ = std::hint::black_box(cache.get(&std::hint::black_box(i)));
}
},
BatchSize::SmallInput,
)
},
);
}
group.finish();
}
criterion_group!(benches, bench_pop_lru_k, bench_touch, bench_k_values);
criterion_main!(benches);