forked from jamiealquiza/bicache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods_test.go
522 lines (419 loc) · 14.7 KB
/
methods_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
package bicache_test
import (
"log"
"strconv"
"testing"
"time"
"github.com/jamiealquiza/bicache/v2"
)
// Benchmarks
func BenchmarkGet(b *testing.B) {
b.StopTimer()
c, _ := bicache.New(&bicache.Config{
MFUSize: 10000,
MRUSize: 600000,
ShardCount: 1024,
AutoEvict: 30000,
})
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
k := strconv.Itoa(i)
keys[i] = k
c.Set(k, "my value")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
c.Get(keys[i])
}
}
func BenchmarkSet(b *testing.B) {
b.StopTimer()
c, _ := bicache.New(&bicache.Config{
MFUSize: 10000,
MRUSize: 600000,
ShardCount: 1024,
AutoEvict: 30000,
})
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
k := strconv.Itoa(i)
keys[i] = k
}
b.StartTimer()
for i := 0; i < b.N; i++ {
c.Set(keys[i], "my value")
}
}
func BenchmarkSetTTL(b *testing.B) {
b.StopTimer()
c, _ := bicache.New(&bicache.Config{
MFUSize: 10000,
MRUSize: 600000,
ShardCount: 1024,
AutoEvict: 30000,
})
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
k := strconv.Itoa(i)
keys[i] = k
}
b.StartTimer()
for i := 0; i < b.N; i++ {
c.SetTTL(keys[i], "my value", 3600)
}
}
func BenchmarkDel(b *testing.B) {
b.StopTimer()
c, _ := bicache.New(&bicache.Config{
MFUSize: 10000,
MRUSize: 600000,
ShardCount: 1024,
AutoEvict: 30000,
})
keys := make([]string, b.N)
for i := 0; i < b.N; i++ {
k := strconv.Itoa(i)
keys[i] = k
}
for i := 0; i < b.N; i++ {
c.Set(keys[i], "my value")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
c.Del(keys[i])
}
}
func BenchmarkList(b *testing.B) {
b.StopTimer()
c, _ := bicache.New(&bicache.Config{
MFUSize: 10000,
MRUSize: 600000,
ShardCount: 1024,
AutoEvict: 30000,
})
keys := make([]string, b.N/2)
for i := 0; i < b.N/2; i++ {
k := strconv.Itoa(i)
keys[i] = k
}
for i := 0; i < b.N/2; i++ {
c.Set(keys[i], "my value")
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = c.List(b.N / 2)
}
}
// Tests
func TestSetGet(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 2,
AutoEvict: 10000,
})
ok := c.Set("key", "value")
if !ok {
t.Error("Set failed")
}
if c.Get("key") != "value" {
t.Error("Get failed")
}
// Ensure that updates work.
ok = c.Set("key", "value2")
if !ok {
t.Error("Update failed")
}
if c.Get("key") != "value2" {
t.Error("Update failed")
}
}
func TestSetTTL(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 2,
AutoEvict: 1000,
})
ok := c.SetTTL("key", "value", 3)
if !ok {
t.Error("Set failed")
}
if c.Get("key") != "value" {
t.Error("Get failed")
}
log.Printf("Sleeping for 4 seconds to allow evictions")
time.Sleep(4 * time.Second)
if c.Get("key") != nil {
t.Error("Key TTL expiration failed")
}
}
func TestDel(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 2,
AutoEvict: 10000,
})
c.Set("key", "value")
c.Del("key")
if c.Get("key") != nil {
t.Error("Delete failed")
}
stats := c.Stats()
if stats.MRUSize != 0 {
t.Errorf("Expected MRU size 0, got %d", stats.MRUSize)
}
}
func TestList(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 2,
AutoEvict: 1000,
})
for i := 0; i < 40; i++ {
c.Set(strconv.Itoa(i), "value")
}
c.Get("0")
c.Get("0")
c.Get("0")
c.Get("0")
c.Get("1")
c.Get("1")
c.Get("1")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
log.Printf("Sleeping for 2 seconds to allow evictions")
time.Sleep(2 * time.Second)
list := c.List(5)
if len(list) != 5 {
t.Errorf("Exected list output len of 5, got %d", len(list))
}
// Can only reliably expect the MFU nodes
// in the list output. Check that the top3
// are what's expected.
expected := []string{"2", "0", "1"}
for i, n := range list[:3] {
if n.Key != expected[i] {
t.Errorf(`Expected key "%s" at list element %d, got "%s"`,
expected[i], i, n.Key)
}
}
}
func TestFlushMRU(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 1,
AutoEvict: 1000,
})
for i := 0; i < 30; i++ {
c.Set(strconv.Itoa(i), "value")
}
// Check before.
stats := c.Stats()
if stats.MRUSize != 30 {
t.Errorf("Expected MFU size of 30, got %d", stats.MFUSize)
}
c.FlushMRU()
// Check after.
stats = c.Stats()
if stats.MRUSize != 0 {
t.Errorf("Expected MRU size of 0, got %d", stats.MRUSize)
}
}
func TestFlushMFU(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 1,
AutoEvict: 1000,
})
for i := 0; i < 40; i++ {
c.Set(strconv.Itoa(i), "value")
}
c.Get("0")
c.Get("0")
c.Get("0")
c.Get("0")
c.Get("1")
c.Get("1")
c.Get("1")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
log.Printf("Sleeping for 2 seconds to allow promotions")
time.Sleep(2 * time.Second)
// MFU promotion is already tested in bicache_tests.go
// TestPromoteEvict. This is somewhat of a dupe, but
// ensure that if we're testing for a length of 0
// after a flush that it wasn't 0 to begin with.
// Check before.
stats := c.Stats()
if stats.MFUSize != 3 {
t.Errorf("Expected MFU size of 3, got %d", stats.MFUSize)
}
c.FlushMFU()
// Check after.
stats = c.Stats()
if stats.MFUSize != 0 {
t.Errorf("Expected MFU size of 0, got %d", stats.MFUSize)
}
}
func TestFlushAll(t *testing.T) {
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 1,
AutoEvict: 1000,
})
for i := 0; i < 40; i++ {
c.Set(strconv.Itoa(i), "value")
}
c.Get("0")
c.Get("0")
c.Get("0")
c.Get("0")
c.Get("1")
c.Get("1")
c.Get("1")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
c.Get("2")
log.Printf("Sleeping for 2 seconds to allow promotions")
time.Sleep(2 * time.Second)
// Check before.
stats := c.Stats()
if stats.MFUSize != 3 {
t.Errorf("Expected MFU size of 3, got %d", stats.MFUSize)
}
if stats.MRUSize != 30 {
t.Errorf("Expected MFU size of 30, got %d", stats.MFUSize)
}
c.FlushAll()
// Check after.
stats = c.Stats()
if stats.MFUSize != 0 {
t.Errorf("Expected MFU size of 0, got %d", stats.MFUSize)
}
if stats.MRUSize != 0 {
t.Errorf("Expected MRU size of 0, got %d", stats.MRUSize)
}
}
func TestIntegrity(t *testing.T) {
words := []string{"&c", "'d", "'em", "'ll", "'m", "'mid", "'midst", "'mongst", "'prentice", "'re", "'s", "'sblood", "'sbodikins", "'sdeath", "'sfoot", "'sheart", "'shun", "'slid", "'slife", "'slight", "'snails", "'strewth", "'t", "'til", "'tis", "'twas", "'tween", "'twere", "'twill", "'twixt", "'twould", "'un", "'ve", "1080", "10th", "1st", "2", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "a", "a'", "a's", "a/c", "a1", "aa", "aaa", "aah", "aahed", "aahing", "aahs", "aal", "aalii", "aaliis", "aals", "aam", "aardvark", "aardvarks", "aardwolf", "aardwolves", "aargh", "aaron", "aaronic", "aarrgh", "aarrghh", "aas", "aasvogel", "aasvogels", "ab", "aba", "abac", "abaca", "abacas", "abacate", "abacaxi", "abacay", "abaci", "abacinate", "abacination", "abacisci", "abaciscus", "abacist", "aback", "abacli", "abacot", "abacterial", "abactinal", "abactinally", "abaction", "abactor", "abaculi", "abaculus", "abacus", "abacuses", "abada", "abaddon", "abadejo", "abadengo", "abadia", "abaff", "abaft", "abaisance", "abaised", "abaiser", "abaisse", "abaissed", "abaka", "abakas", "abalation", "abalienate", "abalienated", "abalienating", "abalienation", "abalone", "abalones", "abamp", "abampere", "abamperes", "abamps", "aband", "abandon", "abandonable", "abandoned", "abandonedly", "abandonee", "abandoner", "abandoners", "abandoning", "abandonment", "abandonments", "abandons", "abandum", "abanet", "abanga", "abannition", "abapical", "abaptiston", "abaptistum", "abarthrosis", "abarticular", "abarticulation", "abas", "abase", "abased", "abasedly", "abasedness", "abasement", "abasements", "abaser", "abasers", "abases", "abash", "abashed", "abashedly", "abashedness", "abashes", "abashing", "abashless", "abashlessly", "abashment", "abashments", "abasia", "abasias", "abasic", "abasing", "abasio", "abask", "abassi", "abastard", "abastardize", "abastral", "abatable", "abatage", "abate", "abated", "abatement", "abatements", "abater", "abaters", "abates", "abatic", "abating", "abatis", "abatised", "abatises", "abatjour", "abatjours", "abaton", "abator", "abators", "abattage", "abattis", "abattised", "abattises", "abattoir", "abattoirs", "abattu", "abattue", "abature", "abaue", "abave", "abaxial", "abaxile", "abay", "abayah", "abaze", "abb", "abba", "abbacies", "abbacomes", "abbacy", "abbandono", "abbas", "abbasi", "abbasid", "abbassi", "abbate", "abbatial", "abbatical", "abbatie", "abbaye", "abbe", "abbes", "abbess", "abbesses", "abbest", "abbevillian", "abbey", "abbey's", "abbeys", "abbeystead", "abbeystede", "abboccato", "abbogada", "abbot", "abbot's", "abbotcies", "abbotcy", "abbotnullius", "abbotric", "abbots", "abbotship", "abbotships", "abbott", "abbozzo", "abbr", "abbrev", "abbreviatable", "abbreviate", "abbreviated", "abbreviately", "abbreviates", "abbreviating", "abbreviation", "abbreviations", "abbreviator", "abbreviators", "abbreviatory", "abbreviature", "abbroachment", "abby", "abc", "abcess", "abcissa", "abcoulomb", "abd", "abdal", "abdali", "abdaria", "abdat", "abdest", "abdicable", "abdicant", "abdicate", "abdicated", "abdicates", "abdicating", "abdication", "abdications", "abdicative", "abdicator", "abditive", "abditory", "abdom", "abdomen", "abdomen's", "abdomens", "abdomina", "abdominal", "abdominales", "abdominalia", "abdominalian", "abdominally", "abdominals", "abdominoanterior", "abdominocardiac", "abdominocentesis", "abdominocystic", "abdominogenital", "abdominohysterectomy", "abdominohysterotomy", "abdominoposterior", "abdominoscope", "abdominoscopy", "abdominothoracic", "abdominous", "abdominovaginal", "abdominovesical", "abduce", "abduced", "abducens", "abducent", "abducentes", "abduces", "abducing", "abduct", "abducted", "abducting", "abduction", "abduction's", "abductions", "abductor", "abductor's", "abductores", "abductors", "abducts", "abeam", "abear", "abearance", "abecedaire", "abecedaria", "abecedarian", "abecedarians", "abecedaries", "abecedarium", "abecedarius", "abecedary", "abed", "abede", "abedge", "abegge", "abeigh", "abel", "abele", "abeles", "abelian", "abelite", "abelmosk", "abelmosks", "abelmusk", "abeltree", "abend", "abends", "abenteric", "abepithymia", "aberdavine", "aberdeen", "aberdevine", "aberduvine", "abernethy", "aberr", "aberrance", "aberrancies", "aberrancy", "aberrant", "aberrantly", "aberrants", "aberrate", "aberrated", "aberrating", "aberration", "aberrational", "aberrations", "aberrative", "aberrator", "aberrometer", "aberroscope", "aberuncate", "aberuncator", "abesse", "abessive", "abet", "abetment", "abetments", "abets", "abettal", "abettals", "abetted", "abetter", "abetters", "abetting", "abettor", "abettors", "abevacuation", "abey", "abeyance", "abeyances", "abeyancies", "abeyancy", "abeyant", "abfarad", "abfarads", "abhenries", "abhenry", "abhenrys", "abhinaya", "abhiseka", "abhominable", "abhor", "abhorred", "abhorrence", "abhorrences", "abhorrency", "abhorrent", "abhorrently", "abhorrer", "abhorrers", "abhorrible", "abhorring", "abhors", "abib", "abichite", "abidal", "abidance", "abidances", "abidden", "abide", "abided", "abider", "abiders", "abides", "abidi", "abiding", "abidingly", "abidingness", "abiegh", "abience", "abient", "abietate", "abietene", "abietic", "abietin", "abietineous", "abietinic", "abietite", "abigail", "abigails", "abigailship", "abigeat", "abigei", "abigeus", "abilao", "abilene", "abiliment", "abilitable", "abilities", "ability", "ability's", "abilla", "abilo", "abime", "abintestate", "abiogeneses", "abiogenesis", "abiogenesist", "abiogenetic", "abiogenetical", "abiogenetically", "abiogenist", "abiogenous", "abiogeny", "abiological", "abiologically", "abiology", "abioses", "abiosis", "abiotic", "abiotical", "abiotically", "abiotrophic", "abiotrophy", "abir", "abirritant", "abirritate", "abirritated", "abirritating", "abirritation", "abirritative", "abiston", "abit", "abiuret", "abject", "abjectedness", "abjection", "abjections"}
c, _ := bicache.New(&bicache.Config{
MFUSize: 10,
MRUSize: 30,
ShardCount: 8,
AutoEvict: 2000,
})
for _, w := range words {
c.Set(w, w)
}
// Test pre-eviction integrity.
for _, w := range words {
if v := c.Get(w); v != nil && v != w {
t.Errorf("Expected value %s, got %s", v, w)
}
}
time.Sleep(3 * time.Second)
// Test post-eviction integrity.
for _, w := range words {
if v := c.Get(w); v != nil && v != w {
t.Errorf("Expected value %s, got %s", v, w)
}
}
c.FlushAll()
// Test integrity for keys that
// traverse MFU/MRU.
// Add item targeted for MFU.
c.Set("promoted", "promoted")
for i := 0; i < 20; i++ {
c.Get("promoted")
}
// Seq scan to exhaust MRU.
for _, w := range words {
c.Set(w, w)
}
time.Sleep(3 * time.Second)
// Check that target item
// was promoted.
if c.Get("promoted") != "promoted" {
t.Errorf(`Expected MFU item value "promoted", got "%s"`, c.Get("promoted"))
}
// Check MFU key state.
keys := c.List(len(words))
for _, k := range keys {
if k.Key == "promoted" && k.State != 1 {
t.Errorf("Expected key state 1, got %d", k.State)
}
}
c, _ = bicache.New(&bicache.Config{
MFUSize: 1,
MRUSize: 30,
ShardCount: 1,
AutoEvict: 2000,
})
// Add item targeted for MFU
// promotion then demotion.
c.Set("promoted", "promoted")
for i := 0; i < 5; i++ {
c.Get("promoted")
}
// Seq scan to exhaust MRU.
for _, w := range words {
c.Set(w, w)
}
time.Sleep(3 * time.Second)
// Check that the promoted key
// has the proper key state.
keys = c.List(len(words))
for _, k := range keys {
if k.State == 1 && k.Key != "promoted" {
t.Errorf(`Expected key name "promoted", got "%s"`, k.Key)
}
}
// Set MFU replacement target.
c.Set("replacement", "replacement")
for i := 0; i < 10; i++ {
c.Get("replacement")
}
// Seq scan to exhaust MRU.
for _, w := range words {
c.Set(w, w)
}
time.Sleep(3 * time.Second)
// Check that the promoted key
// has the proper key state.
keys = c.List(len(words))
for _, k := range keys {
if k.State == 1 && k.Key != "replacement" {
t.Errorf(`Expected key name "replacement", got "%s"`, k.Key)
}
}
// Check that the demoted key
// has the proper key state.
keys = c.List(len(words))
for _, k := range keys {
if k.Key == "promoted" && k.State != 0 {
t.Errorf(`Expected key "promoted" to be in state 0, got "%d"`, k.State)
}
}
// Previous and new MFU keys
// should still be present.
if c.Get("replacement") != "replacement" || c.Get("promoted") != "promoted" {
t.Errorf("Unexpected cache miss")
}
}