-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlru_cache_test.go
404 lines (334 loc) · 9.59 KB
/
lru_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
package lru
import (
"context"
"fmt"
"math/rand"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"unsafe"
)
func TestLRUCacheCompactness(t *testing.T) {
if runtime.GOARCH != "amd64" {
return
}
compact := isamd64
defer func() {
isamd64 = compact
}()
for _, b := range []bool{true, false} {
isamd64 = b
cache := NewLRUCache[string, []byte](32 * 1024)
if length := cache.Len(); length != 0 {
t.Fatalf("bad cache length: %v", length)
}
}
}
func TestLRUCacheDefaultkey(t *testing.T) {
cache := NewLRUCache[string, int](1)
var k string
var i int = 10
if prev, replaced := cache.Set(k, i); replaced {
t.Fatalf("value %v should not be replaced", prev)
}
if v, ok := cache.Get(k); !ok || v != i {
t.Fatalf("bad returned value: %v != %v", v, i)
}
}
func TestLRUCacheGetSet(t *testing.T) {
cache := NewLRUCache[int, int](128)
if v, ok := cache.Get(5); ok {
t.Fatalf("bad returned value: %v", v)
}
if _, replaced := cache.Set(5, 10); replaced {
t.Fatal("should not have replaced")
}
if v, ok := cache.Get(5); !ok || v != 10 {
t.Fatalf("bad returned value: %v != %v", v, 10)
}
if v, replaced := cache.Set(5, 9); v != 10 || !replaced {
t.Fatal("old value should be evicted")
}
if v, replaced := cache.Set(5, 9); v != 9 || !replaced {
t.Fatal("old value should be evicted")
}
if v, ok := cache.Get(5); !ok || v != 9 {
t.Fatalf("bad returned value: %v != %v", v, 10)
}
}
func TestLRUCacheSetIfAbsent(t *testing.T) {
cache := NewLRUCache[int, int](128)
cache.Set(5, 5)
if _, replaced := cache.SetIfAbsent(5, 10); replaced {
t.Fatal("should not have replaced")
}
if v, ok := cache.Get(5); !ok || v != 5 {
t.Fatalf("bad returned value: %v = %v", v, 5)
}
cache.Delete(5)
if _, replaced := cache.SetIfAbsent(5, 10); replaced {
t.Fatal("should not have replaced")
}
if v, ok := cache.Get(5); !ok || v != 10 {
t.Fatalf("bad returned value: %v = %v", v, 10)
}
cache.Delete(5)
if _, replaced := cache.SetIfAbsent(5, 10); replaced {
t.Fatal("should not have replaced")
}
if v, ok := cache.Get(5); !ok || v != 10 {
t.Fatalf("bad returned value: %v = %v", v, 10)
}
}
func TestLRUCacheEviction(t *testing.T) {
cache := NewLRUCache[int, *int](256, WithShards[int, *int](1024))
if cache.mask+1 != uint32(cap(cache.shards)) {
t.Fatalf("bad shard mask: %v", cache.mask)
}
cache = NewLRUCache[int, *int](256, WithShards[int, *int](1))
evictedCounter := 0
for i := 0; i < 512; i++ {
if v, _ := cache.Set(i, &i); v != nil {
evictedCounter++
}
}
if cache.Len() != 256 {
t.Fatalf("bad len: %v", cache.Len())
}
if evictedCounter != 256 {
t.Fatalf("bad evicted count: %v", evictedCounter)
}
for i := 0; i < 256; i++ {
if v, ok := cache.Get(i); ok || v != nil {
t.Fatalf("key %v value %v should be evicted", i, *v)
}
}
for i := 256; i < 512; i++ {
if v, ok := cache.Get(i); !ok {
t.Fatalf("key %v value %v should not be evicted", i, *v)
}
}
for i := 256; i < 384; i++ {
cache.Delete(i)
if v, ok := cache.Get(i); ok {
t.Fatalf("old key %v value %v should be deleted", i, *v)
}
}
for i := 384; i < 512; i++ {
if v, ok := cache.Get(i); !ok || v == nil {
t.Fatalf("old key %v value %v should not be deleted", i, *v)
}
}
if got, want := cache.Len(), 128; got != want {
t.Fatalf("curent cache length %v should be %v", got, want)
}
cache.Set(400, &evictedCounter)
if got, want := len(cache.AppendKeys(nil)), 128; got != want {
t.Fatalf("curent cache keys length %v should be %v", got, want)
}
}
func TestLRUCachePeek(t *testing.T) {
cache := NewLRUCache[int, int](64)
cache.Set(10, 10)
cache.Set(20, 20)
if v, ok := cache.Peek(10); !ok || v != 10 {
t.Errorf("10 should be set to 10: %v", v)
}
if v, ok := cache.Peek(20); !ok || v != 20 {
t.Errorf("20 should be set to 20: %v,", v)
}
if v, ok := cache.Peek(30); ok || v != 0 {
t.Errorf("30 should be set to 0: %v,", v)
}
for k := 3; k < 1024; k++ {
cache.Set(k, k)
}
if v, ok := cache.Peek(10); ok || v == 10 {
t.Errorf("%v should not have updated recent-ness of 10", v)
}
if v, ok := cache.Peek(30); ok || v != 0 {
t.Errorf("%v should have updated recent-ness of 30", v)
}
}
func TestLRUCacheHasher(t *testing.T) {
cache := NewLRUCache[string, int](1024,
WithHasher[string, int](func(key unsafe.Pointer, seed uintptr) (x uintptr) {
x = 5381
for _, c := range []byte(*(*string)(key)) {
x = x*33 + uintptr(c)
}
return
}),
WithShards[string, int](1),
)
if v, ok := cache.Get("abcde"); ok {
t.Fatalf("bad returned value: %v", v)
}
if _, replaced := cache.Set("abcde", 10); replaced {
t.Fatal("should not have replaced")
}
if v, ok := cache.Get("abcde"); !ok || v != 10 {
t.Fatalf("bad returned value: %v != %v", v, 10)
}
}
func TestLRUCacheSliding(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if !strings.Contains(fmt.Sprint(r), "not_supported") {
t.Errorf("should be not_supported")
}
}
}()
_ = NewLRUCache[string, int](1024, WithSliding[string, int](true))
t.Errorf("should be panic above")
}
func TestLRUCacheLoader(t *testing.T) {
cache := NewLRUCache[string, int](1024)
if v, err, ok := cache.GetOrLoad(context.Background(), "a", nil); ok || err == nil || v != 0 {
t.Errorf("cache.GetOrLoad(\"a\", nil) again should be return error: %v, %v, %v", v, err, ok)
}
cache = NewLRUCache[string, int](1024, WithLoader[string, int](func(ctx context.Context, key string) (int, error) {
if key == "" {
return 0, fmt.Errorf("invalid key: %v", key)
}
i := int(key[0] - 'a' + 1)
return i, nil
}))
if v, err, ok := cache.GetOrLoad(context.Background(), "", nil); ok || err == nil || v != 0 {
t.Errorf("cache.GetOrLoad(\"a\", nil) again should be return error: %v, %v, %v", v, err, ok)
}
if v, err, ok := cache.GetOrLoad(context.Background(), "b", nil); ok || err != nil || v != 2 {
t.Errorf("cache.GetOrLoad(\"b\", nil) again should be return 2: %v, %v, %v", v, err, ok)
}
if v, err, ok := cache.GetOrLoad(context.Background(), "a", nil); ok || err != nil || v != 1 {
t.Errorf("cache.GetOrLoad(\"a\", nil) should be return 1: %v, %v, %v", v, err, ok)
}
if v, err, ok := cache.GetOrLoad(context.Background(), "a", nil); !ok || err != nil || v != 1 {
t.Errorf("cache.GetOrLoad(\"a\", nil) again should be return 1: %v, %v, %v", v, err, ok)
}
}
func TestLRUCacheLoaderPanic(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if !strings.Contains(fmt.Sprint(r), "not_supported") {
t.Errorf("should be not_supported")
}
}
}()
_ = NewLRUCache[string, int](1024, WithLoader[string, int](func(ctx context.Context, key string) (int, time.Duration, error) {
return 1, time.Hour, nil
}))
t.Errorf("should be panic above")
}
func TestLRUCacheLoaderSingleflight(t *testing.T) {
var loads uint32
cache := NewLRUCache[string, int](1024, WithLoader[string, int](func(ctx context.Context, key string) (int, error) {
atomic.AddUint32(&loads, 1)
time.Sleep(100 * time.Millisecond)
return int(key[0] - 'a' + 1), nil
}))
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func(i int) {
defer wg.Done()
v, err, ok := cache.GetOrLoad(context.Background(), "a", nil)
if v != 1 || err != nil || !ok {
t.Errorf("a should be set to 1: %v,%v,%v", v, err, ok)
}
}(i)
}
wg.Wait()
if n := atomic.LoadUint32(&loads); n != 1 {
t.Errorf("a should be loaded only once: %v", n)
}
}
func TestLRUCacheStats(t *testing.T) {
cache := NewLRUCache[string, int](256, WithShards[string, int](1))
cache.Set("a", 1)
cache.Set("b", 2)
cache.Set("c", 3)
cache.Set("d", 3)
stats := cache.Stats()
if got, want := stats.EntriesCount, uint64(4); got != want {
t.Fatalf("cache entries should be %v: %v", want, got)
}
if got, want := stats.GetCalls, uint64(0); got != want {
t.Fatalf("cache get calls should be %v: %v", want, got)
}
if got, want := stats.SetCalls, uint64(4); got != want {
t.Fatalf("cache set calls should be %v: %v", want, got)
}
if got, want := stats.Misses, uint64(0); got != want {
t.Fatalf("cache misses should be %v: %v", want, got)
}
cache.Get("a")
cache.Get("b")
cache.Get("x")
cache.Get("y")
cache.Get("z")
cache.Set("c", 13)
stats = cache.Stats()
if got, want := stats.EntriesCount, uint64(4); got != want {
t.Fatalf("cache entries should be %v: %v", want, got)
}
if got, want := stats.GetCalls, uint64(5); got != want {
t.Fatalf("cache get calls should be %v: %v", want, got)
}
if got, want := stats.SetCalls, uint64(5); got != want {
t.Fatalf("cache set calls should be %v: %v", want, got)
}
if got, want := stats.Misses, uint64(3); got != want {
t.Fatalf("cache misses should be %v: %v", want, got)
}
}
func BenchmarkLRUCacheRand(b *testing.B) {
cache := NewLRUCache[int64, int64](8192)
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
trace[i] = rand.Int63() % 32768
}
b.ReportAllocs()
b.ResetTimer()
var hit, miss int
for i := 0; i < 2*b.N; i++ {
if i%2 == 0 {
cache.Set(trace[i], trace[i])
} else {
if _, ok := cache.Get(trace[i]); ok {
hit++
} else {
miss++
}
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
}
func BenchmarkLRUCacheFreq(b *testing.B) {
cache := NewLRUCache[int64, int64](8192)
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
if i%2 == 0 {
trace[i] = rand.Int63() % 16384
} else {
trace[i] = rand.Int63() % 32768
}
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache.Set(trace[i], trace[i])
}
var hit, miss int
for i := 0; i < b.N; i++ {
if _, ok := cache.Get(trace[i]); ok {
hit++
} else {
miss++
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(hit+miss))
}