forked from coocood/freecache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
366 lines (337 loc) · 8.35 KB
/
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
package freecache
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"strings"
"testing"
"time"
)
func TestFreeCache(t *testing.T) {
cache := NewCache(1024)
if cache.HitRate() != 0 {
t.Error("initial hit rate should be zero")
}
if cache.AverageAccessTime() != 0 {
t.Error("initial average access time should be zero")
}
key := []byte("abcd")
val := []byte("efghijkl")
err := cache.Set(key, val, 0)
if err != nil {
t.Error("err should be nil")
}
value, err := cache.Get(key)
if err != nil || !bytes.Equal(value, val) {
t.Error("value not equal")
}
affected := cache.Del(key)
if !affected {
t.Error("del should return affected true")
}
value, err = cache.Get(key)
if err != ErrNotFound {
t.Error("error should be ErrNotFound after being deleted")
}
affected = cache.Del(key)
if affected {
t.Error("del should not return affected true")
}
cache.Clear()
n := 5000
for i := 0; i < n; i++ {
keyStr := fmt.Sprintf("key%v", i)
valStr := strings.Repeat(keyStr, 10)
err = cache.Set([]byte(keyStr), []byte(valStr), 0)
if err != nil {
t.Error(err)
}
}
time.Sleep(time.Second)
for i := 1; i < n; i += 2 {
keyStr := fmt.Sprintf("key%v", i)
cache.Get([]byte(keyStr))
}
for i := 1; i < n; i += 8 {
keyStr := fmt.Sprintf("key%v", i)
cache.Del([]byte(keyStr))
}
for i := 0; i < n; i += 2 {
keyStr := fmt.Sprintf("key%v", i)
valStr := strings.Repeat(keyStr, 10)
err = cache.Set([]byte(keyStr), []byte(valStr), 0)
if err != nil {
t.Error(err)
}
}
for i := 1; i < n; i += 2 {
keyStr := fmt.Sprintf("key%v", i)
expectedValStr := strings.Repeat(keyStr, 10)
value, err = cache.Get([]byte(keyStr))
if err == nil {
if string(value) != expectedValStr {
t.Errorf("value is %v, expected %v", string(value), expectedValStr)
}
}
}
t.Logf("hit rate is %v, evacuates %v, entries %v, average time %v, expire count %v\n",
cache.HitRate(), cache.EvacuateCount(), cache.EntryCount(), cache.AverageAccessTime(), cache.ExpiredCount())
cache.ResetStatistics()
t.Logf("hit rate is %v, evacuates %v, entries %v, average time %v, expire count %v\n",
cache.HitRate(), cache.EvacuateCount(), cache.EntryCount(), cache.AverageAccessTime(), cache.ExpiredCount())
}
func TestOverwrite(t *testing.T) {
cache := NewCache(1024)
key := []byte("abcd")
var val []byte
cache.Set(key, val, 0)
val = []byte("efgh")
cache.Set(key, val, 0)
val = append(val, 'i')
cache.Set(key, val, 0)
if count := cache.OverwriteCount(); count != 0 {
t.Error("overwrite count is", count, "expected ", 0)
}
res, _ := cache.Get(key)
if string(res) != string(val) {
t.Error(string(res))
}
val = append(val, 'j')
cache.Set(key, val, 0)
res, _ = cache.Get(key)
if string(res) != string(val) {
t.Error(string(res), "aaa")
}
val = append(val, 'k')
cache.Set(key, val, 0)
res, _ = cache.Get(key)
if string(res) != "efghijk" {
t.Error(string(res))
}
val = append(val, 'l')
cache.Set(key, val, 0)
res, _ = cache.Get(key)
if string(res) != "efghijkl" {
t.Error(string(res))
}
val = append(val, 'm')
cache.Set(key, val, 0)
if count := cache.OverwriteCount(); count != 3 {
t.Error("overwrite count is", count, "expected ", 3)
}
}
func TestExpire(t *testing.T) {
cache := NewCache(1024)
key := []byte("abcd")
val := []byte("efgh")
err := cache.Set(key, val, 1)
if err != nil {
t.Error("err should be nil")
}
time.Sleep(time.Second)
val, err = cache.Get(key)
if err == nil {
t.Fatal("key should be expired", string(val))
}
cache.ResetStatistics()
if cache.ExpiredCount() != 0 {
t.Error("expired count should be zero.")
}
}
func TestTTL(t *testing.T) {
cache := NewCache(1024)
key := []byte("abcd")
val := []byte("efgh")
err := cache.Set(key, val, 2)
if err != nil {
t.Error("err should be nil", err.Error())
}
time.Sleep(time.Second)
ttl, err := cache.TTL(key)
if err != nil {
t.Error("err should be nil", err.Error())
}
if ttl != 1 {
t.Fatalf("ttl should be 1, but %d return", ttl)
}
}
func TestAverageAccessTimeWhenUpdateInplace(t *testing.T) {
cache := NewCache(1024)
key := []byte("test-key")
valueLong := []byte("very-long-de-value")
valueShort := []byte("short")
err := cache.Set(key, valueLong, 0)
if err != nil {
t.Fatal("err should be nil")
}
now := time.Now().Unix()
aat := cache.AverageAccessTime()
if (now - aat) > 1 {
t.Fatalf("track average access time error, now:%d, aat:%d", now, aat)
}
time.Sleep(time.Second * 4)
err = cache.Set(key, valueShort, 0)
if err != nil {
t.Fatal("err should be nil")
}
now = time.Now().Unix()
aat = cache.AverageAccessTime()
if (now - aat) > 1 {
t.Fatalf("track average access time error, now:%d, aat:%d", now, aat)
}
}
func TestAverageAccessTimeWhenUpdateWithNewSpace(t *testing.T) {
cache := NewCache(1024)
key := []byte("test-key")
valueLong := []byte("very-long-de-value")
valueShort := []byte("short")
err := cache.Set(key, valueShort, 0)
if err != nil {
t.Fatal("err should be nil")
}
now := time.Now().Unix()
aat := cache.AverageAccessTime()
if (now - aat) > 1 {
t.Fatalf("track average access time error, now:%d, aat:%d", now, aat)
}
time.Sleep(time.Second * 4)
err = cache.Set(key, valueLong, 0)
if err != nil {
t.Fatal("err should be nil")
}
now = time.Now().Unix()
aat = cache.AverageAccessTime()
if (now - aat) > 2 {
t.Fatalf("track average access time error, now:%d, aat:%d", now, aat)
}
}
func TestLargeEntry(t *testing.T) {
cacheSize := 512 * 1024
cache := NewCache(cacheSize)
key := make([]byte, 65536)
val := []byte("efgh")
err := cache.Set(key, val, 0)
if err != ErrLargeKey {
t.Error("large key should return ErrLargeKey")
}
val, err = cache.Get(key)
if val != nil {
t.Error("value should be nil when get a big key")
}
key = []byte("abcd")
maxValLen := cacheSize/1024 - ENTRY_HDR_SIZE - len(key)
val = make([]byte, maxValLen+1)
err = cache.Set(key, val, 0)
if err != ErrLargeEntry {
t.Error("err should be ErrLargeEntry", err)
}
val = make([]byte, maxValLen-2)
err = cache.Set(key, val, 0)
if err != nil {
t.Error(err)
}
val = append(val, 0)
err = cache.Set(key, val, 0)
if err != nil {
t.Error(err)
}
val = append(val, 0)
err = cache.Set(key, val, 0)
if err != nil {
t.Error(err)
}
if cache.OverwriteCount() != 1 {
t.Errorf("over write count should be one, actual: %d.", cache.OverwriteCount())
}
val = append(val, 0)
err = cache.Set(key, val, 0)
if err != ErrLargeEntry {
t.Error("err should be ErrLargeEntry", err)
}
cache.ResetStatistics()
if cache.OverwriteCount() != 0 {
t.Error("over write count should be zero.")
}
}
func TestInt64Key(t *testing.T) {
cache := NewCache(1024)
err := cache.SetInt(1, []byte("abc"), 0)
if err != nil {
t.Error("err should be nil")
}
err = cache.SetInt(2, []byte("cde"), 0)
if err != nil {
t.Error("err should be nil")
}
val, err := cache.GetInt(1)
if err != nil {
t.Error("err should be nil")
}
if !bytes.Equal(val, []byte("abc")) {
t.Error("value not equal")
}
affected := cache.DelInt(1)
if !affected {
t.Error("del should return affected true")
}
_, err = cache.GetInt(1)
if err != ErrNotFound {
t.Error("error should be ErrNotFound after being deleted")
}
}
func BenchmarkCacheSet(b *testing.B) {
cache := NewCache(256 * 1024 * 1024)
var key [8]byte
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(key[:], uint64(i))
cache.Set(key[:], make([]byte, 8), 0)
}
}
func BenchmarkMapSet(b *testing.B) {
m := make(map[string][]byte)
var key [8]byte
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(key[:], uint64(i))
m[string(key[:])] = make([]byte, 8)
}
}
func BenchmarkCacheGet(b *testing.B) {
b.StopTimer()
cache := NewCache(256 * 1024 * 1024)
var key [8]byte
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(key[:], uint64(i))
cache.Set(key[:], make([]byte, 8), 0)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(key[:], uint64(i))
cache.Get(key[:])
}
}
func BenchmarkMapGet(b *testing.B) {
b.StopTimer()
m := make(map[string][]byte)
var key [8]byte
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(key[:], uint64(i))
m[string(key[:])] = make([]byte, 8)
}
b.StartTimer()
var hitCount int64
for i := 0; i < b.N; i++ {
binary.LittleEndian.PutUint64(key[:], uint64(i))
if m[string(key[:])] != nil {
hitCount++
}
}
}
func BenchmarkHashFunc(b *testing.B) {
key := make([]byte, 8)
rand.Read(key)
b.ResetTimer()
for i := 0; i < b.N; i++ {
hashFunc(key)
}
}