- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
Latest commit
56 lines (45 loc) · 1.54 KB
/
Copy pathtest_cache.py
File metadata and controls
56 lines (45 loc) · 1.54 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
importpandasaspd
importnumpyasnp
importjoblib
fromcollectionsimportdefaultdict
fromdata_generatorimportgenerate_skewed_data
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)
defget_hit_rate(self):
returnself.hits/self.requestsifself.requests>0else0.0
if__name__=="__main__":
NUM_REQUESTS=10000
UNIQUE_ITEMS=100
CACHE_CAPACITY=10
SKEW=1.3
print("Generating test request stream...")
test_data=generate_skewed_data(NUM_REQUESTS, UNIQUE_ITEMS, SKEW)
print("Loading trained best model...")
model=joblib.load("models/best_model.pkl")
print("Running smart cache simulation...")
cache=MLCache(CACHE_CAPACITY, model)
foritemintest_data:
cache.access(item)
hit_rate=cache.get_hit_rate()
print(f"Smart cache hit rate: {hit_rate:.4f}")