forked from patrickmn/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharded_test.go
More file actions
350 lines (306 loc) · 7.76 KB
/
sharded_test.go
File metadata and controls
350 lines (306 loc) · 7.76 KB
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
package cache
import (
"fmt"
"strconv"
"sync"
"testing"
"time"
)
// func TestDjb33(t *testing.T) {
// }
var shardedKeys = []string{
"f",
"fo",
"foo",
"barf",
"barfo",
"foobar",
"bazbarf",
"bazbart",
"bazbarr",
"bazbare",
"bazbarw",
"bazbarq",
"bazbara",
"bazbarfo",
"bazbarfs",
"bazbarff",
"bazbarfg",
"bazbarfr",
"bazbarfe",
"bazbarfoo",
"bazbarfoi",
"bazbarfou",
"bazbarfoy",
"bazbarfog",
"bazbarfod",
"bazbarfoz",
"bazbarfox",
"bazbarfoc",
"foobarbazq",
"foobarbazy",
"foobarbazu",
"foobarbazi",
"foobarbazo",
"foobarbazl",
"foobarbazk",
"foobarbazj",
"foobarbazh",
"foobarbazf",
"foobarbazs",
"foobarbazz",
"foobarbazqu",
"foobarbazquu",
"foobarbazquux",
}
func TestShardedCache(t *testing.T) {
tc := NewShardedCache[string](13, DefaultExpiration, DefaultExpiration)
for _, v := range shardedKeys {
tc.Set(v, "value", DefaultExpiration)
}
}
// Test for ShardedNumericCache
func TestShardedModifyNumeric(t *testing.T) {
// Create a sharded numeric cache with 5 shards
snc := NewShardedNumeric[int](5, DefaultExpiration, 0)
// Test incrementing a non-existent key
newVal, err := snc.ModifyNumeric("counter", 5, true)
if err != nil {
t.Error("Error incrementing counter:", err)
}
if newVal != 5 {
t.Errorf("Expected counter to be 5, got %d", newVal)
}
// Test incrementing an existing key
newVal, err = snc.ModifyNumeric("counter", 3, true)
if err != nil {
t.Error("Error incrementing counter:", err)
}
if newVal != 8 {
t.Errorf("Expected counter to be 8, got %d", newVal)
}
// Test decrementing
newVal, err = snc.ModifyNumeric("counter", 2, false)
if err != nil {
t.Error("Error decrementing counter:", err)
}
if newVal != 6 {
t.Errorf("Expected counter to be 6, got %d", newVal)
}
// Test with multiple shards
var wg sync.WaitGroup
numKeys := 100
wg.Add(numKeys)
for i := 0; i < numKeys; i++ {
go func(idx int) {
defer wg.Done()
key := fmt.Sprintf("key-%d", idx)
snc.Set(key, 0, DefaultExpiration)
for j := 0; j < 10; j++ {
snc.ModifyNumeric(key, 1, true)
}
}(i)
}
wg.Wait()
// Verify all keys have been incremented to 10
for i := 0; i < numKeys; i++ {
key := fmt.Sprintf("key-%d", i)
val, found := snc.Get(key)
if found != Found {
t.Errorf("Key %s not found", key)
continue
}
if val != 10 {
t.Errorf("Expected value for %s to be 10, got %d", key, val)
}
}
}
func TestShardedIncrDecr(t *testing.T) {
snc := NewShardedNumeric[int](5, DefaultExpiration, 0)
snc.Set("val", 10, DefaultExpiration)
// Test Incr
newVal, err := snc.Incr("val", 5)
if err != nil {
t.Fatalf("Incr failed: %v", err)
}
if newVal != 15 {
t.Errorf("Expected 15 after Incr, got %d", newVal)
}
// Test Decr
newVal, err = snc.Decr("val", 3)
if err != nil {
t.Fatalf("Decr failed: %v", err)
}
if newVal != 12 {
t.Errorf("Expected 12 after Decr, got %d", newVal)
}
}
func TestShardedIncrOverflow(t *testing.T) {
snc := NewShardedNumeric[int8](5, DefaultExpiration, 0)
snc.Set("int8", int8(127), DefaultExpiration)
newVal, err := snc.Incr("int8", 1)
if err != nil {
t.Fatalf("Incr failed: %v", err)
}
if newVal != -128 {
t.Errorf("Expected -128 after overflow, got %d", newVal)
}
}
func TestShardedDecrUnderflow(t *testing.T) {
snc := NewShardedNumeric[uint8](5, DefaultExpiration, 0)
snc.Set("uint8", uint8(0), DefaultExpiration)
newVal, err := snc.Decr("uint8", 1)
if err != nil {
t.Fatalf("Decr failed: %v", err)
}
if newVal != 255 {
t.Errorf("Expected 255 after underflow, got %d", newVal)
}
}
func TestShardedIncrWithoutSet(t *testing.T) {
snc := NewShardedNumeric[int](5, DefaultExpiration, 0)
newVal, err := snc.Incr("newval", 5)
if err != nil {
t.Fatalf("Incr failed: %v", err)
}
if newVal != 5 {
t.Errorf("Expected 5 after Incr on empty key, got %d", newVal)
}
}
func BenchmarkShardedCacheGetExpiring(b *testing.B) {
benchmarkShardedCacheGet(b, 5*time.Minute)
}
func BenchmarkShardedCacheGetNotExpiring(b *testing.B) {
benchmarkShardedCacheGet(b, NoExpiration)
}
func benchmarkShardedCacheGet(b *testing.B, exp time.Duration) {
b.StopTimer()
tc := NewShardedCache[any](10, exp, 0)
tc.Set("foobarba", "zquux", DefaultExpiration)
b.StartTimer()
for i := 0; i < b.N; i++ {
tc.Get("foobarba")
}
}
func BenchmarkShardedCacheGetManyConcurrentExpiring(b *testing.B) {
benchmarkShardedCacheGetManyConcurrent(b, 5*time.Minute)
}
func BenchmarkShardedCacheGetManyConcurrentNotExpiring(b *testing.B) {
benchmarkShardedCacheGetManyConcurrent(b, NoExpiration)
}
func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
b.StopTimer()
n := 10000
tsc := NewShardedCache[any](20, exp, 0)
keys := make([]string, n)
for i := 0; i < n; i++ {
k := "foo" + strconv.Itoa(i)
keys[i] = k
tsc.Set(k, "bar", DefaultExpiration)
}
each := b.N / n
wg := new(sync.WaitGroup)
wg.Add(n)
for _, v := range keys {
go func(k string) {
for j := 0; j < each; j++ {
tsc.Get(k)
}
wg.Done()
}(v)
}
b.StartTimer()
wg.Wait()
}
// Add benchmark for ShardedNumericCache ModifyNumeric
func BenchmarkShardedModifyNumericInt(b *testing.B) {
b.StopTimer()
sc := NewShardedNumeric[int](5, DefaultExpiration, 0)
sc.Set("foo", 0, DefaultExpiration)
b.StartTimer()
for i := 0; i < b.N; i++ {
sc.ModifyNumeric("foo", 1, true) // Increment
}
}
// Add benchmark comparing sharded vs non-sharded numeric operations
func BenchmarkCompareShardedVsRegularModifyNumeric(b *testing.B) {
b.Run("Sharded", func(b *testing.B) {
sc := NewShardedNumeric[int](10, DefaultExpiration, 0)
sc.Set("counter", 0, DefaultExpiration)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sc.ModifyNumeric("counter", 1, true)
}
})
b.Run("Regular", func(b *testing.B) {
nc := newNumericTestCache[int](nil, DefaultExpiration, 0)
nc.Set("counter", 0, DefaultExpiration)
b.ResetTimer()
for i := 0; i < b.N; i++ {
nc.ModifyNumeric("counter", 1, true)
}
})
}
func TestShardedCacheItemExpiration(t *testing.T) {
ts := NewShardedCache[int](2, 50*time.Millisecond, 10*time.Millisecond)
ts.OnEvicted(func(s string, i int) {
fmt.Printf("delete %s (%d)\n", s, i)
})
ts.Set("short-lived", 1, DefaultExpiration)
ts.Set("custom-expiry", 2, 20*time.Millisecond)
ts.Set("no-expiry", 3, NoExpiration)
// Wait for short-lived to expire but custom-expiry to still be alive
time.Sleep(300 * time.Millisecond)
// short-lived should be gone
_, found := ts.Get("short-lived")
if found == Found {
t.Error("short-lived item should have expired")
}
// custom-expiry should be gone
_, found = ts.Get("custom-expiry")
if found == Found {
t.Error("custom-expiry item should have expired")
}
// no-expiry should still be there
val, found := ts.Get("no-expiry")
if found != Found || val != 3 {
t.Error("no-expiry item should still exist with value 3")
}
// Wait for janitor to run multiple times
time.Sleep(50 * time.Millisecond)
// Count should be 1 (just no-expiry)
count := ts.ItemCount()
if count != 1 {
t.Errorf("Expected 1 item, got %d", count)
for i := 0; i < len(ts.Items()); i++ {
bi := ts.Items()[i]
for k, v := range bi {
t.Errorf("item: %s (%v)", k, v)
}
}
}
}
func TestShardedCache_OnEvicted_Capacity(t *testing.T) {
// Create a cache with 4 shards and total capacity of 4 (1 per shard)
sc := NewShardedCache[int](4, DefaultExpiration, 0)
sc.WithCapacity(4)
var mu sync.Mutex
evictedKeys := make([]string, 0)
sc.OnEvicted(func(k string, v int) {
mu.Lock()
evictedKeys = append(evictedKeys, k)
mu.Unlock()
})
// Fill the cache to trigger eviction
// With 4 shards and cap 4, cap per shard is 1.
// Add 20 items to ensure overflow across shards.
for i := 0; i < 20; i++ {
sc.Set(fmt.Sprintf("key-%d", i), i, DefaultExpiration)
}
mu.Lock()
count := len(evictedKeys)
mu.Unlock()
if count == 0 {
t.Errorf("Expected eviction callbacks to be called, got 0")
}
}