@@ -88,29 +88,42 @@ func TestS3FIFO_CapacityAccuracy(t *testing.T) {
8888}
8989
9090func TestS3FIFO_Eviction (t * testing.T ) {
91- cache := newS3FIFO [int , int ](& config {size : 10000 })
91+ // Use 65536 to get proper S3-FIFO behavior (4096 entries per shard)
92+ cache := newS3FIFO [int , int ](& config {size : 65536 })
9293
9394 // Fill cache to capacity
94- for i := range 10000 {
95+ for i := range 65536 {
9596 cache .set (i , i , 0 )
9697 }
9798
98- // Access item 0 to mark it for promotion
99- cache .get (0 )
99+ // Access items 0-99 multiple times to mark them as hot
100+ for range 3 {
101+ for i := range 100 {
102+ cache .get (i )
103+ }
104+ }
100105
101- // Add more items to trigger evictions - item 0 should survive
102- for i := 10000 ; i < 15000 ; i ++ {
106+ // Add more items to trigger evictions - hot items should survive
107+ for i := 65536 ; i < 130000 ; i ++ {
103108 cache .set (i , i , 0 )
104109 }
105110
106- // Item 0 should still exist (it was accessed before evictions)
107- if _ , ok := cache .get (0 ); ! ok {
108- t .Error ("item 0 was evicted but should have been promoted" )
111+ // Count how many hot items survived
112+ hotSurvived := 0
113+ for i := range 100 {
114+ if _ , ok := cache .get (i ); ok {
115+ hotSurvived ++
116+ }
117+ }
118+
119+ // Most hot items should survive (at least 75%)
120+ if hotSurvived < 75 {
121+ t .Errorf ("only %d/100 hot items survived; want >= 75" , hotSurvived )
109122 }
110123
111124 // Should be near capacity (allow 10% variance)
112- if cache .len () < 9000 || cache .len () > 11000 {
113- t .Errorf ("cache length = %d; want ~10000 " , cache .len ())
125+ if cache .len () < 58000 || cache .len () > 72000 {
126+ t .Errorf ("cache length = %d; want ~65536 " , cache .len ())
114127 }
115128}
116129
@@ -191,43 +204,51 @@ func TestS3FIFO_Concurrent(t *testing.T) {
191204}
192205
193206func TestS3FIFO_FrequencyPromotion (t * testing.T ) {
194- // Use a larger capacity to ensure meaningful per-shard capacity
195- // With 512 shards, 10000 items = ~20 per shard
196- cache := newS3FIFO [int , int ](& config {size : 10000 })
207+ // Use 65536 to get proper S3-FIFO behavior (4096 entries per shard)
208+ cache := newS3FIFO [int , int ](& config {size : 65536 })
197209
198210 // Fill cache with items using int keys for predictable sharding
199- for i := range 10000 {
211+ for i := range 65536 {
200212 cache .set (i , i , 0 )
201213 }
202214
203- // Access even-numbered keys to increase their frequency
204- for i := 0 ; i < 10000 ; i += 2 {
205- cache .get (i )
215+ // Access first 1000 keys multiple times to mark them as hot
216+ for range 3 {
217+ for i := range 1000 {
218+ cache .get (i )
219+ }
206220 }
207221
208- // Add more items to trigger evictions
209- for i := 10000 ; i < 15000 ; i ++ {
222+ // Add more items to trigger significant evictions (2x capacity)
223+ for i := 65536 ; i < 130000 ; i ++ {
210224 cache .set (i , i , 0 )
211225 }
212226
213- // Count how many accessed items survived vs unaccessed
214- accessedSurvived := 0
215- unaccesedSurvived := 0
216- for i := range 10000 {
227+ // Count how many hot items survived vs cold items
228+ hotSurvived := 0
229+ coldSurvived := 0
230+ for i := range 65536 {
217231 if _ , ok := cache .get (i ); ok {
218- if i % 2 == 0 {
219- accessedSurvived ++
232+ if i < 1000 {
233+ hotSurvived ++
220234 } else {
221- unaccesedSurvived ++
235+ coldSurvived ++
222236 }
223237 }
224238 }
225239
226- // Accessed items should survive at higher rate than unaccessed
240+ // Calculate survival rates
241+ hotRate := float64 (hotSurvived ) / 1000.0
242+ coldRate := float64 (coldSurvived ) / 64536.0
243+
244+ t .Logf ("Hot survived: %d/1000 (%.1f%%), Cold survived: %d/64536 (%.1f%%)" ,
245+ hotSurvived , hotRate * 100 , coldSurvived , coldRate * 100 )
246+
247+ // Hot items should survive at higher rate than cold items
227248 // This verifies the frequency promotion mechanism works
228- if accessedSurvived <= unaccesedSurvived {
229- t .Errorf ("accessed items (%d ) should survive more than unaccessed (%d )" ,
230- accessedSurvived , unaccesedSurvived )
249+ if hotRate <= coldRate {
250+ t .Errorf ("hot item survival rate (%.1f%% ) should exceed cold item rate (%.1f%% )" ,
251+ hotRate * 100 , coldRate * 100 )
231252 }
232253}
233254
@@ -373,43 +394,50 @@ func TestS3FIFOEvictionOrder(t *testing.T) {
373394
374395// Test S3-FIFO vs LRU: hot items survive, cold items evicted
375396func TestS3FIFODetailed (t * testing.T ) {
376- cache := New [int , int ](Size (40 ))
397+ // Use 65536 capacity for proper S3-FIFO behavior with tiered sharding
398+ const cacheSize = 65536
399+ cache := New [int , int ](Size (cacheSize ))
377400
378- // Insert items 1-40 into cache
379- for i := 1 ; i <= 40 ; i ++ {
401+ // Insert items 1-cacheSize into cache
402+ for i := 1 ; i <= cacheSize ; i ++ {
380403 cache .Set (i , i * 100 , 0 )
381404 }
382405
383- // Access items 1-20 (marks them as hot)
384- for i := 1 ; i <= 20 ; i ++ {
385- cache .Get (i )
406+ // Access items 1-1000 multiple times (marks them as hot)
407+ const hotItems = 1000
408+ for range 3 {
409+ for i := 1 ; i <= hotItems ; i ++ {
410+ cache .Get (i )
411+ }
386412 }
387413
388- // Insert one-hit wonders 100-119
389- for i := 100 ; i < 120 ; i ++ {
414+ // Insert one-hit wonders to trigger eviction
415+ for i := cacheSize + 1 ; i <= cacheSize + 20000 ; i ++ {
390416 cache .Set (i , i * 100 , 0 )
391417 }
392418
393- // Check which items survived
419+ // Check which hot items survived
394420 hotSurvived := 0
395- for i := 1 ; i <= 20 ; i ++ {
421+ for i := 1 ; i <= hotItems ; i ++ {
396422 if _ , found := cache .Get (i ); found {
397423 hotSurvived ++
398424 }
399425 }
400426
427+ // Check which cold items survived (items that were never accessed)
401428 coldSurvived := 0
402- for i := 21 ; i <= 40 ; i ++ {
429+ for i := hotItems + 1 ; i <= hotItems + 1000 ; i ++ {
403430 if _ , found := cache .Get (i ); found {
404431 coldSurvived ++
405432 }
406433 }
407434
408- t .Logf ("Hot items found: %d/20 , Cold items found: %d/20 " , hotSurvived , coldSurvived )
435+ t .Logf ("Hot items found: %d/%d , Cold items found: %d/1000 " , hotSurvived , hotItems , coldSurvived )
409436
410437 // Verify expected behavior - hot items should mostly survive
411- if hotSurvived < 15 {
412- t .Errorf ("Expected most hot items to survive, got %d/20" , hotSurvived )
438+ // With proper S3-FIFO, at least 75% of hot items should survive
439+ if hotSurvived < hotItems * 3 / 4 {
440+ t .Errorf ("Expected most hot items to survive, got %d/%d" , hotSurvived , hotItems )
413441 }
414442}
415443
0 commit comments