forked from coocood/freecache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
249 lines (233 loc) · 5.49 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
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\n",
cache.HitRate(), cache.EvacuateCount(), cache.EntryCount(), cache.AverageAccessTime())
}
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))
}
}
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.Error("over write count should be one.")
}
val = append(val, 0)
err = cache.Set(key, val, 0)
if err != ErrLargeEntry {
t.Error("err should be ErrLargeEntry", err)
}
}
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)
}
}