File tree Expand file tree Collapse file tree 2 files changed +96
-0
lines changed Expand file tree Collapse file tree 2 files changed +96
-0
lines changed Original file line number Diff line number Diff line change 1+ from libcachesim import BloomFilterAdmissioner , SyntheticReader , LRU
2+
3+ BloomFilter = BloomFilterAdmissioner ()
4+ lru_without_admission = LRU (
5+ cache_size = 1024 ,
6+ # admissioner=BloomFilter
7+ )
8+ lru_with_admission = LRU (
9+ cache_size = 1024 ,
10+ admissioner = BloomFilter
11+ )
12+
13+ reader = SyntheticReader (
14+ num_of_req = 100_000 ,
15+ num_objects = 10_000 ,
16+ obj_size = 100 ,
17+ alpha = 0.8 ,
18+ dist = "zipf" ,
19+ )
20+
21+ without_admission_hits = 0
22+ with_admission_hits = 0
23+
24+ for req in reader :
25+ if lru_without_admission .get (req ):
26+ without_admission_hits += 1
27+ if lru_with_admission .get (req ):
28+ with_admission_hits += 1
29+
30+ print (f'Obtained { without_admission_hits } without using cache admission' )
31+ print (f'Obtained { with_admission_hits } using cache admission' )
Original file line number Diff line number Diff line change 1+ from libcachesim import PluginAdmissioner , SyntheticReader , LRU
2+ import random
3+
4+ '''
5+ A toy example where we admit ten percent of all requests
6+ at random. The admit rate is tracked and printed in the
7+ free hook to serve as a final sanity check.
8+ '''
9+
10+
11+ class AdmissionerStats :
12+ admitted_requests : int = 0
13+ total_requests : int = 0
14+
15+
16+ def init_hook ():
17+ return AdmissionerStats ()
18+
19+
20+ def admit_hook (data , request ):
21+ admit = random .randint (1 , 10 ) == 5
22+ if admit :
23+ data .admitted_requests += 1
24+ data .total_requests += 1
25+ return admit
26+
27+
28+ def clone_hook ():
29+ pass
30+
31+
32+ def update_hook (data , request , cs ):
33+ pass
34+
35+
36+ def free_hook (data ):
37+ print (f'Admit rate: { 100 * data .admitted_requests / data .total_requests } %' )
38+
39+
40+ custom_admissioner = PluginAdmissioner (
41+ "AdmitTenPercent" ,
42+ init_hook ,
43+ admit_hook ,
44+ clone_hook ,
45+ update_hook ,
46+ free_hook ,
47+ )
48+ lru_cache = LRU (
49+ cache_size = 1024 ,
50+ admissioner = custom_admissioner
51+ )
52+
53+ reader = SyntheticReader (
54+ num_of_req = 100_000 ,
55+ num_objects = 10_000 ,
56+ obj_size = 100 ,
57+ alpha = 0.8 ,
58+ dist = "zipf" ,
59+ )
60+
61+ for req in reader :
62+ lru_cache .get (req )
63+
64+ # Invokes free_hook, percentage should be ~10%
65+ del lru_cache
You can’t perform that action at this time.
0 commit comments