-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
784 lines (639 loc) · 18.3 KB
/
cache_test.go
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
package gw_cache
import (
"encoding/json"
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
"time"
"github.com/geniussportsgroup/gateway_cache/v2/mocks"
"github.com/geniussportsgroup/gateway_cache/v2/models"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
)
type RequestEntry struct {
N int
Time time.Time
PutValue string
}
type URequest struct {
Request *RequestEntry
PutValue string
}
type MyProcessor struct {
putValue string
}
type UResponse struct {
Urequest *URequest
}
type Response struct {
Uresponse *UResponse
Poem string
}
func (p *MyProcessor) ToMapKey(entry *RequestEntry) (string, error) {
b, err := json.Marshal(entry)
if err != nil {
return "", err
}
return string(b), nil
}
func (p *MyProcessor) CacheMissSolver(request *RequestEntry) ([]byte, *models.RequestError) {
entry := request
urequest := &URequest{
Request: entry,
PutValue: entry.PutValue + "-" + p.putValue,
}
uresponse := &UResponse{Urequest: urequest}
response := &Response{
Uresponse: uresponse,
Poem: keats,
}
b, err := json.Marshal(*response)
if err != nil {
return nil, &models.RequestError{
Error: err,
Code: Status5xx,
}
}
return b, nil
}
func TestNew(t *testing.T) {
mp := &MyProcessor{}
cache := New[*RequestEntry, []byte](
100,
.4,
time.Minute,
mp,
)
assert.Equal(t, 100, cache.capacity)
assert.Equal(t, time.Minute, cache.ttl)
assert.Equal(t, 0, cache.hitCount)
assert.Equal(t, 0, cache.missCount)
assert.Equal(t, 0, cache.numEntries)
assert.Less(t, cache.capacity, cache.extendedCapacity)
}
func TestBadFactor(t *testing.T) {
mp := &MyProcessor{}
assert.Panics(t, func() {
New[*RequestEntry, []byte](100, .099, time.Minute, mp)
})
assert.Panics(t, func() {
New[*RequestEntry, []byte](100, 3.00001, time.Minute, mp)
})
}
const Capacity = 31
const TTL = 15 * time.Second
func TestWithCompress(t *testing.T) {
transformer := mocks.NewTransformerI[any](t)
processor := mocks.NewProcessorI[any, any](t)
compressor := mocks.NewCompressorI(t)
processor.EXPECT().ToMapKey(mock.Anything).Return("Keats", nil).Times(1)
processor.EXPECT().CacheMissSolver(mock.Anything).Return(keats, nil).Times(1)
transformer.EXPECT().ValueToBytes(keats).Return([]byte(keats), nil).Times(1)
compressedResponse := []byte("compressed")
compressor.EXPECT().Compress([]byte(keats)).Return(compressedResponse, nil).Times(1)
cache := NewWithCompression[any, any](Capacity, .4, 3*time.Minute, processor, transformer)
cache.compressor = compressor
val, ptr := cache.RetrieveFromCacheOrCompute("Keats")
assert.Nil(t, ptr)
assert.Equal(t, val, keats)
processor.EXPECT().ToMapKey(mock.Anything).Return("Keats", nil).Times(1)
compressor.EXPECT().Decompress(compressedResponse).Return([]byte(keats), nil).Times(1)
transformer.EXPECT().BytesToValue([]byte(keats)).Return(keats, nil).Times(1)
val, ptr = cache.RetrieveFromCacheOrCompute("Keats")
assert.Nil(t, ptr)
assert.Equal(t, val, keats)
}
func insertEntry[T any](
cache *CacheDriver[T, T],
processor *mocks.ProcessorI[T, T],
request T,
) (T, *models.RequestError) {
s, _ := json.Marshal(request)
processor.EXPECT().ToMapKey(request).Return(string(s), nil).Times(1)
processor.EXPECT().CacheMissSolver(request).Return(request, nil).Times(1)
return cache.RetrieveFromCacheOrCompute(request)
}
func createCacheWithCapEntriesInside(
capacity int,
processor *mocks.ProcessorI[*RequestEntry, *RequestEntry],
) (*CacheDriver[*RequestEntry, *RequestEntry], map[*RequestEntry]bool) {
cache := New[*RequestEntry, *RequestEntry](capacity, .4, TTL, processor)
requestTbl := make(map[*RequestEntry]bool)
for i := 0; i < capacity; i++ {
request := &RequestEntry{
N: i + 10,
Time: time.Now(),
}
insertEntry(cache, processor, request)
requestTbl[request] = true
}
return cache, requestTbl
}
func createCompressedCacheWithCapEntriesInside(
capacity int,
processor *mocks.ProcessorI[*RequestEntry, *RequestEntry],
) (*CacheDriver[*RequestEntry, *RequestEntry], map[*RequestEntry]bool) {
transformer := &DefaultTransformer[*RequestEntry]{}
cache := NewWithCompression[*RequestEntry, *RequestEntry](capacity, .4, TTL, processor, transformer)
requestTbl := make(map[*RequestEntry]bool)
for i := 0; i < capacity; i++ {
request := &RequestEntry{
N: i + 10,
Time: time.Now(),
}
insertEntry(cache, processor, request)
requestTbl[request] = true
}
return cache, requestTbl
}
func TestEvictions(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(Capacity, processor)
// now we insert Capacity new entries which should evict all the previously inserted ones
for i := Capacity; i < 2*Capacity; i++ {
request := &RequestEntry{
N: i + 10,
Time: time.Now(),
}
b, requestError := insertEntry(cache, processor, request)
assert.Nil(t, requestError)
assert.NotNil(t, b)
}
// now we verify that entries en tbl are not in the cache
for req := range tbl {
processor.EXPECT().ToMapKey(req).Return(strconv.Itoa(req.Time.Nanosecond()), nil).Times(1)
assert.False(t, cache.has(req))
}
}
func TestCacheDriver_Has(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(Capacity, processor)
for req := range tbl {
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
assert.True(t, cache.has(req))
}
}
func TestLRUOrder(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, _ := createCacheWithCapEntriesInside(
Capacity,
processor,
)
it := cache.NewCacheIt()
prevTimeStamp := it.GetCurr().timestamp
for it.Next(); it.HasCurr(); it.Next() {
curr := it.GetCurr()
assert.True(t, prevTimeStamp.After(curr.timestamp))
prevTimeStamp = curr.timestamp
}
}
func TestCacheDriver_testTTL(t *testing.T) {
ttl := 3 * time.Second
processor := mocks.NewProcessorI[any, any](t)
cache := New[any, any](Capacity, .4, ttl, processor)
request := &RequestEntry{
N: 10,
Time: time.Now(),
}
processor.EXPECT().ToMapKey(request).Return(strconv.Itoa(request.Time.Nanosecond()), nil).Times(2)
processor.EXPECT().CacheMissSolver(request).Return(request, nil).Times(1)
b, requestError := cache.RetrieveFromCacheOrCompute(request)
assert.Nil(t, requestError)
assert.NotNil(t, b)
time.Sleep(ttl) // wait for tt expiration
assert.False(t, cache.has(request))
}
func TestRandomTouches(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(
2,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, N)
i := 0
for req := range tbl {
requests[i] = req
i++
}
// var response *RequestEntry
for i := range rand.Perm(N) {
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
b, requestError := cache.RetrieveFromCacheOrCompute(req)
assert.Nil(t, requestError)
assert.Equal(t, cache.getMru().postProcessedResponse, b)
}
}
func TestCompressRandomTouches(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCompressedCacheWithCapEntriesInside(
2,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, N)
i := 0
for req := range tbl {
requests[i] = req
i++
}
// var response *RequestEntry
for i := range rand.Perm(N) {
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
b, requestError := cache.RetrieveFromCacheOrCompute(req)
assert.Nil(t, requestError)
decompressedReponse, _ := cache.compressor.Decompress(cache.getMru().postProcessedResponseCompressed)
data, _ := cache.transformer.BytesToValue(decompressedReponse)
assert.Equal(t, data, b)
}
}
func TestCacheDriver_CacheState(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(
2,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
// some random touches
for i := 0; i < 100; i++ {
i := rand.Intn(N)
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
_, _ = cache.RetrieveFromCacheOrCompute(req)
}
state, err := cache.GetState()
assert.Nil(t, err)
assert.NotNil(t, state)
}
func TestCacheDriver_Clean(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(
Capacity,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
// some random touches
for i := 0; i < 100; i++ {
i := rand.Intn(N)
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
_, _ = cache.RetrieveFromCacheOrCompute(req)
}
err := cache.Clean()
assert.Nil(t, err)
assert.Equal(t, 0, cache.missCount)
assert.Equal(t, 0, cache.hitCount)
assert.Equal(t, 0, cache.numEntries)
assert.Equal(t, Capacity, cache.capacity)
assert.Equal(t, TTL, cache.ttl)
state, _ := cache.GetState()
var s CacheState
err = json.Unmarshal([]byte(state), &s)
assert.Equal(t, 0, s.NumEntries)
assert.Equal(t, 0, s.HitCount)
assert.Equal(t, 0, s.MissCount)
}
func TestCacheDriver_HitCost(t *testing.T) {
processor := &MyProcessor{}
cache := New[*RequestEntry, []byte](Capacity, .4, TTL, processor)
requestTbl := make(map[*RequestEntry]bool)
for i := 0; i < Capacity; i++ {
request := &RequestEntry{
N: i + 10,
Time: time.Now(),
}
cache.RetrieveFromCacheOrCompute(request)
requestTbl[request] = true
}
// 1,
// proccessor,
// )
N := len(requestTbl)
requests := make([]*RequestEntry, 0, N)
for req := range requestTbl {
requests = append(requests, req)
}
// some random touches
for i := 0; i < 1000000; i++ {
req := requests[0]
_, err := cache.RetrieveFromCacheOrCompute(req)
assert.Nil(t, err)
}
}
func TestConcurrency(t *testing.T) {
const ConcurrencyLevel = 20
const SuperCap = 3037
const NumRepeatedCalls = 50
myProccessor := &MyProcessor{}
cache := New[*RequestEntry, []byte](
SuperCap,
.3,
30*time.Second,
myProccessor,
)
tbl := make(map[*RequestEntry]bool)
for i := 0; i < Capacity; i++ {
request := &RequestEntry{
N: i + 10,
Time: time.Now(),
}
_, _ = cache.RetrieveFromCacheOrCompute(request)
tbl[request] = true
}
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
for i := 0; i < 1e4; i++ {
wg := sync.WaitGroup{}
wg.Add(ConcurrencyLevel)
for k := 0; k < ConcurrencyLevel; k++ {
go func() { // goroutine emulates an avalanche of repeated requests
idx := rand.Intn(N) // choose request randomly
req := requests[idx]
// now we simulate the avalanche
for j := 0; j < NumRepeatedCalls; j++ {
go func(req *RequestEntry) {
_, requestError := cache.RetrieveFromCacheOrCompute(req)
assert.Nil(t, requestError)
// var response Response
// err := json.Unmarshal(b.([]byte), &response)
// assert.Nil(t, err)
}(req)
}
wg.Done()
}()
}
wg.Wait()
}
}
func TestConcurrencyAndCompress(t *testing.T) {
const ConcurrencyLevel = 10
const SuperCap = 1019
const NumRepeatedCalls = 20
myProccessor := &MyProcessor{}
defaultTransformer := &DefaultTransformer[[]byte]{}
cache := NewWithCompression[*RequestEntry, []byte](
SuperCap,
.3,
30*time.Second,
myProccessor,
defaultTransformer,
)
tbl := make(map[*RequestEntry]bool)
for i := 0; i < Capacity; i++ {
request := &RequestEntry{
N: i + 10,
Time: time.Now(),
}
_, _ = cache.RetrieveFromCacheOrCompute(request)
tbl[request] = true
}
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
for i := 0; i < 1e3; i++ {
wg := sync.WaitGroup{}
wg.Add(ConcurrencyLevel)
for k := 0; k < ConcurrencyLevel; k++ {
go func() { // goroutine emulates an avalanche of repeated requests
idx := rand.Intn(N) // choose request randomly
req := requests[idx]
// now we simulate the avalanche
for j := 0; j < NumRepeatedCalls; j++ {
go func(req *RequestEntry) {
response, requestError := cache.RetrieveFromCacheOrCompute(req)
assert.Nil(t, requestError)
ref := &Response{}
err := json.Unmarshal(response, &ref)
assert.Nil(t, err)
assert.Equal(t, ref.Uresponse.Urequest.Request.N, req.N)
assert.Equal(t, ref.Poem, keats)
}(req)
}
wg.Done()
}()
}
wg.Wait()
}
}
func TestCacheDriver_LazyRemove(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(
Capacity,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
var lastRequest *RequestEntry
for i := 0; i < 100; i++ {
i := rand.Intn(N)
req := requests[i]
lastRequest = req
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(2)
_, _ = cache.RetrieveFromCacheOrCompute(req)
isMru, err := cache.isKeyMru(req)
assert.Nil(t, err)
assert.True(t, isMru)
}
s, _ := json.Marshal(lastRequest)
processor.EXPECT().ToMapKey(lastRequest).Return(string(s), nil).Times(2)
err := cache.LazyRemove(lastRequest)
assert.Nil(t, err)
assert.False(t, cache.has(lastRequest))
}
func TestCacheDriver_Contains(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(
Capacity,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
for i := 0; i < N; i++ {
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
_, _ = cache.RetrieveFromCacheOrCompute(req)
}
for i := 0; i < N; i++ {
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(1)
ok, err := cache.Contains(req)
assert.Nil(t, err)
assert.True(t, ok)
}
}
func TestCacheDriver_Touch(t *testing.T) {
processor := mocks.NewProcessorI[*RequestEntry, *RequestEntry](t)
cache, tbl := createCacheWithCapEntriesInside(
Capacity,
processor,
)
N := len(tbl)
requests := make([]*RequestEntry, 0, N)
for req := range tbl {
requests = append(requests, req)
}
for i := 0; i < N; i++ {
req := requests[i]
s, _ := json.Marshal(req)
processor.EXPECT().ToMapKey(req).Return(string(s), nil).Times(2)
err := cache.Touch(req)
assert.Nil(t, err)
mru, err := cache.isKeyMru(req)
assert.Nil(t, err)
assert.True(t, mru)
}
}
// test the StoreValue method first insert values in the cache
func TestCacheDriver_StoreOrUpdateValue(t *testing.T) {
processor := mocks.NewProcessorI[int, int](t)
cache := New[int, int](Capacity, .4, TTL, processor)
elements := Capacity
for i := 0; i < elements; i++ {
processor.EXPECT().ToMapKey(i).Return(fmt.Sprint(i), nil).Times(2)
err := cache.StoreOrUpdate(i, i)
assert.Nil(t, err)
err = cache.StoreOrUpdate(i, i)
assert.Nil(t, err)
}
for i := 0; i < elements; i++ {
processor.EXPECT().ToMapKey(i).Return(fmt.Sprint(i), nil).Times(1)
val, err := cache.RetrieveFromCacheOrCompute(i)
assert.Equal(t, val, i)
assert.Nil(t, err)
}
}
func TestCacheDriver_StoreValueConcurrentInsert(t *testing.T) {
processor := mocks.NewProcessorI[int, int](t)
cache := New[int, int](Capacity, .4, TTL, processor)
elements := Capacity
goroutines := 5
wg := sync.WaitGroup{}
wg.Add(goroutines * elements)
for i := 0; i < elements; i++ {
processor.EXPECT().ToMapKey(i).Return(fmt.Sprint(i), nil).Times(goroutines)
for j := 0; j < goroutines; j++ {
go func(i int, t *testing.T, wg *sync.WaitGroup) {
err := cache.StoreOrUpdate(i, i)
assert.Nil(t, err)
wg.Done()
}(i, t, &wg)
}
}
wg.Wait()
for i := 0; i < elements; i++ {
processor.EXPECT().ToMapKey(i).Return(fmt.Sprint(i), nil).Times(1)
val, err := cache.RetrieveFromCacheOrCompute(i)
assert.Equal(t, val, i)
assert.Nil(t, err)
}
}
// test retrieve value
func TestCacheDriver_RetrieveValue(t *testing.T) {
processor := mocks.NewProcessorI[int, int](t)
cache := New[int, int](Capacity, .4, TTL, processor)
elements := Capacity
for i := 0; i < elements; i++ {
b, requestError := insertEntry(cache, processor, i)
assert.Nil(t, requestError)
assert.NotNil(t, b)
}
for i := 0; i < elements; i++ {
processor.EXPECT().ToMapKey(i).Return(fmt.Sprint(i), nil).Times(1)
val, err := cache.RetrieveValue(i)
assert.Equal(t, val, i)
assert.Nil(t, err)
}
}
//benchmark to test the performance of the cache
//when the processor should perform an addition task
type BenchProcessor struct{}
type Adder struct {
num1, num2 int
}
func (p *BenchProcessor) ToMapKey(adder Adder) (string, error) {
return fmt.Sprintf("%d+%d", adder.num1, adder.num2), nil
}
func (p *BenchProcessor) CacheMissSolver(adder Adder) (int, *models.RequestError) {
return adder.num1 + adder.num2, nil
}
var seed int64 = 39823823434
func BenchmarkInsertStatic(b *testing.B) {
benchInsert(b, seed)
}
func BenchmarkInsertDynamic(b *testing.B) {
benchInsert(b, time.Now().Unix())
}
func benchInsert(b *testing.B, seed int64) {
cache := New[Adder, int](Capacity, 0.5, TTL, &BenchProcessor{})
rand.Seed(seed)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 100; j++ {
num1 := rand.Int()
num2 := rand.Int()
adder := Adder{num1, num2}
_, _ = cache.RetrieveFromCacheOrCompute(adder)
}
}
}
func createRandomArray(seed int64, size int) []Adder {
rand.Seed(seed)
arr := make([]Adder, size)
for i := 0; i < size; i++ {
num1 := rand.Int()
num2 := rand.Int()
arr[i] = Adder{num1, num2}
}
return arr
}
func benchInsertAvalanche(b *testing.B, seed int64) {
var size int = 1e3
arr := createRandomArray(seed, size)
cache := New[Adder, int](Capacity, 0.5, TTL, &BenchProcessor{})
rand.Seed(seed)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < size; j++ {
for k := 0; k < 1e3; k++ {
_, _ = cache.RetrieveFromCacheOrCompute(arr[j])
}
}
}
}
func BenchmarkAvalancheStatic(b *testing.B) {
benchInsertAvalanche(b, seed)
}
func BenchmarkAvalancheDynamic(b *testing.B) {
benchInsertAvalanche(b, time.Now().Unix())
}