-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmap_broker_redis_bench_test.go
More file actions
415 lines (363 loc) · 9.87 KB
/
Copy pathmap_broker_redis_bench_test.go
File metadata and controls
415 lines (363 loc) · 9.87 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//go:build integration
package centrifuge
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
)
func setupMapBrokerBench(b *testing.B) (*RedisMapBroker, func()) {
b.Helper()
node, _ := New(Config{})
broker := newTestRedisMapBroker(b, node)
return broker, func() {
_ = node.Shutdown(context.Background())
}
}
// BenchmarkRedisMapBroker_PublishStreamOnly benchmarks publishing to stream without state.
func BenchmarkRedisMapBroker_PublishStreamOnly(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_stream")
b.ReportAllocs()
b.ResetTimer()
var counter int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := atomic.AddInt64(&counter, 1)
data := []byte(fmt.Sprintf("message_%d", i))
_, err := broker.Publish(ctx, channel, "", MapPublishOptions{
Data: data,
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_PublishMapStateSimple benchmarks simple keyed state (HASH only).
func BenchmarkRedisMapBroker_PublishMapStateSimple(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_map_simple")
b.ReportAllocs()
b.ResetTimer()
var counter int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := atomic.AddInt64(&counter, 1)
key := fmt.Sprintf("key%d", i)
data := []byte(fmt.Sprintf("data%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_PublishMapStateOrdered benchmarks ordered keyed state (HASH+ZSET).
func BenchmarkRedisMapBroker_PublishMapStateOrdered(b *testing.B) {
b.Helper()
node, _ := New(Config{
Map: MapConfig{
GetMapChannelOptions: func(channel string) MapChannelOptions {
return MapChannelOptions{
Mode: MapModeRecoverable,
ordered: true,
StreamSize: 10000,
StreamTTL: 300 * time.Second,
MetaTTL: time.Hour,
KeyTTL: 300 * time.Second,
}
},
},
})
broker := newTestRedisMapBroker(b, node)
defer func() { _ = node.Shutdown(context.Background()) }()
ctx := context.Background()
channel := randomChannel("bench_map_ordered")
b.ReportAllocs()
b.ResetTimer()
var counter int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := atomic.AddInt64(&counter, 1)
key := fmt.Sprintf("key%d", i)
data := []byte(fmt.Sprintf("data%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
score: i,
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_PublishCombined benchmarks publishing with stream + state.
func BenchmarkRedisMapBroker_PublishCombined(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_combined")
b.ReportAllocs()
b.ResetTimer()
var counter int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := atomic.AddInt64(&counter, 1)
key := fmt.Sprintf("key%d", i)
data := []byte(fmt.Sprintf("data%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_ReadStream benchmarks reading from stream.
func BenchmarkRedisMapBroker_ReadStream(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_read_stream")
// Prepopulate stream with 1000 messages
var sp StreamPosition
for i := 0; i < 1000; i++ {
data := []byte(fmt.Sprintf("message_%d", i))
res, err := broker.Publish(ctx, channel, "", MapPublishOptions{
Data: data,
})
if err != nil {
b.Fatal(err)
}
sp = res.Position
}
b.ReportAllocs()
b.ResetTimer()
sp.Offset = sp.Offset - 1000
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := broker.ReadStream(ctx, channel, MapReadStreamOptions{
Filter: StreamFilter{
Limit: 1000,
Since: &sp,
},
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_ReadStateFull benchmarks reading full unordered state.
func BenchmarkRedisMapBroker_ReadStateFull(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_read_state")
// Prepopulate state with 1000 entries
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("key%d", i)
data := []byte(fmt.Sprintf("data%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
})
if err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := broker.ReadState(ctx, channel, MapReadStateOptions{
Limit: -1, // Read all
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_ReadStatePaginated benchmarks paginated state reads.
func BenchmarkRedisMapBroker_ReadStatePaginated(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_read_state_paginated")
// Prepopulate state with 1000 entries
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("key%d", i)
data := []byte(fmt.Sprintf("data%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
})
if err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := broker.ReadState(ctx, channel, MapReadStateOptions{
Limit: 100, // Read 100 at a time
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_ReadStateOrdered benchmarks reading ordered state.
func BenchmarkRedisMapBroker_ReadStateOrdered(b *testing.B) {
b.Helper()
node, _ := New(Config{
Map: MapConfig{
GetMapChannelOptions: func(channel string) MapChannelOptions {
return MapChannelOptions{
Mode: MapModeRecoverable,
ordered: true,
StreamSize: 10000,
StreamTTL: 300 * time.Second,
MetaTTL: time.Hour,
KeyTTL: 300 * time.Second,
}
},
},
})
broker := newTestRedisMapBroker(b, node)
defer func() { _ = node.Shutdown(context.Background()) }()
ctx := context.Background()
channel := randomChannel("bench_read_state_ordered")
// Prepopulate ordered state with 1000 entries
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("key%d", i)
data := []byte(fmt.Sprintf("data%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
score: int64(i),
})
if err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := broker.ReadState(ctx, channel, MapReadStateOptions{
Limit: 100,
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_IdempotentPublish benchmarks idempotent publishing.
func BenchmarkRedisMapBroker_IdempotentPublish(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_idempotent")
b.ReportAllocs()
b.ResetTimer()
var counter int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := atomic.AddInt64(&counter, 1)
data := []byte(fmt.Sprintf("message_%d", i))
idempotencyKey := fmt.Sprintf("key_%d", i)
_, err := broker.Publish(ctx, channel, "", MapPublishOptions{
Data: data,
IdempotencyKey: idempotencyKey,
IdempotentResultTTL: 60 * time.Second,
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_VersionedPublish benchmarks version-based publishing.
func BenchmarkRedisMapBroker_VersionedPublish(b *testing.B) {
broker, cleanup := setupMapBrokerBench(b)
defer cleanup()
ctx := context.Background()
channel := randomChannel("bench_versioned")
b.ReportAllocs()
b.ResetTimer()
var counter int64
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := atomic.AddInt64(&counter, 1)
key := fmt.Sprintf("key%d", i%100) // Reuse 100 keys
data := []byte(fmt.Sprintf("data_%d", i))
_, err := broker.Publish(ctx, channel, key, MapPublishOptions{
Data: data,
Version: uint64(i),
})
if err != nil {
b.Fatal(err)
}
}
})
}
// BenchmarkRedisMapBroker_Cleanup benchmarks TTL-based key cleanup throughput.
// Measures how fast the cleanup Lua script processes expired keys.
// Throughput in keys/second = keys/op * 1e9 / ns_per_op.
func BenchmarkRedisMapBroker_Cleanup(b *testing.B) {
for _, ordered := range []bool{false, true} {
orderLabel := "unordered"
if ordered {
orderLabel = "ordered"
}
for _, numKeys := range []int{100, 1000} {
b.Run(fmt.Sprintf("%s/keys_%d", orderLabel, numKeys), func(b *testing.B) {
node, _ := New(Config{
Map: MapConfig{
GetMapChannelOptions: func(channel string) MapChannelOptions {
return MapChannelOptions{
Mode: MapModeRecoverable,
KeyTTL: 30 * time.Second,
StreamSize: numKeys * 3,
ordered: ordered,
}
},
},
})
broker := newTestRedisMapBroker(b, node)
ctx := context.Background()
futureNow := time.Now().UnixMilli() + 31_000
b.ReportAllocs()
for i := 0; i < b.N; i++ {
b.StopTimer()
ch := fmt.Sprintf("bench_cleanup_%s_%d_%d", orderLabel, numKeys, i)
for k := 0; k < numKeys; k++ {
_, err := broker.Publish(ctx, ch, fmt.Sprintf("key%d", k), MapPublishOptions{
Data: []byte("data"),
score: int64(k),
})
if err != nil {
b.Fatal(err)
}
}
shardWrapper := broker.shards[0]
cleanupKey := broker.cleanupRegistrationKeyForChannel(shardWrapper.shard, ch)
b.StartTimer()
_ = broker.cleanupChannel(ctx, shardWrapper.shard, ch, cleanupKey, futureNow)
}
b.ReportMetric(float64(numKeys), "keys/op")
})
}
}
}