-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcache_test.go
300 lines (260 loc) · 6.96 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
package utils
import (
"context"
"fmt"
"math/rand"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func ExampleExpCache() {
cc := NewExpCache[string](context.Background(), 100*time.Millisecond)
cc.Store("key", "val")
cc.Load("key") // return "val"
// data expired
time.Sleep(200 * time.Millisecond)
data, ok := cc.Load("key")
fmt.Println(data)
fmt.Println(ok)
// Output:
// false
}
func TestExpCache_Store(t *testing.T) {
t.Parallel()
Clock.SetInterval(1 * time.Millisecond)
time.Sleep(time.Second) // wait for clock's interval to take effect
startAt := Clock.GetUTCNow()
ttl := 100 * time.Millisecond
cm := NewExpCache[string](context.Background(), ttl)
key := "key"
val := "val"
cm.Store(key, val)
for {
now := Clock.GetUTCNow()
if gotV, ok := cm.Load(key); ok {
require.Equal(t, val, gotV)
require.Less(t, now.Sub(startAt), ttl)
time.Sleep(10 * time.Millisecond)
} else {
require.Greater(t, now.Sub(startAt), ttl)
break
}
}
_, ok := cm.Load(key)
require.False(t, ok)
}
// goos: linux
// goarch: amd64
// pkg: github.com/Laisky/go-utils
// BenchmarkExpMap-8 141680 10275 ns/op 54 B/op 6 allocs/op
// PASS
// ok github.com/Laisky/go-utils 1.573s
func BenchmarkExpMap(b *testing.B) {
cm, err := NewLRUExpiredMap(context.Background(),
10*time.Millisecond,
func() any { return 1 },
)
if err != nil {
b.Fatalf("%+v", err)
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
cm.Get(RandomStringWithLength(1))
}
})
}
func Benchmark_NewSimpleExpCache(b *testing.B) {
c := NewSingleItemExpCache[string](time.Millisecond)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if rand.Intn(10) < 5 {
c.Set(RandomStringWithLength(rand.Intn(100)))
} else {
c.Get()
}
}
})
}
func TestNewSimpleExpCache(t *testing.T) {
t.Parallel()
// another test may change the clock's interval.
// default interval is 10ms, so we need to set interval bigger than 10ms.
//
// time.clock's test set interval to 100ms.
fmt.Println("interval", Clock.Interval())
Clock.SetInterval(1 * time.Millisecond)
time.Sleep(time.Second) // wait for clock's interval to take effect
// This test case used to have a small chance of failure
for i := 0; i < 30; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
c := NewSingleItemExpCache[string](10 * time.Millisecond)
_, ok := c.Get()
require.False(t, ok)
_, ok = c.Get()
require.False(t, ok)
_, ok = c.Get()
require.False(t, ok)
data := "yo"
c.Set(data)
v, ok := c.Get()
require.True(t, ok)
require.Equal(t, data, v)
ret, ok := c.Get()
require.True(t, ok)
require.Equal(t, data, ret)
time.Sleep(25 * time.Millisecond)
v, ok = c.Get()
require.False(t, ok)
require.Equal(t, data, v)
})
}
}
func TestNewExpiredMap(t *testing.T) {
ctx := context.Background()
m, err := NewLRUExpiredMap(ctx, time.Millisecond, func() any { return 666 })
require.NoError(t, err)
const key = "key"
v := m.Get(key)
require.Equal(t, 666, v)
v = m.Get(key)
require.Equal(t, 666, v)
}
func TestNewLruCache(t *testing.T) {
t.Parallel()
c := NewLruCache[string, string](100, time.Millisecond*100)
c.Set("key", "val")
v, ok := c.Get("key")
require.True(t, ok)
require.Equal(t, "val", v)
c.Set("key2", "val2")
v, ok = c.Get("key2")
require.True(t, ok)
require.Equal(t, "val2", v)
v, ok = c.Get("key")
require.True(t, ok)
require.Equal(t, "val", v)
c.Set("key3", "val3")
v, ok = c.Get("key3")
require.True(t, ok)
require.Equal(t, "val3", v)
c.Set("key4", "val4")
v, ok = c.Get("key4")
require.True(t, ok)
require.Equal(t, "val4", v)
}
// goos: linux
// goarch: amd64
// pkg: github.com/Laisky/go-utils/v5
// cpu: Intel(R) Xeon(R) Gold 5320 CPU @ 2.20GHz
// Benchmark_TtlCache
// Benchmark_TtlCache/set
// Benchmark_TtlCache/set-104 107455 13311 ns/op 362 B/op 10 allocs/op
// Benchmark_TtlCache/get
// Benchmark_TtlCache/get-104 740449 1676 ns/op 16 B/op 1 allocs/op
// Benchmark_TtlCache/get_&_set
// Benchmark_TtlCache/get_&_set-104 57244 20544 ns/op 231 B/op 10 allocs/op
func Benchmark_TtlCache(b *testing.B) {
c := NewTtlCache[string]()
start := time.Now().Nanosecond()
b.Run("set", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := start + i
c.Set(strconv.Itoa(v), strconv.Itoa(v), time.Millisecond*100)
}
})
b.Run("get", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Get(strconv.Itoa(start + i))
}
})
b.Run("get & set", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
v := start + rand.Intn(b.N)
c.Set(strconv.Itoa(v), strconv.Itoa(v), time.Millisecond*100)
c.Get(strconv.Itoa(v))
}
})
})
}
// goos: linux
// goarch: amd64
// pkg: github.com/Laisky/go-utils/v5
// cpu: Intel(R) Xeon(R) Gold 5320 CPU @ 2.20GHz
// Benchmark_ExpCache
// Benchmark_ExpCache/set
// Benchmark_ExpCache/set-104 198330 8029 ns/op 285 B/op 7 allocs/op
// Benchmark_ExpCache/get
// Benchmark_ExpCache/get-104 912698 1320 ns/op 16 B/op 1 allocs/op
// Benchmark_ExpCache/get_&_set
// Benchmark_ExpCache/get_&_set-104 61234 21028 ns/op 299 B/op 8 allocs/op
func Benchmark_ExpCache(b *testing.B) {
ctx := context.Background()
c := NewExpCache[string](ctx, time.Millisecond*100)
start := time.Now().Nanosecond()
b.Run("set", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := start + i
c.Store(strconv.Itoa(v), strconv.Itoa(v))
}
})
b.Run("get", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Load(strconv.Itoa(start + i))
}
})
b.Run("get & set", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
v := start + rand.Intn(b.N)
c.Store(strconv.Itoa(v), strconv.Itoa(v))
c.Load(strconv.Itoa(v))
}
})
})
}
// pkg: github.com/Laisky/go-utils/v5
// cpu: Intel(R) Xeon(R) Gold 5320 CPU @ 2.20GHz
// Benchmark_Sieve
// Benchmark_Sieve/set
// Benchmark_Sieve/set-104 144274 9077 ns/op 186 B/op 4 allocs/op
// Benchmark_Sieve/get
// Benchmark_Sieve/get-104 587437 1795 ns/op 16 B/op 1 allocs/op
// Benchmark_Sieve/get_&_set
// Benchmark_Sieve/get_&_set-104 67621 20670 ns/op 179 B/op 5 allocs/op
// PASS
func Benchmark_Sieve(b *testing.B) {
c := NewLruCache[string, string](100000, time.Millisecond*100)
start := time.Now().Nanosecond()
b.Run("set", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
v := start + i
c.Set(strconv.Itoa(v), strconv.Itoa(v))
}
})
b.Run("get", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Get(strconv.Itoa(start + i))
}
})
b.Run("get & set", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
v := start + rand.Intn(b.N)
c.Set(strconv.Itoa(v), strconv.Itoa(v))
c.Get(strconv.Itoa(v))
}
})
})
}