-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinycache_test.go
More file actions
574 lines (476 loc) · 13 KB
/
tinycache_test.go
File metadata and controls
574 lines (476 loc) · 13 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package tinycache
import (
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
"time"
)
func TestNewCacheWithDefaultTTL(t *testing.T) {
cache := New[int](WithTTL(5 * time.Second))
if cache.defaultTTL != 5*time.Second {
t.Errorf("expected defaultTTL to be 5s, got %v", cache.defaultTTL)
}
}
func TestNewCacheWithReapInterval(t *testing.T) {
cache := New[int](WithReapInterval(1 * time.Second))
defer cache.Close()
time.Sleep(2 * time.Second)
cache.SetTTL("key", 42, -1*time.Second)
time.Sleep(2 * time.Second)
if _, ok := cache.Get("key"); ok {
t.Errorf("expected key to be deleted by reaper")
}
}
func TestCacheSetAndGet(t *testing.T) {
cache := New[int](WithTTL(5 * time.Second))
cache.Set("key", 42)
value, ok := cache.Get("key")
if !ok || value != 42 {
t.Errorf("expected to get 42, got %v", value)
}
}
func TestCacheSetTTL(t *testing.T) {
cache := New[int](WithTTL(5 * time.Second))
cache.SetTTL("key", 42, 1*time.Second)
value, ok := cache.Get("key")
if !ok || value != 42 {
t.Errorf("expected to get 42, got %v", value)
}
time.Sleep(2 * time.Second)
if _, ok := cache.Get("key"); ok {
t.Errorf("expected key to be expired")
}
}
func TestCacheSetPermanent(t *testing.T) {
cache := New[int](WithTTL(1 * time.Second))
// Regular entry should expire
cache.Set("temp-key", 100)
// Permanent entry should never expire
cache.SetPermanent("perm-key", 42)
// Zero TTL should also create permanent entry
cache.SetTTL("zero-ttl-key", 84, 0)
// Wait for regular entry to expire
time.Sleep(2 * time.Second)
// Check regular entry is gone
if _, ok := cache.Get("temp-key"); ok {
t.Errorf("expected temporary key to be expired")
}
// Check permanent entry is still there
value, ok := cache.Get("perm-key")
if !ok || value != 42 {
t.Errorf("expected permanent key to still exist with value 42, got %v, exists: %v", value, ok)
}
// Check zero TTL entry is still there
value, ok = cache.Get("zero-ttl-key")
if !ok || value != 84 {
t.Errorf("expected zero TTL key to still exist with value 84, got %v, exists: %v", value, ok)
}
// Reap shouldn't affect permanent entries
cache.Reap()
value, ok = cache.Get("perm-key")
if !ok || value != 42 {
t.Errorf("expected permanent key to still exist after reap, got %v, exists: %v", value, ok)
}
}
func TestCacheZeroDefaultTTL(t *testing.T) {
// Create cache with zero default TTL (all entries permanent by default)
cache := New[int](WithTTL(0))
cache.Set("key1", 42)
cache.Set("key2", 84)
// Wait some time
time.Sleep(2 * time.Second)
// Check entries still exist
value, ok := cache.Get("key1")
if !ok || value != 42 {
t.Errorf("expected key1 to exist with value 42, got %v, exists: %v", value, ok)
}
value, ok = cache.Get("key2")
if !ok || value != 84 {
t.Errorf("expected key2 to exist with value 84, got %v, exists: %v", value, ok)
}
// Run reaper
cache.Reap()
// Check entries still exist after reap
value, ok = cache.Get("key1")
if !ok || value != 42 {
t.Errorf("expected key1 to still exist after reap, got %v, exists: %v", value, ok)
}
}
func TestCacheNegativeTTL(t *testing.T) {
cache := New[int](WithTTL(5 * time.Second))
// Negative TTL should expire immediately
cache.SetTTL("key", 42, -1*time.Second)
// Should be expired already
if _, ok := cache.Get("key"); ok {
t.Errorf("expected key with negative TTL to be expired immediately")
}
}
func TestCacheReap(t *testing.T) {
cache := New[int](WithTTL(5 * time.Second))
cache.SetTTL("key", 42, -1*time.Second)
cache.Reap()
if _, ok := cache.Get("key"); ok {
t.Errorf("expected key to be deleted by reaper")
}
}
func TestCacheSetAndGetStruct(t *testing.T) {
type testStruct struct {
Name string
Value int
}
cache := New[testStruct](WithTTL(5 * time.Second))
expected := testStruct{Name: "test", Value: 42}
cache.Set("key", expected)
value, ok := cache.Get("key")
if !ok {
t.Errorf("expected to get a value, but got none")
}
if value.Name != expected.Name || value.Value != expected.Value {
t.Errorf("expected to get %v, got %v", expected, value)
}
}
func TestCacheConcurrentSetAndGet(t *testing.T) {
cache := New[int](WithTTL(5 * time.Second))
var wg sync.WaitGroup
numGoroutines := 100
numOperations := 1000
// Concurrently set values
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < numOperations; j++ {
cache.Set(fmt.Sprintf("key-%d-%d", id, j), id*j)
}
}(i)
}
// Concurrently get values
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < numOperations; j++ {
cache.Get(fmt.Sprintf("key-%d-%d", id, j))
}
}(i)
}
wg.Wait()
// Verify some values
for i := 0; i < numGoroutines; i++ {
for j := 0; j < numOperations; j++ {
value, ok := cache.Get(fmt.Sprintf("key-%d-%d", i, j))
if !ok {
t.Errorf("expected to get a value for key-%d-%d, but got none", i, j)
}
if value != i*j {
t.Errorf("expected to get %d for key-%d-%d, but got %d", i*j, i, j, value)
}
}
}
}
func TestCloseWithNilChannel(t *testing.T) {
// Create a cache without a reaper (so closeCh is nil)
cache := New[int]()
// This should not panic
cache.Close()
}
// ******* BENCHMARKS *******
// Small struct for testing struct caching performance
type benchStruct struct {
ID int
Name string
Value float64
Created time.Time
Metadata map[string]string
}
// Create a medium-sized struct for benchmarking
func newBenchStruct(id int) benchStruct {
return benchStruct{
ID: id,
Name: fmt.Sprintf("test-item-%d", id),
Value: float64(id) * 1.5,
Created: time.Now(),
Metadata: map[string]string{
"type": "benchmark",
"description": "A test item for benchmarking struct performance",
"category": strconv.Itoa(id % 10),
},
}
}
// Basic set/get benchmark for integers
func BenchmarkIntCache_Set(b *testing.B) {
cache := New[int](WithTTL(5 * time.Minute))
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Set(strconv.Itoa(i), i)
}
}
func BenchmarkIntCache_Get(b *testing.B) {
cache := New[int](WithTTL(5 * time.Minute))
// Pre-populate the cache
for i := 0; i < 10000; i++ {
cache.Set(strconv.Itoa(i), i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Get a mix of existing and non-existing keys
cache.Get(strconv.Itoa(i % 20000))
}
}
// Benchmark for struct caching
func BenchmarkStructCache_Set(b *testing.B) {
cache := New[benchStruct](WithTTL(5 * time.Minute))
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Set(strconv.Itoa(i), newBenchStruct(i))
}
}
func BenchmarkStructCache_Get(b *testing.B) {
cache := New[benchStruct](WithTTL(5 * time.Minute))
// Pre-populate the cache
for i := 0; i < 10000; i++ {
cache.Set(strconv.Itoa(i), newBenchStruct(i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Get a mix of existing and non-existing keys
cache.Get(strconv.Itoa(i % 20000))
}
}
// Benchmark TTL expiration handling
func BenchmarkCache_GetWithExpiredTTL(b *testing.B) {
cache := New[int](WithTTL(1 * time.Nanosecond))
// Pre-populate the cache with already expired items
for i := 0; i < 10000; i++ {
cache.Set(strconv.Itoa(i), i)
}
// Wait to ensure expiration
time.Sleep(1 * time.Millisecond)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Getting expired items forces expiration check
cache.Get(strconv.Itoa(i % 10000))
}
}
// Benchmark the Reap function
func BenchmarkCache_Reap(b *testing.B) {
// Create sets of 10,000 entries for each iteration
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
cache := New[int](WithTTL(1 * time.Nanosecond))
// Add many items
for j := 0; j < 10000; j++ {
cache.Set(strconv.Itoa(j), j)
}
// Wait to ensure expiration
time.Sleep(1 * time.Millisecond)
b.StartTimer()
// Benchmark the reap operation
cache.Reap()
}
}
// Benchmark permanent entries
func BenchmarkCache_GetPermanent(b *testing.B) {
cache := New[int](WithTTL(5 * time.Minute))
// Pre-populate with permanent entries
for i := 0; i < 10000; i++ {
cache.SetPermanent(strconv.Itoa(i), i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Get(strconv.Itoa(i % 10000))
}
}
// Benchmark concurrent access
func BenchmarkCache_ConcurrentAccess(b *testing.B) {
cache := New[int](WithTTL(5 * time.Minute))
// Pre-populate
for i := 0; i < 10000; i++ {
cache.Set(strconv.Itoa(i), i)
}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
// Create a local random source for each goroutine
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for pb.Next() {
// 80% reads, 20% writes to simulate typical cache usage
if r.Float32() < 0.8 {
cache.Get(strconv.Itoa(r.Intn(20000)))
} else {
cache.Set(strconv.Itoa(r.Intn(20000)), r.Int())
}
}
})
}
// Benchmark entry reuse via sync.Pool
func BenchmarkCache_EntryReuse(b *testing.B) {
cache := New[int](WithTTL(1 * time.Millisecond))
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Set, wait for expiration, then set again to trigger pool reuse
key := "test-key"
cache.Set(key, i)
if i%100 == 0 {
// Periodically force expiration to trigger pool reuse
time.Sleep(2 * time.Millisecond)
cache.Reap()
}
// Set the same key again, which should reuse the entry from the pool
cache.Set(key, i+1)
}
}
// Benchmark comparing different sized values
func BenchmarkCache_DifferentSizes(b *testing.B) {
// Test with small values
b.Run("SmallValue", func(b *testing.B) {
cache := New[int](WithTTL(5 * time.Minute))
for i := 0; i < b.N; i++ {
cache.Set(strconv.Itoa(i), i)
cache.Get(strconv.Itoa(i))
}
})
// Test with medium strings
b.Run("MediumString", func(b *testing.B) {
cache := New[string](WithTTL(5 * time.Minute))
for i := 0; i < b.N; i++ {
value := fmt.Sprintf("medium-sized-string-value-%d-for-testing-performance", i)
cache.Set(strconv.Itoa(i), value)
cache.Get(strconv.Itoa(i))
}
})
// Test with large structs
b.Run("LargeStruct", func(b *testing.B) {
type largeStruct struct {
ID int
Name string
Values [100]float64
Tags []string
Timestamp time.Time
Active bool
Metadata map[string]interface{}
}
cache := New[largeStruct](WithTTL(5 * time.Minute))
for i := 0; i < b.N; i++ {
// Create a large struct with arrays and maps
value := largeStruct{
ID: i,
Name: fmt.Sprintf("large-struct-%d", i),
Timestamp: time.Now(),
Active: true,
Tags: []string{"benchmark", "performance", "testing", "large"},
Metadata: map[string]interface{}{
"description": "A large struct for benchmarking",
"version": 1.0,
"created": time.Now().String(),
"nested": map[string]string{
"level1": "value1",
"level2": "value2",
},
},
}
// Fill the array with values
for j := 0; j < 100; j++ {
value.Values[j] = float64(i) * float64(j)
}
cache.Set(strconv.Itoa(i), value)
cache.Get(strconv.Itoa(i))
}
})
}
// Compare map with mutex vs sync.Map for different workloads
func BenchmarkSyncMapVsMapMutex(b *testing.B) {
// Helper for testing a map with mutex
type mutexCache struct {
mu sync.RWMutex
store map[string]int
}
// Test with read-heavy workload (90% reads)
b.Run("ReadHeavy", func(b *testing.B) {
// Test sync.Map
b.Run("SyncMap", func(b *testing.B) {
var sm sync.Map
for i := 0; i < 1000; i++ {
sm.Store(strconv.Itoa(i), i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if rand.Float32() < 0.9 {
// 90% reads
sm.Load(strconv.Itoa(rand.Intn(2000)))
} else {
// 10% writes
sm.Store(strconv.Itoa(rand.Intn(2000)), i)
}
}
})
// Test map with mutex
b.Run("MapMutex", func(b *testing.B) {
mc := mutexCache{
store: make(map[string]int, 1000),
}
for i := 0; i < 1000; i++ {
mc.store[strconv.Itoa(i)] = i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if rand.Float32() < 0.9 {
// 90% reads
mc.mu.RLock()
_ = mc.store[strconv.Itoa(rand.Intn(2000))]
mc.mu.RUnlock()
} else {
// 10% writes
mc.mu.Lock()
mc.store[strconv.Itoa(rand.Intn(2000))] = i
mc.mu.Unlock()
}
}
})
})
// Test with write-heavy workload (50% writes)
b.Run("WriteHeavy", func(b *testing.B) {
// Test sync.Map
b.Run("SyncMap", func(b *testing.B) {
var sm sync.Map
for i := 0; i < 1000; i++ {
sm.Store(strconv.Itoa(i), i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if rand.Float32() < 0.5 {
// 50% reads
sm.Load(strconv.Itoa(rand.Intn(2000)))
} else {
// 50% writes
sm.Store(strconv.Itoa(rand.Intn(2000)), i)
}
}
})
// Test map with mutex
b.Run("MapMutex", func(b *testing.B) {
mc := mutexCache{
store: make(map[string]int, 1000),
}
for i := 0; i < 1000; i++ {
mc.store[strconv.Itoa(i)] = i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if rand.Float32() < 0.5 {
// 50% reads
mc.mu.RLock()
_ = mc.store[strconv.Itoa(rand.Intn(2000))]
mc.mu.RUnlock()
} else {
// 50% writes
mc.mu.Lock()
mc.store[strconv.Itoa(rand.Intn(2000))] = i
mc.mu.Unlock()
}
}
})
})
}