-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfuda_test.go
363 lines (306 loc) · 6.93 KB
/
lfuda_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
package lfuda
import (
"math"
"math/rand"
"testing"
)
func BenchmarkLFUDA(b *testing.B) {
l := New(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.ResetTimer()
for i := 0; i < b.N; i++ {
l.Set(trace[i], trace[i])
}
var hit, miss int
for i := 0; i < b.N; i++ {
_, ok := l.Get(trace[i])
if ok {
hit++
} else {
miss++
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss))
}
func BenchmarkLFUDA_Rand(b *testing.B) {
l := New(8192)
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
trace[i] = rand.Int63() % 32768
}
b.ResetTimer()
var hit, miss int
for i := 0; i < 2*b.N; i++ {
if i%2 == 0 {
l.Set(trace[i], trace[i])
}
if i%7 == 0 {
for j := 0; j < 20; j++ {
_, ok := l.Get(trace[i])
if ok {
hit++
} else {
miss++
}
}
} else {
_, ok := l.Get(trace[i])
if ok {
hit++
} else {
miss++
}
}
}
b.Logf("hit: %d miss: %d ratio: %f", hit, miss, float64(hit)/float64(miss))
}
func TestLFUDA(t *testing.T) {
evictCounter := 0
onEvicted := func(k interface{}, v interface{}) {
if k != v {
t.Errorf("Evict values not equal (%v!=%v)", k, v)
}
evictCounter++
}
l := NewWithEvict(666, onEvicted)
numSet := 0
for i := 100; i < 1000; i++ {
if l.Set(i, i) {
numSet++
}
}
if l.Len() != 222 || l.Len() != len(l.Keys()) {
t.Errorf("bad len: %v", l.Len())
}
if evictCounter != 900-222 {
t.Errorf("bad evict count: %v", evictCounter)
}
for _, k := range l.Keys() {
if v, ok := l.Get(k); !ok || v != k {
t.Fatalf("bad key: %v, %v, %t", k, v, ok)
}
}
// these should all be misses since their hits will be too low
// relative to newer keys set when the cache is more aged
for i := 100; i < 765; i++ {
_, ok := l.Get(i)
if ok {
t.Fatalf("should not be in cache")
}
}
if ok := l.Set(256, 256); !ok {
t.Errorf("Wasn't able to set key/value in cache (but should have been)")
}
if val, _ := l.Get(256); val != 256 {
t.Errorf("Wrong value returned for key")
}
// expect 256 to be the top hit in l.Keys()
if l.Keys()[0] != 256 {
t.Errorf("out of order key (last set of keys should have hits=5 and should be first): %d", l.Keys()[0])
}
l.Purge()
if l.Len() != 0 {
t.Errorf("bad len: %v", l.Len())
}
if _, ok := l.Get(200); ok {
t.Errorf("should contain nothing")
}
}
func TestGDSF(t *testing.T) {
l := NewGDSF(666)
numSet := 0
for i := 10; i < 20; i++ {
if l.Set(i, math.Pow(2, float64(i))) {
numSet++
}
}
for i := 100; i < 1000; i++ {
if l.Set(i, i) {
numSet++
}
}
if l.Len() != 222 || l.Len() != len(l.Keys()) {
t.Errorf("bad len: %v", l.Len())
}
// all large values should be evicted
for _, k := range l.Keys() {
if v, ok := l.Get(k); !ok || v != k {
t.Fatalf("bad key: %v, %v, %t", k, v, ok)
}
}
// these should all be misses since their hits will be too low
// relative to newer keys set when the cache is more aged
for i := 100; i < 765; i++ {
_, ok := l.Get(i)
if ok {
t.Fatalf("should not be in cache")
}
}
if ok := l.Set(256, 256); !ok {
t.Errorf("Wasn't able to set key/value in cache (but should have been)")
}
if val, _ := l.Get(256); val != 256 {
t.Errorf("Wrong value returned for key")
}
// expect 256 to be the top hit in l.Keys()
if l.Keys()[0] != 256 {
t.Errorf("out of order key (last set of keys should have hits=5 and should be first): %d", l.Keys()[0])
}
}
// test that Set returns true/false
func TestLFUDASet(t *testing.T) {
evictCounter := 0
onEvicted := func(k interface{}, v interface{}) {
evictCounter++
}
l := NewWithEvict(1, onEvicted)
if l.Set(1, 1) == true || evictCounter != 0 {
t.Errorf("should not have evicted")
}
if l.Set(2, 2) == false || evictCounter != 1 {
t.Errorf("should have evicted")
}
}
// test that Contains doesn't update recent-ness
func TestLFUDAContains(t *testing.T) {
l := NewWithEvict(2, nil)
l.Set(1, 1)
l.Set(2, 2)
// bump 1 hits
for i := 0; i < 10; i++ {
l.Get(1)
}
if l.Keys()[0] != 1 {
t.Errorf("key 1 should be the most frequently used")
}
// should not bump 2 hits
for i := 0; i < 20; i++ {
l.Contains(2)
}
if l.Keys()[0] != 1 {
t.Errorf("key 1 should be the most frequently used")
}
}
// test that ContainsOrSet doesn't update recent-ness
func TestLFUDAContainsOrSet(t *testing.T) {
l := New(2)
l.Set(1, 1)
l.Set(2, 2)
contains, eviction := l.ContainsOrSet(1, 1)
if !contains {
t.Errorf("1 should be contained")
}
if eviction {
t.Errorf("nothing should have been set")
}
contains, eviction = l.ContainsOrSet(3, 3)
if contains {
t.Errorf("3 should not have been contained")
}
if !eviction {
t.Errorf("3 should have been set and an eviction should have occurred")
}
}
// test that PeekOrSet doesn't update recent-ness
func TestLFUDAPeekOrSet(t *testing.T) {
l := New(2)
l.Set(1, 1)
l.Set(2, 2)
previous, contains, set := l.PeekOrSet(1, 1)
if !contains {
t.Errorf("1 should be contained")
}
if set {
t.Errorf("nothing should have been set here")
}
if previous != 1 {
t.Errorf("previous is not equal to 1")
}
_, contains, set = l.PeekOrSet(3, 3)
if contains {
t.Errorf("3 should not have been contained")
}
if !set {
t.Errorf("3 should have been set here")
}
l.Get(3)
_, contains, set = l.PeekOrSet(3, 3)
if !contains {
t.Errorf("3 should have been contained")
}
if set {
t.Errorf("3 should not have been set")
}
previous, contains, set = l.PeekOrSet(3, 3)
if previous != 3 {
t.Errorf("3 should be returned")
}
if !contains {
t.Errorf("3 should have been contained")
}
if set {
t.Errorf("nothing should be set here")
}
}
// test that Peek doesn't update recent-ness
func TestLFUDAPeek(t *testing.T) {
l := New(2)
l.Set(1, 1)
l.Set(2, 2)
if v, ok := l.Peek(1); !ok || v != 1 {
t.Errorf("1 should be set to 1: %v, %v", v, ok)
}
l.Get(2)
l.Set(3, 3)
if l.Contains(1) {
t.Errorf("should not have updated hits for 1")
}
}
func TestLFUDARemove(t *testing.T) {
l := New(2)
l.Set(1, 1)
l.Set(2, 2)
if v, ok := l.Get(1); !ok || v != 1 {
t.Errorf("1 should be set to 1: %v, %v", v, ok)
}
l.Remove(1)
if _, ok := l.Get(1); ok {
t.Errorf("1 should not be in the cache")
}
if l.Len() != 1 {
t.Errorf("Cache should be length 1 (but it wasn't)")
}
}
func TestLFUDAAge(t *testing.T) {
l := New(1)
l.Set(1, 1)
// bump hits on key 1 to 2
l.Get(1)
if evicted := l.Set(2, 2); !evicted {
t.Errorf("Set op should have evicted (but it didn't)")
}
// key 1 was evicted so cache age should be its hits value (2)
if l.Age() != 2 {
t.Errorf("cache age should have been set to 1 (but it was't)")
}
}
func TestLFUDASize(t *testing.T) {
l := New(11)
for i := 10; i < 30; i++ {
l.Set(i, i)
}
if l.Size() != 10 {
t.Errorf("Cache can only store up to 11 bytes so Size should be divisible by 2")
}
l.Purge()
if l.Size() != 0 {
t.Errorf("Cache size should be reset to 0 (but it wasn't)")
}
}