This repository was archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlrucache_test.go
235 lines (184 loc) · 4.24 KB
/
lrucache_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
package lrucache
import (
"math/rand"
"runtime"
"strconv"
"testing"
"time"
"github.com/gregjones/httpcache"
"github.com/stretchr/testify/assert"
)
var entries = []struct {
key string
value string
}{
{"1", "one"},
{"2", "two"},
{"3", "three"},
{"4", "four"},
{"5", "five"},
}
func TestInterface(t *testing.T) {
var h httpcache.Cache = New(1000000, 0)
if assert.NotNil(t, h) {
_, ok := h.Get("missing")
assert.False(t, ok)
}
}
func TestCache(t *testing.T) {
c := New(1000000, 0)
for _, e := range entries {
c.Set(e.key, []byte(e.value))
}
c.Delete("missing")
_, ok := c.Get("missing")
assert.False(t, ok)
for _, e := range entries {
value, ok := c.Get(e.key)
if assert.True(t, ok) {
assert.Equal(t, e.value, string(value))
}
}
for _, e := range entries {
c.Delete(e.key)
_, ok := c.Get(e.key)
assert.False(t, ok)
}
}
func TestSize(t *testing.T) {
c := New(1000000, 0)
assert.Equal(t, int64(0), c.size)
// Check that size is overhead + len(key) + len(value)
c.Set("some", []byte("text"))
assert.Equal(t, int64(entryOverhead+8), c.size)
// Replace key
c.Set("some", []byte("longer text"))
assert.Equal(t, int64(entryOverhead+15), c.size)
assert.Equal(t, c.size, c.Size())
c.Delete("some")
assert.Equal(t, int64(0), c.size)
}
func TestMaxSize(t *testing.T) {
c := New(entryOverhead*2+20, 0)
for _, e := range entries {
c.Set(e.key, []byte(e.value))
}
// Make sure only the last two entries were kept.
assert.Equal(t, int64(entryOverhead*2+10), c.size)
}
func TestMaxAge(t *testing.T) {
c := New(1000000, 86400)
now := time.Now().Unix()
expected := now + 86400
// Add one expired entry
c.Set("foo", []byte("bar"))
c.lru.Back().Value.(*entry).expires = now
// Set a few and verify expiration times
for _, s := range entries {
c.Set(s.key, []byte(s.value))
e := c.lru.Back().Value.(*entry)
assert.True(t, e.expires >= expected && e.expires <= expected+10)
}
// Make sure we can get them all
for _, s := range entries {
_, ok := c.Get(s.key)
assert.True(t, ok)
}
// Make sure only non-expired entries are still in the cache
assert.Equal(t, int64(entryOverhead*5+24), c.size)
// Expire all entries
for _, s := range entries {
le, ok := c.cache[s.key]
if assert.True(t, ok) {
le.Value.(*entry).expires = now
}
}
// Get one expired entry, which should clear all expired entries
_, ok := c.Get("3")
assert.False(t, ok)
assert.Equal(t, int64(0), c.size)
}
func TestRace(t *testing.T) {
c := New(100000, 0)
for worker := 0; worker < 8; worker++ {
go testRaceWorker(c)
}
}
func testRaceWorker(c *LruCache) {
v := []byte("value")
for n := 0; n < 1000; n++ {
c.Set(randKey(100), v)
_, _ = c.Get(randKey(200))
c.Delete(randKey(100))
_ = c.Size()
}
}
func TestOverhead(t *testing.T) {
if testing.Short() || !testing.Verbose() {
t.SkipNow()
}
num := int64(1000000)
c := New(num*1000, 0)
mem := readMem()
for n := 0; int64(n) < num; n++ {
c.Set(strconv.Itoa(n), []byte(randKey(1000000000)))
}
mem = readMem() - mem
stored := c.Size() - num*entryOverhead
t.Log("entryOverhead =", (mem-stored)/num)
}
func BenchmarkSet(b *testing.B) {
v := []byte("value")
c := benchSetup(b, 10000000, 10000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
c.Set(randKey(10000), v)
}
})
}
func BenchmarkGet(b *testing.B) {
c := benchSetup(b, 10000000, 10000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = c.Get(randKey(20000))
}
})
}
func BenchmarkSize(b *testing.B) {
c := benchSetup(b, 10000000, 10000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = c.Size()
}
})
}
func BenchmarkSetGetDeleteSize(b *testing.B) {
v := []byte("value")
c := benchSetup(b, 10000000, 10000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
c.Set(randKey(10000), v)
_, _ = c.Get(randKey(20000))
c.Delete(randKey(10000))
_ = c.Size()
}
})
}
func benchSetup(b *testing.B, size int64, entries int) *LruCache {
c := New(size, 0)
v := []byte("value")
for i := 0; i < entries; i++ {
c.Set(strconv.Itoa(i), v)
}
b.ResetTimer()
return c
}
func randKey(n int32) string {
return strconv.Itoa(int(rand.Int31n(n)))
}
func readMem() int64 {
m := runtime.MemStats{}
runtime.GC()
runtime.ReadMemStats(&m)
return int64(m.Alloc)
}