-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_test.go
313 lines (276 loc) · 7.6 KB
/
stats_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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache_test
import (
"context"
"errors"
"fmt"
"math/rand"
"runtime"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/actforgood/xcache"
)
//nolint:lll
func TestStats_String(t *testing.T) {
t.Parallel()
// arrange
tests := [...]struct {
name string
subject xcache.Stats
expectedResult any
}{
{
name: "mb memory/gb memory",
subject: xcache.Stats{
Memory: 10.5 * 1024 * 1024,
MaxMemory: 4 * 1024 * 1024 * 1024,
Hits: 50,
Misses: 100,
Keys: 355,
Expired: 129,
Evicted: 3,
},
expectedResult: "mem=10.50M maxMem=4G memUsage=0.26% hits=50 misses=100 hitRate=33.33% keys=355 expired=129 evicted=3",
},
{
name: "b memory/kb memory",
subject: xcache.Stats{
Memory: 999,
MaxMemory: 1998,
Hits: 30,
Misses: 70,
Keys: 1,
Expired: 0,
Evicted: 0,
},
expectedResult: "mem=999B maxMem=1.95K memUsage=50.00% hits=30 misses=70 hitRate=30.00% keys=1 expired=0 evicted=0",
},
{
name: "tb memory, no max mem, no hits, no misses",
subject: xcache.Stats{
Memory: 1024 * 1024 * 1024 * 1024,
MaxMemory: 0,
Hits: 0,
Misses: 0,
Keys: 1001,
Expired: 1000002,
Evicted: 50000,
},
expectedResult: "mem=1T maxMem=0B memUsage=100.00% hits=0 misses=0 hitRate=100.00% keys=1001 expired=1000002 evicted=50000",
},
}
for _, testData := range tests {
test := testData // capture range variable
t.Run(test.name, func(t *testing.T) {
// act
result1 := test.subject.String()
result2 := fmt.Sprint(test.subject)
// assert
assertEqual(t, result2, result1)
assertEqual(t, test.expectedResult, result1)
})
}
}
func TestStatsWatcher(t *testing.T) {
t.Parallel()
t.Run("callback is executed periodically", testStatsWatcherCallbackIsExecutedPeriodically)
t.Run("Close stops watching", testStatsWatcherCloseStopsWatching)
t.Run("cancel context stops watching", testStatsWatcherCancelContextStopsWatching)
t.Run("finalizer is called", testStatsWatcherFinalizerIsCalled)
}
func testStatsWatcherCallbackIsExecutedPeriodically(t *testing.T) {
t.Parallel()
// arrange
var (
cache = new(xcache.Mock)
subject = xcache.NewStatsWatcher(cache, 400*time.Millisecond)
ctx = context.Background()
expectedStats1 = xcache.Stats{
Memory: 1024,
MaxMemory: 2048,
Hits: 100,
Misses: 1001,
Keys: 300,
Expired: 200,
Evicted: 900,
}
expectedStats2 = xcache.Stats{
Memory: 4096,
MaxMemory: 4096,
Hits: 1,
Misses: 2,
Keys: 3,
Expired: 4,
Evicted: 5,
}
expectedErr = errors.New("intentionally triggered Stats error")
callsCnt uint32
fn = func(ctxx context.Context, s xcache.Stats, err error) {
atomic.AddUint32(&callsCnt, 1)
assertEqual(t, ctx, ctxx)
switch atomic.LoadUint32(&callsCnt) {
case 1:
assertEqual(t, expectedStats1, s)
assertNil(t, err)
case 2:
assertEqual(t, xcache.Stats{}, s)
assertTrue(t, errors.Is(err, expectedErr))
case 3:
assertEqual(t, expectedStats2, s)
assertNil(t, err)
}
}
)
defer subject.Close()
cache.SetStatsCallback(func(ctxx context.Context) (xcache.Stats, error) {
assertEqual(t, ctx, ctxx)
switch cache.StatsCallsCount() {
case 1:
return expectedStats1, nil
case 2:
return xcache.Stats{}, expectedErr
case 3:
return expectedStats2, nil
}
return xcache.Stats{}, nil
})
// act
subject.Watch(ctx, fn)
// assert
time.Sleep(1500 * time.Millisecond)
assertEqual(t, 3, cache.StatsCallsCount())
assertEqual(t, uint32(3), atomic.LoadUint32(&callsCnt))
}
func testStatsWatcherCloseStopsWatching(t *testing.T) {
t.Parallel()
// arrange
var (
cache = new(xcache.Mock)
subject = xcache.NewStatsWatcher(cache, 500*time.Millisecond)
callsCnt uint32
fn = func(context.Context, xcache.Stats, error) {
atomic.AddUint32(&callsCnt, 1)
}
)
subject.Watch(context.Background(), fn)
// act
time.Sleep(50 * time.Millisecond)
err := subject.Close()
time.Sleep(700 * time.Millisecond)
// assert
assertNil(t, err)
assertEqual(t, 0, cache.StatsCallsCount())
assertEqual(t, uint32(0), atomic.LoadUint32(&callsCnt))
}
func testStatsWatcherCancelContextStopsWatching(t *testing.T) {
t.Parallel()
// arrange
var (
cache = new(xcache.Mock)
ctx, cancelCtx = context.WithCancel(context.Background())
subject = xcache.NewStatsWatcher(cache, 500*time.Millisecond)
callsCnt uint32
fn = func(context.Context, xcache.Stats, error) {
atomic.AddUint32(&callsCnt, 1)
}
)
subject.Watch(ctx, fn)
// act
time.Sleep(50 * time.Millisecond)
cancelCtx()
time.Sleep(700 * time.Millisecond)
// assert
assertEqual(t, 0, cache.StatsCallsCount())
assertEqual(t, uint32(0), atomic.LoadUint32(&callsCnt))
}
func testStatsWatcherFinalizerIsCalled(t *testing.T) {
// test finalizer is called if we "forget" to call Close.
// arrange
var (
cache = new(xcache.Mock)
subject = xcache.NewStatsWatcher(cache, 500*time.Millisecond)
callsCnt uint32
fn = func(context.Context, xcache.Stats, error) {
atomic.AddUint32(&callsCnt, 1)
}
)
subject.Watch(context.Background(), fn)
// act
time.Sleep(50 * time.Millisecond)
runtime.GC()
time.Sleep(700 * time.Millisecond)
// assert
assertEqual(t, 0, cache.StatsCallsCount())
assertEqual(t, uint32(0), atomic.LoadUint32(&callsCnt))
}
func BenchmarkStats_String(b *testing.B) {
stats := xcache.Stats{
Memory: 512 * 1024,
MaxMemory: 1.5 * 1024 * 1024,
Hits: 9000000,
Misses: 1000000,
Keys: 9000000,
Expired: 14000000,
Evicted: 10000000,
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = stats.String()
}
}
func ExampleStatsWatcher() {
// initialize our application cache...
cache := xcache.NewMemory(10 * 1024 * 1024) // 10 Mb
ctx, cancelCtx := context.WithCancel(context.Background())
// perform some operations upon cache to have some data...
var wg sync.WaitGroup
wg.Add(1)
go generateRandomStats(ctx, cache, &wg)
// initialize our stats watcher, which will execute a logging callback every second.
subject := xcache.NewStatsWatcher(cache, time.Second)
defer subject.Close() // close your watcher! (at your app shutdown eventually)
// start watching
subject.Watch(
context.Background(),
func(_ context.Context, stats xcache.Stats, err error) {
if err != nil {
fmt.Println("could not get cache stats:" + err.Error())
} else { // do something useful with stats, log / sent it to a metrics system...
fmt.Println(stats)
}
},
)
time.Sleep(3 * time.Second)
cancelCtx() // cancel data generator goroutine
wg.Wait() // wait for data generator goroutine to finish
// should output periodically something like:
// mem=10M maxMem=10M memUsage=100.00% hits=10 misses=1 hitRate=90.91% keys=10 expired=0 evicted=0
}
func generateRandomStats(ctx context.Context, cache xcache.Cache, wg *sync.WaitGroup) {
defer wg.Done()
keyPrefix := "example-stats-watcher-"
value := []byte("Hello Memory Cache")
ttl := 5 * time.Minute
for {
select {
case <-ctx.Done():
return
default:
randLoop := rand.Intn(10)
for i := 0; i <= randLoop; i++ {
key := keyPrefix + strconv.FormatInt(time.Now().UnixNano(), 10)
_ = cache.Save(ctx, key, value, ttl)
_, _ = cache.Load(ctx, key)
}
_, _ = cache.Load(ctx, keyPrefix+"miss")
time.Sleep(100 * time.Millisecond)
}
}
}