-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
122 lines (100 loc) · 3.3 KB
/
benchmark.py
File metadata and controls
122 lines (100 loc) · 3.3 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import numpy as np
import joblib
from data_generator import generate_skewed_data
from collections import defaultdict, OrderedDict, Counter
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.cache = OrderedDict()
self.hits = 0
self.requests = 0
def access(self, key):
self.requests += 1
if key in self.cache:
self.hits += 1
self.cache.move_to_end(key)
else:
if len(self.cache) >= self.capacity:
self.cache.popitem(last=False)
self.cache[key] = None
def hit_rate(self):
return self.hits / self.requests if self.requests > 0 else 0
class LFUCache:
def __init__(self, capacity):
self.capacity = capacity
self.cache = set()
self.freq = Counter()
self.hits = 0
self.requests = 0
def access(self, key):
self.requests += 1
if key in self.cache:
self.hits += 1
else:
if len(self.cache) >= self.capacity:
evict = min(self.cache, key=lambda x: self.freq[x])
self.cache.remove(evict)
self.cache.add(key)
self.freq[key] += 1
def hit_rate(self):
return self.hits / self.requests if self.requests > 0 else 0
class MFUCache(LFUCache):
def access(self, key):
self.requests += 1
if key in self.cache:
self.hits += 1
else:
if len(self.cache) >= self.capacity:
evict = max(self.cache, key=lambda x: self.freq[x])
self.cache.remove(evict)
self.cache.add(key)
self.freq[key] += 1
class MLCache:
def __init__(self, capacity, model):
self.capacity = capacity
self.cache = set()
self.freq = defaultdict(int)
self.model = model
self.hits = 0
self.requests = 0
def access(self, key):
self.requests += 1
self.freq[key] += 1
features = np.array([[key, self.freq[key]]])
if key in self.cache:
self.hits += 1
else:
if len(self.cache) >= self.capacity:
probs = {
k: self.model.predict_proba(np.array([[k, self.freq[k]]]))[0][1]
for k in self.cache
}
evict = min(probs, key=probs.get)
self.cache.remove(evict)
self.cache.add(key)
def hit_rate(self):
return self.hits / self.requests if self.requests > 0 else 0
if __name__ == "__main__":
NUM_REQUESTS = 10000
UNIQUE_ITEMS = 100
CACHE_CAPACITY = 10
SKEW = 1.2
print("Generating test request stream...")
requests = generate_skewed_data(NUM_REQUESTS, UNIQUE_ITEMS, SKEW)
print("Loading best model...")
model = joblib.load("models/best_model.pkl")
print("Simulating caches...")
lru = LRUCache(CACHE_CAPACITY)
lfu = LFUCache(CACHE_CAPACITY)
mfu = MFUCache(CACHE_CAPACITY)
ml = MLCache(CACHE_CAPACITY, model)
for r in requests:
lru.access(r)
lfu.access(r)
mfu.access(r)
ml.access(r)
print("\n=== Cache Hit Rates ===")
print(f"LRU : {lru.hit_rate():.4f}")
print(f"LFU : {lfu.hit_rate():.4f}")
print(f"MFU : {mfu.hit_rate():.4f}")
print(f"ML : {ml.hit_rate():.4f}")