-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_level_cache_bench_test.go
More file actions
306 lines (262 loc) · 7.33 KB
/
two_level_cache_bench_test.go
File metadata and controls
306 lines (262 loc) · 7.33 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
package echo_http_cache
import (
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/kenshin579/echo-http-cache/test"
)
// setupBenchmarkStores creates memory and redis stores for benchmarking
func setupBenchmarkStores(b *testing.B) (CacheStore, CacheStore, func()) {
memoryStore := NewCacheMemoryStore()
db, _ := test.NewRedisDB()
redisStore := NewCacheRedisStoreWithConfig(redis.Options{
Addr: db.Addr(),
})
cleanup := func() {
db.Close()
}
return memoryStore, redisStore, cleanup
}
// BenchmarkTwoLevelCache_L1Hit benchmarks L1 cache hits
func BenchmarkTwoLevelCache_L1Hit(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "write-through",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "sync",
CacheWarming: true,
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
// Pre-populate cache
key := uint64(12345)
value := []byte("benchmark-value")
expiration := time.Now().Add(10 * time.Minute)
store.Set(key, value, expiration)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = store.Get(key)
}
})
}
// BenchmarkTwoLevelCache_L2Hit benchmarks L2 cache hits (L1 miss)
func BenchmarkTwoLevelCache_L2Hit(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "write-through",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "sync",
CacheWarming: false, // Disable warming to test L2 hits
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
// Pre-populate only L2
key := uint64(12346)
value := []byte("benchmark-value-l2")
expiration := time.Now().Add(10 * time.Minute)
redisStore.Set(key, value, expiration)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = store.Get(key)
}
})
}
// BenchmarkTwoLevelCache_Miss benchmarks cache misses
func BenchmarkTwoLevelCache_Miss(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "write-through",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "sync",
CacheWarming: false,
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
// Use different keys to ensure misses
key := uint64(99999 + i)
_, _ = store.Get(key)
i++
}
})
}
// BenchmarkTwoLevelCache_Set_WriteThrough benchmarks write-through strategy
func BenchmarkTwoLevelCache_Set_WriteThrough(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "write-through",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "sync",
CacheWarming: true,
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
value := []byte("benchmark-set-value")
expiration := time.Now().Add(10 * time.Minute)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := uint64(20000 + i)
store.Set(key, value, expiration)
i++
}
})
}
// BenchmarkTwoLevelCache_Set_WriteBack benchmarks write-back strategy
func BenchmarkTwoLevelCache_Set_WriteBack(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "write-back",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "async",
CacheWarming: true,
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
value := []byte("benchmark-writeback-value")
expiration := time.Now().Add(10 * time.Minute)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := uint64(30000 + i)
store.Set(key, value, expiration)
i++
}
})
}
// BenchmarkTwoLevelCache_Set_CacheAside benchmarks cache-aside strategy
func BenchmarkTwoLevelCache_Set_CacheAside(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "cache-aside",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "sync",
CacheWarming: true,
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
value := []byte("benchmark-cacheaside-value")
expiration := time.Now().Add(10 * time.Minute)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
key := uint64(40000 + i)
store.Set(key, value, expiration)
i++
}
})
}
// BenchmarkTwoLevelCache_Mixed benchmarks mixed read/write operations
func BenchmarkTwoLevelCache_Mixed(b *testing.B) {
memoryStore, redisStore, cleanup := setupBenchmarkStores(b)
defer cleanup()
config := TwoLevelConfig{
L1Store: memoryStore,
L2Store: redisStore,
Strategy: "write-through",
L1TTL: 5 * time.Minute,
L2TTL: 30 * time.Minute,
SyncMode: "sync",
CacheWarming: true,
}
store := NewCacheTwoLevelStoreWithConfig(config)
twoLevel := store.(*CacheTwoLevelStore)
defer twoLevel.Stop()
// Pre-populate some data
for i := 0; i < 100; i++ {
key := uint64(50000 + i)
value := []byte("mixed-benchmark-value")
expiration := time.Now().Add(10 * time.Minute)
store.Set(key, value, expiration)
}
value := []byte("mixed-new-value")
expiration := time.Now().Add(10 * time.Minute)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
if i%4 == 0 {
// 25% writes
key := uint64(50000 + (i % 1000))
store.Set(key, value, expiration)
} else {
// 75% reads
key := uint64(50000 + (i % 100))
_, _ = store.Get(key)
}
i++
}
})
}
// BenchmarkComparison_SingleLevel_Memory benchmarks single level memory cache
func BenchmarkComparison_SingleLevel_Memory(b *testing.B) {
store := NewCacheMemoryStore()
// Pre-populate cache
key := uint64(60000)
value := []byte("single-memory-value")
expiration := time.Now().Add(10 * time.Minute)
store.Set(key, value, expiration)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = store.Get(key)
}
})
}
// BenchmarkComparison_SingleLevel_Redis benchmarks single level Redis cache
func BenchmarkComparison_SingleLevel_Redis(b *testing.B) {
db, _ := test.NewRedisDB()
defer db.Close()
store := NewCacheRedisStoreWithConfig(redis.Options{
Addr: db.Addr(),
})
// Pre-populate cache
key := uint64(61000)
value := []byte("single-redis-value")
expiration := time.Now().Add(10 * time.Minute)
store.Set(key, value, expiration)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = store.Get(key)
}
})
}