-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
483 lines (392 loc) · 10.4 KB
/
cache_test.go
File metadata and controls
483 lines (392 loc) · 10.4 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
package atomiccache
import (
"math/rand"
"reflect"
"strconv"
"testing"
"time"
big "github.com/allegro/bigcache"
fre "github.com/coocood/freecache"
has "github.com/hashicorp/golang-lru"
)
func TestCacheFuncGetShardsSectionBySize(t *testing.T) {
for _, c := range []struct {
in int
want int
}{
{256, 1}, {512, 1}, {2048, 2}, {8127, 3},
} {
cache := New()
_, shardSectionID := cache.getShardsSectionBySize(c.in)
if !reflect.DeepEqual(shardSectionID, c.want) {
t.Errorf("%v != %v", c.in, c.want)
}
}
}
func TestCacheSimple(t *testing.T) {
for i, c := range []struct {
in []byte
want []byte
}{
{[]byte{0}, []byte{0}},
{[]byte{0, 1, 2, 3, 4, 5}, []byte{0, 1, 2, 3, 4, 5}},
} {
cache := New()
if err := cache.Set(string(byte(i)), c.in, 0); err != nil {
t.Errorf("Set error: %s", err.Error())
}
value, err := cache.Get(string(byte(i)))
if err != nil {
t.Errorf("Get error: %s", err.Error())
}
if !reflect.DeepEqual(value, c.want) {
t.Errorf("%v != %v", value, c.want)
}
}
}
func TestCacheDiffSizeData(t *testing.T) {
for _, c := range []struct {
in int
}{
{256}, {512}, {513}, {2047}, {2048}, {2049}, {4096}, {8127}, {8128},
} {
var bigString string
for x := 0; x < c.in; x++ {
bigString += "x"
}
cache := New()
if err := cache.Set("X", []byte(bigString), 0); err != nil {
t.Errorf("Set error: %s", err.Error())
}
value, err := cache.Get("X")
if err != nil {
t.Errorf("Get error: %s", err.Error())
}
if !reflect.DeepEqual(string(value), bigString) {
t.Errorf("%v != %v", string(value), bigString)
}
}
}
func TestCacheIntermediate(t *testing.T) {
for _, c := range []struct {
in []byte
want []byte
}{
{[]byte("test value"), []byte("test value")},
} {
cache := New()
for i := 0; i < 1000; i++ {
if err := cache.Set(strconv.Itoa(i), c.in, 0); err != nil {
t.Errorf("Set error: %s", err.Error())
}
}
value, err := cache.Get("0")
if err != nil {
t.Errorf("Get error: %s", err.Error())
}
if !reflect.DeepEqual(value, c.want) {
t.Errorf("%v != %v", value, c.want)
}
}
}
func TestCacheDataError(t *testing.T) {
for i, c := range []struct {
recordSizeSmall int
recordSizeMedium int
recordSizeLarge int
in []byte
want []byte
}{
{1, 2, 3, []byte{0, 1, 2, 3, 4, 5}, []byte{0}},
} {
cache := New(OptionRecordSizeSmall(c.recordSizeSmall), OptionRecordSizeMedium(c.recordSizeMedium), OptionRecordSizeLarge(c.recordSizeLarge))
if err := cache.Set(strconv.Itoa(i), c.in, 0); err == nil {
t.Errorf("Expecting error 'errDataLimit'")
}
}
}
func TestCacheFreeAfterExpiration(t *testing.T) {
cache := New(OptionGcStarter(1))
cache.Set("key", []byte("data"), 500*time.Millisecond)
time.Sleep(100 * time.Millisecond)
if _, err := cache.Get("key"); err != nil {
t.Errorf("Cache is empty, but expecting some data")
}
time.Sleep(500 * time.Millisecond)
cache.Set("key2", []byte("data"), 500*time.Millisecond)
time.Sleep(100 * time.Millisecond)
if _, err := cache.Get("key"); err == nil {
t.Errorf("Cache is not empty, but expecting nothing")
}
}
func TestCacheKeepTTL(t *testing.T) {
cache := New()
key := "keep-ttl-key"
data1 := []byte("first")
data2 := []byte("second")
expire := 2 * time.Second
// Set initial value with expiration
if err := cache.Set(key, data1, expire); err != nil {
t.Fatalf("Set error: %s", err)
}
val, ok := cache.lookup[key]
if !ok {
t.Fatalf("Key not found after Set")
}
origExp := val.Expiration
// Update value with KeepTTL
if err := cache.Set(key, data2, KeepTTL); err != nil {
t.Fatalf("Set error (KeepTTL): %s", err)
}
val2, ok := cache.lookup[key]
if !ok {
t.Fatalf("Key not found after Set with KeepTTL")
}
if !val2.Expiration.Equal(origExp) {
t.Errorf("Expiration changed: got %v, want %v", val2.Expiration, origExp)
}
// Value should be updated
got, err := cache.Get(key)
if err != nil {
t.Fatalf("Get error: %s", err)
}
if !reflect.DeepEqual(got, data2) {
t.Errorf("Value not updated: got %v, want %v", got, data2)
}
}
func TestCacheExists(t *testing.T) {
cache := New()
key := "exists-key"
data := []byte("exists-data")
// Should not exist before set
if cache.Exists(key) {
t.Errorf("Exists returned true for unset key")
}
// Set and check exists
if err := cache.Set(key, data, 10*time.Second); err != nil {
t.Fatalf("Set error: %s", err)
}
if !cache.Exists(key) {
t.Errorf("Exists returned false for set key")
}
// Delete and check exists
if err := cache.Delete(key); err != nil {
t.Fatalf("Delete error: %s", err)
}
if cache.Exists(key) {
t.Errorf("Exists returned true after Delete")
}
// Never-set key
if cache.Exists("never-existed") {
t.Errorf("Exists returned true for never-set key")
}
}
func TestCacheDelete(t *testing.T) {
cache := New()
key := "del-key"
data := []byte("to-delete")
// Set and then delete
if err := cache.Set(key, data, 0); err != nil {
t.Fatalf("Set error: %s", err)
}
if err := cache.Delete(key); err != nil {
t.Errorf("Delete error: %s", err)
}
// Should not be able to get deleted key
if _, err := cache.Get(key); err == nil {
t.Errorf("Expected error on Get after Delete, got nil")
}
// Deleting again should return ErrNotFound
if err := cache.Delete(key); err != ErrNotFound {
t.Errorf("Expected ErrNotFound on double Delete, got %v", err)
}
// Deleting a never-set key should return ErrNotFound
if err := cache.Delete("never-existed"); err != ErrNotFound {
t.Errorf("Expected ErrNotFound for never-set key, got %v", err)
}
}
func benchmarkCacheNew(recordCount int, b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
New(OptionMaxRecords(recordCount))
}
}
func benchmarkCacheSet(recordCount, dataSize int, b *testing.B) {
var data []byte
keys := generateKeys(32000, 64)
for i := 0; i < dataSize; i++ {
data = append(data, 1)
}
cache := New(OptionMaxRecords(recordCount))
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Set(keys[n%len(keys)], data, 0)
}
}
func benchmarkCacheGet(recordCount, dataSize int, b *testing.B) {
var data []byte
for i := 0; i < dataSize; i++ {
data = append(data, 1)
}
cache := New(OptionMaxRecords(recordCount))
cache.Set("0", data, 0)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Get("0")
}
}
func BenchmarkCacheNewSmall(b *testing.B) {
benchmarkCacheNew(512, b)
}
func BenchmarkCacheNewMedium(b *testing.B) {
benchmarkCacheNew(2048, b)
}
func BenchmarkCacheNewLarge(b *testing.B) {
benchmarkCacheNew(16384, b)
}
func BenchmarkCacheSetSmall(b *testing.B) {
benchmarkCacheSet(512, 1024, b)
}
func BenchmarkCacheSetMedium(b *testing.B) {
benchmarkCacheSet(2048, 1024, b)
}
func BenchmarkCacheSetLarge(b *testing.B) {
benchmarkCacheSet(16384, 1024, b)
}
func BenchmarkCacheGetSmall(b *testing.B) {
benchmarkCacheGet(512, 1024, b)
}
func BenchmarkCacheGetMedium(b *testing.B) {
benchmarkCacheGet(2048, 1024, b)
}
func BenchmarkCacheGetLarge(b *testing.B) {
benchmarkCacheGet(16384, 1024, b)
}
func BenchmarkAtomicCacheSet(b *testing.B) {
cache := newAtoCache()
keys := generateKeys(32000, 64)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Set(keys[n%len(keys)], []byte("Testing data input"), time.Duration(10*time.Minute))
}
}
func BenchmarkBigCacheSet(b *testing.B) {
cache := newBigCache()
keys := generateKeys(32000, 64)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Set(keys[n%len(keys)], []byte("Testing data input"))
}
}
func BenchmarkFreeCacheSet(b *testing.B) {
cache := newFreCache()
keys := generateKeys(32000, 64)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Set([]byte(keys[n%len(keys)]), []byte("Testing data input"), 600)
}
}
func BenchmarkHashicorpCacheSet(b *testing.B) {
cache := newHasCache()
keys := generateKeys(32000, 64)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Add(keys[n%len(keys)], []byte("Testing data input"))
}
}
func BenchmarkAtomicCacheGet(b *testing.B) {
cache := newAtoCache()
keys := generateKeys(32000, 64)
for i := 0; i < 32000; i++ {
cache.Set(keys[i%len(keys)], []byte("Testing data input"), time.Duration(10*time.Minute))
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Get(keys[n%len(keys)])
}
}
func BenchmarkBigCacheGet(b *testing.B) {
cache := newBigCache()
keys := generateKeys(32000, 64)
for i := 0; i < 32000; i++ {
cache.Set(keys[i%len(keys)], []byte("Testing data input"))
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Get(keys[n%len(keys)])
}
}
func BenchmarkFreeCacheGet(b *testing.B) {
cache := newFreCache()
keys := generateKeys(32000, 64)
for i := 0; i < 32000; i++ {
cache.Set([]byte(keys[i%len(keys)]), []byte("Testing data input"), 600)
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Get([]byte(keys[n%len(keys)]))
}
}
func BenchmarkHashicorpCacheGet(b *testing.B) {
cache := newHasCache()
keys := generateKeys(32000, 64)
for i := 0; i < 32000; i++ {
cache.Add(keys[i%len(keys)], []byte("Testing data input"))
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
cache.Get(keys[n%len(keys)])
}
}
func newAtoCache() *AtomicCache {
var options []Option
options = append(options, OptionRecordSizeSmall(256))
options = append(options, OptionRecordSizeMedium(1024))
options = append(options, OptionRecordSizeLarge(8128))
options = append(options, OptionMaxRecords(4096))
options = append(options, OptionMaxShardsSmall(512))
options = append(options, OptionMaxShardsMedium(256))
options = append(options, OptionMaxShardsLarge(64))
cache := New(options...)
return cache
}
func newBigCache() *big.BigCache {
cache, _ := big.NewBigCache(big.Config{
Shards: 128,
LifeWindow: 10 * time.Minute,
MaxEntriesInWindow: 1000 * 10 * 60,
MaxEntrySize: 2048,
HardMaxCacheSize: 0,
})
return cache
}
func newFreCache() *fre.Cache {
cache := fre.NewCache(100 * 1024 * 1024)
return cache
}
func newHasCache() *has.Cache {
cache, _ := has.New(262144)
return cache
}
func generateKeys(cnt, length int) (keys []string) {
var source = rand.NewSource(time.Now().UnixNano())
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for count := 0; count < cnt; count++ {
b := make([]byte, length)
for i := range b {
b[i] = charset[source.Int63()%int64(len(charset))]
}
keys = append(keys, string(b))
}
return
}