-
Notifications
You must be signed in to change notification settings - Fork 11
/
lfu_test.go
357 lines (300 loc) · 7.01 KB
/
lfu_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
package mcache
import (
"math/rand"
"runtime"
"strconv"
"sync"
"testing"
)
func BenchmarkLFU_Rand(b *testing.B) {
l, err := NewLFU(8192)
if err != nil {
b.Fatalf("err: %v", err)
}
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.Add(trace[i], trace[i], 0)
} 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 BenchmarkLFU_Freq(b *testing.B) {
l, err := NewLFU(8192)
if err != nil {
b.Fatalf("err: %v", err)
}
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.Add(trace[i], trace[i], 0)
}
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 TestLFU(t *testing.T) {
evictCounter := 0
onEvicted := func(k interface{}, v interface{}, expirationTime int64) {
if k != v {
t.Fatalf("Evict values not equal (%v!=%v)", k, v)
}
evictCounter++
}
l, err := NewLruWithEvict(128, onEvicted)
if err != nil {
t.Fatalf("err: %v", err)
}
for i := 0; i < 256; i++ {
l.Add(i, i, 0)
}
if l.Len() != 128 {
t.Fatalf("bad len: %v", l.Len())
}
if evictCounter != 128 {
t.Fatalf("bad evict count: %v", evictCounter)
}
for i, k := range l.Keys() {
if v, _, ok := l.Get(k); !ok || v != k || v != i+128 {
t.Fatalf("bad key: %v", k)
}
}
for i := 0; i < 128; i++ {
_, _, ok := l.Get(i)
if ok {
t.Fatalf("should be evicted")
}
}
for i := 128; i < 256; i++ {
_, _, ok := l.Get(i)
if !ok {
t.Fatalf("should not be evicted")
}
}
for i := 128; i < 192; i++ {
l.Remove(i)
_, _, ok := l.Get(i)
if ok {
t.Fatalf("should be deleted")
}
}
l.Get(192) // expect 192 to be last key in l.Keys()
for i, k := range l.Keys() {
if (i < 63 && k != i+193) || (i == 63 && k != 192) {
t.Fatalf("out of order key: %v", k)
}
}
l.Purge()
if l.Len() != 0 {
t.Fatalf("bad len: %v", l.Len())
}
if _, _, ok := l.Get(200); ok {
t.Fatalf("should contain nothing")
}
}
// test that Add returns true/false if an eviction occurred
func TestLFUAdd(t *testing.T) {
evictCounter := 0
onEvicted := func(k interface{}, v interface{}, expirationTime int64) {
evictCounter++
}
l, err := NewLfuWithEvict(1, onEvicted)
if err != nil {
t.Fatalf("err: %v", err)
}
if l.Add(1, 1, 0) == false || evictCounter != 0 {
t.Errorf("should not have an eviction")
}
if l.Add(2, 2, 0) == false || evictCounter != 1 {
t.Errorf("should have an eviction")
}
}
// test that Contains doesn't update recent-ness
func TestLFUContains(t *testing.T) {
l, err := NewLRU(2)
if err != nil {
t.Fatalf("err: %v", err)
}
l.Add(1, 1, 0)
l.Add(2, 2, 0)
if !l.Contains(1) {
t.Errorf("1 should be contained")
}
l.Add(3, 3, 0)
if l.Contains(1) {
t.Errorf("Contains should not have updated recent-ness of 1")
}
}
// test that ContainsOrAdd doesn't update recent-ness
func TestLFUContainsOrAdd(t *testing.T) {
l, err := NewLRU(2)
if err != nil {
t.Fatalf("err: %v", err)
}
l.Add(1, 1, 0)
l.Add(2, 2, 0)
contains, evict := l.ContainsOrAdd(1, 1, 0)
if !contains {
t.Errorf("1 should be contained")
}
if evict {
t.Errorf("nothing should be evicted here")
}
l.Add(3, 3, 0)
contains, evict = l.ContainsOrAdd(1, 1, 0)
if contains {
t.Errorf("1 should not have been contained")
}
if !evict {
t.Errorf("an eviction should have occurred")
}
if !l.Contains(1) {
t.Errorf("now 1 should be contained")
}
}
// test that PeekOrAdd doesn't update recent-ness
func TestLFUPeekOrAdd(t *testing.T) {
l, err := NewLRU(2)
if err != nil {
t.Fatalf("err: %v", err)
}
l.Add(1, 1, 0)
l.Add(2, 2, 0)
previous, contains, evict := l.PeekOrAdd(1, 1, 0)
if !contains {
t.Errorf("1 should be contained")
}
if evict {
t.Errorf("nothing should be evicted here")
}
if previous != 1 {
t.Errorf("previous is not equal to 1")
}
l.Add(3, 3, 0)
contains, evict = l.ContainsOrAdd(1, 1, 0)
if contains {
t.Errorf("1 should not have been contained")
}
if !evict {
t.Errorf("an eviction should have occurred")
}
if !l.Contains(1) {
t.Errorf("now 1 should be contained")
}
}
// test that Peek doesn't update recent-ness
func TestLFUPeek(t *testing.T) {
l, err := NewLRU(2)
if err != nil {
t.Fatalf("err: %v", err)
}
l.Add(1, 1, 0)
l.Add(2, 2, 0)
if v, _, ok := l.Peek(1); !ok || v != 1 {
t.Errorf("1 should be set to 1: %v, %v", v, ok)
}
l.Add(3, 3, 0)
if l.Contains(1) {
t.Errorf("should not have updated recent-ness of 1")
}
}
// test that Resize can upsize and downsize
func TestLFUResize(t *testing.T) {
onEvictCounter := 0
onEvicted := func(k interface{}, v interface{}, expirationTime int64) {
onEvictCounter++
}
l, err := NewLfuWithEvict(2, onEvicted)
if err != nil {
t.Fatalf("err: %v", err)
}
// Downsize
l.Add(1, 1, 0)
l.Add(2, 2, 0)
evicted := l.Resize(1)
if evicted != 1 {
t.Errorf("1 element should have been evicted: %v", evicted)
}
if onEvictCounter != 1 {
t.Errorf("onEvicted should have been called 1 time: %v", onEvictCounter)
}
l.Add(3, 3, 0)
if l.Contains(1) {
t.Errorf("Element 1 should have been evicted")
}
// Upsize
evicted = l.Resize(2)
if evicted != 0 {
t.Errorf("0 elements should have been evicted: %v", evicted)
}
l.Add(4, 4, 0)
if !l.Contains(3) || !l.Contains(4) {
t.Errorf("lruCache should have contained 2 elements")
}
}
// LFU 性能压测
func TestLFU_Performance(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
//cpu 性能分析 go tool pprof --pdf cpu ./cpu.pprof > cpu.pdf
//开始性能分析, 返回一个停止接口
//stopper1 := profile.Start(profile.CPUProfile, profile.ProfilePath("."))
////在main()结束时停止性能分析
//defer stopper1.Stop()
//// 查看导致阻塞同步的堆栈跟踪
//stopper2 := profile.Start(profile.BlockProfile, profile.ProfilePath("."))
//// 在main()结束时停止性能分析
//defer stopper2.Stop()
//
//// 查看当前所有运行的 goroutines 堆栈跟踪
//stopper3 := profile.Start(profile.GoroutineProfile, profile.ProfilePath("."))
//// 在main()结束时停止性能分析
//defer stopper3.Stop()
//
//// 查看当前所有运行的 goroutines 堆栈跟踪
//stopper4 := profile.Start(profile.MemProfile, profile.ProfilePath("."))
//// 在main()结束时停止性能分析
//defer stopper4.Stop()
count := 10000000
l, _ := NewLFU(20000)
wg := &sync.WaitGroup{}
for k := 0; k < count; k++ {
wg.Add(1)
go lfuPerformanceOne(l, wg, k)
}
wg.Wait()
}
func lfuPerformanceOne(h *LfuCache, c *sync.WaitGroup, k int) {
for i := 0; i < 5; i++ {
var strKey string
strKey = strconv.Itoa(k) + "_" + strconv.Itoa(i)
h.Add(strKey, &testJsonStr, 0)
}
// 通知main已经结束循环(我搞定了!)
c.Done()
}