-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_cache.py
More file actions
542 lines (443 loc) · 13.3 KB
/
test_cache.py
File metadata and controls
542 lines (443 loc) · 13.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
"""
Test cases for cache implementations in libCacheSim Python bindings.
This module tests various cache algorithms and their functionality.
"""
import pytest
import tempfile
import os
from libcachesim import (
# Basic algorithms
LHD,
LRU,
FIFO,
LFU,
ARC,
Clock,
Random,
# Advanced algorithms
S3FIFO,
Sieve,
LIRS,
TwoQ,
SLRU,
WTinyLFU,
# Request and other utilities
Request,
ReqOp,
SyntheticReader,
)
# Try to import optional algorithms that might not be available
try:
from libcachesim import LeCaR, LFUDA, ClockPro, Cacheus
OPTIONAL_ALGORITHMS = [LeCaR, LFUDA, ClockPro, Cacheus]
except ImportError:
OPTIONAL_ALGORITHMS = []
try:
from libcachesim import Belady, BeladySize
OPTIMAL_ALGORITHMS = [Belady, BeladySize]
except ImportError:
OPTIMAL_ALGORITHMS = []
try:
from libcachesim import LRUProb, FlashProb
PROBABILISTIC_ALGORITHMS = [LRUProb, FlashProb]
except ImportError:
PROBABILISTIC_ALGORITHMS = []
try:
from libcachesim import Size, GDSF
SIZE_BASED_ALGORITHMS = [Size, GDSF]
except ImportError:
SIZE_BASED_ALGORITHMS = []
try:
from libcachesim import Hyperbolic
HYPERBOLIC_ALGORITHMS = [Hyperbolic]
except ImportError:
HYPERBOLIC_ALGORITHMS = []
class TestCacheBasicFunctionality:
"""Test basic cache functionality across different algorithms"""
@pytest.mark.parametrize(
"cache_class",
[
LHD,
LRU,
FIFO,
LFU,
ARC,
Clock,
Random,
S3FIFO,
Sieve,
LIRS,
TwoQ,
SLRU,
WTinyLFU,
LeCaR,
LFUDA,
ClockPro,
Cacheus,
LRUProb,
FlashProb,
Size,
GDSF,
Hyperbolic,
],
)
def test_cache_initialization(self, cache_class):
"""Test that all cache types can be initialized with different sizes"""
cache_sizes = [1024, 1024 * 1024, 1024 * 1024 * 1024] # 1KB, 1MB, 1GB
for size in cache_sizes:
try:
cache = cache_class(size)
assert cache is not None
assert hasattr(cache, "get")
assert hasattr(cache, "insert")
assert hasattr(cache, "find")
except Exception as e:
pytest.skip(f"Cache {cache_class.__name__} failed to initialize: {e}")
@pytest.mark.parametrize(
"cache_class", [LHD, LRU, FIFO, LFU, ARC, Clock, Random, S3FIFO, Sieve, LIRS, TwoQ, SLRU, WTinyLFU]
)
def test_basic_get_and_insert(self, cache_class):
"""Test basic get and insert operations"""
if cache_class == LHD:
pytest.skip("LHD's insert always returns None")
cache = cache_class(1024 * 1024) # 1MB cache
# Create a request
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
# Initially, object should not be in cache
hit = cache.get(req)
assert hit == False
# Insert the object
if cache_class != LIRS:
cache_obj = cache.insert(req)
assert cache_obj is not None
assert cache_obj.obj_id == 1
assert cache_obj.obj_size == 100
else:
assert cache.insert(req) is None
# Now it should be a hit
hit = cache.get(req)
assert hit == True
@pytest.mark.parametrize(
"cache_class",
[
LHD,
LRU,
FIFO,
LFU,
ARC,
Clock,
Random,
S3FIFO,
Sieve,
LIRS,
TwoQ,
SLRU,
WTinyLFU,
LeCaR,
LFUDA,
ClockPro,
Cacheus,
LRUProb,
FlashProb,
Size,
GDSF,
Hyperbolic,
],
)
def test_cache_eviction(self, cache_class):
"""Test that cache eviction works when cache is full"""
cache = cache_class(1024 * 1024) # 1MB cache
if cache_class == GDSF:
pytest.skip("GDSF should be used with find/get but not insert")
# Insert objects until cache is full
for i in range(5):
req = Request()
req.obj_id = i
req.obj_size = 50 # Each object is 50 bytes
req.op = ReqOp.OP_GET
req.next_access_vtime = 100 + i
cache.insert(req)
# Try to insert one more object
req = Request()
req.obj_id = 999
req.obj_size = 50
req.next_access_vtime = 200
req.op = ReqOp.OP_GET
cache.insert(req)
@pytest.mark.parametrize(
"cache_class",
[
LHD,
LRU,
FIFO,
LFU,
ARC,
Clock,
Random,
S3FIFO,
Sieve,
LIRS,
TwoQ,
SLRU,
WTinyLFU,
LeCaR,
LFUDA,
ClockPro,
Cacheus,
LRUProb,
FlashProb,
Size,
GDSF,
Hyperbolic,
],
)
def test_cache_find_method(self, cache_class):
"""Test the find method functionality"""
cache = cache_class(1024)
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
# Initially should not find the object
cache_obj = cache.find(req, update_cache=False)
assert cache_obj is None
# Insert the object
cache.insert(req)
# Now should find it
cache_obj = cache.find(req, update_cache=False)
assert cache_obj is not None
assert cache_obj.obj_id == 1
@pytest.mark.parametrize(
"cache_class",
[
LHD,
LRU,
FIFO,
LFU,
ARC,
Clock,
Random,
S3FIFO,
Sieve,
LIRS,
TwoQ,
SLRU,
WTinyLFU,
LeCaR,
LFUDA,
ClockPro,
Cacheus,
LRUProb,
FlashProb,
Size,
GDSF,
Hyperbolic,
],
)
def test_cache_can_insert(self, cache_class):
"""Test can_insert method"""
cache = cache_class(1024 * 1024)
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
# Should be able to insert initially
can_insert = cache.can_insert(req)
assert can_insert == True
# Insert the object
cache.insert(req)
# Try to insert a larger object that won't fit
req2 = Request()
req2.obj_id = 2
req2.obj_size = 150 # Too large for remaining space
req2.op = ReqOp.OP_GET
can_insert = cache.can_insert(req2)
# Some algorithms might still return True if they can evict
assert can_insert in [True, False]
class TestCacheEdgeCases:
"""Test edge cases and error conditions"""
def test_zero_size_cache(self):
"""Test cache with zero size"""
cache = LRU(0)
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
# Should not be able to insert
can_insert = cache.can_insert(req)
assert can_insert == False
def test_large_object(self):
"""Test inserting object larger than cache size"""
cache = LRU(100)
req = Request()
req.obj_id = 1
req.obj_size = 200 # Larger than cache
req.op = ReqOp.OP_GET
# Should not be able to insert
can_insert = cache.can_insert(req)
assert can_insert == False
def test_string_object_id(self):
"""Test with string object ID"""
req = Request()
with pytest.raises(Exception):
req.obj_id = "1"
def test_zero_size_object(self):
"""Test with zero size object"""
cache = LRU(1024)
req = Request()
req.obj_id = 1
req.obj_size = 0
req.op = ReqOp.OP_GET
# Should work fine
cache.insert(req)
hit = cache.get(req)
assert hit == True
class TestCacheWithSyntheticTrace:
"""Test cache performance with synthetic traces"""
def test_cache_with_zipf_trace(self):
"""Test cache performance with Zipf distribution"""
# Create synthetic reader with Zipf distribution
reader = SyntheticReader(num_of_req=1000, obj_size=100, alpha=1.0, dist="zipf", num_objects=100, seed=42)
# Test with different cache algorithms
cache_algorithms = [LRU, FIFO, LFU, S3FIFO, Sieve]
for cache_class in cache_algorithms:
cache = cache_class(1024) # 1KB cache
# Process the trace
miss_ratio, _ = cache.process_trace(reader)
# Basic sanity checks
assert 0.0 <= miss_ratio <= 1.0
# Reset reader for next test
reader.reset()
def test_cache_with_uniform_trace(self):
"""Test cache performance with uniform distribution"""
# Create synthetic reader with uniform distribution
reader = SyntheticReader(num_of_req=500, obj_size=50, dist="uniform", num_objects=50, seed=123)
cache = LRU(512) # 512B cache
# Process the trace
miss_ratio, _ = cache.process_trace(reader)
# Basic sanity checks
assert 0.0 <= miss_ratio <= 1.0
class TestCacheStatistics:
"""Test cache statistics and metrics"""
def test_cache_occupied_bytes(self):
"""Test get_occupied_byte method"""
cache = LRU(1024)
# Initially should be 0
occupied = cache.get_occupied_byte()
assert occupied == 0
# Insert an object
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
cache.insert(req)
# Should reflect the inserted object size
occupied = cache.get_occupied_byte()
assert occupied >= 100 # May include metadata overhead
def test_cache_object_count(self):
"""Test get_n_obj method"""
cache = LRU(1024)
# Initially should be 0
n_obj = cache.get_n_obj()
assert n_obj == 0
# Insert objects
for i in range(3):
req = Request()
req.obj_id = i
req.obj_size = 100
req.op = ReqOp.OP_GET
cache.insert(req)
# Should have 3 objects
n_obj = cache.get_n_obj()
assert n_obj == 3
def test_cache_print(self):
"""Test print_cache method"""
cache = LRU(1024)
# Insert an object
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
cache.insert(req)
# Should return a string representation
cache.print_cache()
class TestCacheOperations:
"""Test various cache operations"""
def test_cache_remove(self):
"""Test remove method"""
cache = LRU(1024)
# Insert an object
req = Request()
req.obj_id = 1
req.obj_size = 100
req.op = ReqOp.OP_GET
cache.insert(req)
# Verify it's in cache
hit = cache.get(req)
assert hit == True
# Remove it
removed = cache.remove(1)
assert removed == True
# Verify it's no longer in cache
hit = cache.get(req)
assert hit == False
def test_cache_need_eviction(self):
"""Test need_eviction method"""
cache = LRU(200)
# Insert objects until cache is nearly full
for i in range(3):
req = Request()
req.obj_id = i
req.obj_size = 50
req.op = ReqOp.OP_GET
cache.insert(req)
# Try to insert a larger object
req = Request()
req.obj_id = 999
req.obj_size = 100
req.op = ReqOp.OP_GET
# Should need eviction
need_eviction = cache.need_eviction(req)
assert need_eviction == True
def test_cache_to_evict(self):
"""Test to_evict method"""
cache = LRU(200)
# Insert objects
for i in range(3):
req = Request()
req.obj_id = i
req.obj_size = 50
req.op = ReqOp.OP_GET
cache.insert(req)
# Try to insert a larger object
req = Request()
req.obj_id = 999
req.obj_size = 100
req.op = ReqOp.OP_GET
# Should return an object to evict
evict_obj = cache.to_evict(req)
assert evict_obj is not None
assert hasattr(evict_obj, "obj_id")
class TestCacheOptionalAlgorithms:
"""Test optional algorithms"""
@pytest.mark.optional
def test_glcache(self):
"""Test GLCache algorithm"""
from libcachesim import GLCache
cache = GLCache(1024)
assert cache is not None
@pytest.mark.optional
def test_lrb(self):
"""Test LRB algorithm"""
from libcachesim import LRB
cache = LRB(1024)
assert cache is not None
@pytest.mark.optional
def test_3lcache(self):
"""Test 3LCache algorithm"""
from libcachesim import ThreeLCache
cache = ThreeLCache(1024)
assert cache is not None