- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
Latest commit
122 lines (100 loc) · 3.3 KB
/
Copy pathbenchmark.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
importnumpyasnp
importjoblib
fromdata_generatorimportgenerate_skewed_data
fromcollectionsimportdefaultdict, OrderedDict, Counter
classLRUCache:
def__init__(self, capacity):
self.capacity=capacity
self.cache=OrderedDict()
self.hits=0
self.requests=0
defaccess(self, key):
self.requests+=1
ifkeyinself.cache:
self.hits+=1
self.cache.move_to_end(key)
else:
iflen(self.cache) >=self.capacity:
self.cache.popitem(last=False)
self.cache[key] =None
defhit_rate(self):
returnself.hits/self.requestsifself.requests>0else0
classLFUCache:
def__init__(self, capacity):
self.capacity=capacity
self.cache=set()
self.freq=Counter()
self.hits=0
self.requests=0
defaccess(self, key):
self.requests+=1
ifkeyinself.cache:
self.hits+=1
else:
iflen(self.cache) >=self.capacity:
evict=min(self.cache, key=lambdax: self.freq[x])
self.cache.remove(evict)
self.cache.add(key)
self.freq[key] +=1
defhit_rate(self):
returnself.hits/self.requestsifself.requests>0else0
classMFUCache(LFUCache):
defaccess(self, key):
self.requests+=1
ifkeyinself.cache:
self.hits+=1
else:
iflen(self.cache) >=self.capacity:
evict=max(self.cache, key=lambdax: self.freq[x])
self.cache.remove(evict)
self.cache.add(key)
self.freq[key] +=1
classMLCache:
def__init__(self, capacity, model):
self.capacity=capacity
self.cache=set()
self.freq=defaultdict(int)
self.model=model
self.hits=0
self.requests=0
defaccess(self, key):
self.requests+=1
self.freq[key] +=1
features=np.array([[key, self.freq[key]]])
ifkeyinself.cache:
self.hits+=1
else:
iflen(self.cache) >=self.capacity:
probs= {
k: self.model.predict_proba(np.array([[k, self.freq[k]]]))[0][1]
forkinself.cache
}
evict=min(probs, key=probs.get)
self.cache.remove(evict)
self.cache.add(key)
defhit_rate(self):
returnself.hits/self.requestsifself.requests>0else0
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)
forrinrequests:
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}")