forked from rbaliyan/event-dlq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
201 lines (176 loc) · 4.81 KB
/
Copy pathbenchmark_test.go
File metadata and controls
201 lines (176 loc) · 4.81 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
package dlq
import (
"context"
"errors"
"testing"
"time"
"github.com/rbaliyan/event/v3/transport"
)
// BenchmarkMessageCreation benchmarks creating Message structs
func BenchmarkMessageCreation(b *testing.B) {
now := time.Now()
payload := []byte(`{"order_id": "12345", "amount": 99.99}`)
metadata := map[string]string{"source": "checkout"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Message{
ID: "dlq-msg-" + string(rune(i)),
EventName: "orders.created",
OriginalID: "orig-" + string(rune(i)),
Payload: payload,
Metadata: metadata,
Error: "processing failed",
RetryCount: 3,
CreatedAt: now,
Source: "order-service",
}
}
}
// BenchmarkMemoryStoreStore benchmarks the Store operation
func BenchmarkMemoryStoreStore(b *testing.B) {
ctx := context.Background()
store := NewMemoryStore()
payload := []byte(`{"order_id": "12345"}`)
now := time.Now()
b.ResetTimer()
for i := 0; i < b.N; i++ {
msg := &Message{
ID: "dlq-msg-" + string(rune(i)),
EventName: "orders.created",
OriginalID: "orig-" + string(rune(i)),
Payload: payload,
Error: "processing failed",
RetryCount: 3,
CreatedAt: now,
Source: "order-service",
}
_ = store.Store(ctx, msg)
}
}
// BenchmarkMemoryStoreGet benchmarks the Get operation
func BenchmarkMemoryStoreGet(b *testing.B) {
ctx := context.Background()
store := NewMemoryStore()
payload := []byte(`{"order_id": "12345"}`)
now := time.Now()
// Pre-populate store
for i := 0; i < 1000; i++ {
msg := &Message{
ID: "dlq-msg-" + string(rune(i)),
EventName: "orders.created",
OriginalID: "orig-" + string(rune(i)),
Payload: payload,
Error: "processing failed",
RetryCount: 3,
CreatedAt: now,
Source: "order-service",
}
_ = store.Store(ctx, msg)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = store.Get(ctx, "dlq-msg-"+string(rune(i%1000)))
}
}
// BenchmarkMemoryStoreList benchmarks the List operation
func BenchmarkMemoryStoreList(b *testing.B) {
ctx := context.Background()
store := NewMemoryStore()
payload := []byte(`{"order_id": "12345"}`)
now := time.Now()
// Pre-populate store
for i := 0; i < 1000; i++ {
msg := &Message{
ID: "dlq-msg-" + string(rune(i)),
EventName: "orders.created",
OriginalID: "orig-" + string(rune(i)),
Payload: payload,
Error: "processing failed",
RetryCount: 3,
CreatedAt: now,
Source: "order-service",
}
_ = store.Store(ctx, msg)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = store.List(ctx, Filter{Limit: 100})
}
}
// BenchmarkMemoryStoreCount benchmarks the Count operation
func BenchmarkMemoryStoreCount(b *testing.B) {
ctx := context.Background()
store := NewMemoryStore()
payload := []byte(`{"order_id": "12345"}`)
now := time.Now()
// Pre-populate store
for i := 0; i < 1000; i++ {
msg := &Message{
ID: "dlq-msg-" + string(rune(i)),
EventName: "orders.created",
OriginalID: "orig-" + string(rune(i)),
Payload: payload,
Error: "processing failed",
RetryCount: 3,
CreatedAt: now,
Source: "order-service",
}
_ = store.Store(ctx, msg)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = store.Count(ctx, Filter{})
}
}
// BenchmarkManagerStore benchmarks the Manager.Store operation
func BenchmarkManagerStore(b *testing.B) {
ctx := context.Background()
store := NewMemoryStore()
tr := &benchTransport{}
manager := mustNewManagerWithTransport(b, store, tr)
payload := []byte(`{"order_id": "12345"}`)
err := errors.New("processing failed")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = manager.Store(ctx, StoreParams{
EventName: "orders.created",
OriginalID: "orig-" + string(rune(i)),
Payload: payload,
Metadata: map[string]string{"key": "value"},
Err: err,
RetryCount: 3,
Source: "order-service",
})
}
}
// BenchmarkFilterCreation benchmarks creating Filter structs
func BenchmarkFilterCreation(b *testing.B) {
now := time.Now()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = Filter{
EventName: "orders.created",
StartTime: now.Add(-24 * time.Hour),
EndTime: now,
ExcludeRetried: true,
Limit: 100,
}
}
}
// benchTransport is a minimal transport for benchmarks
type benchTransport struct{}
func (t *benchTransport) Publish(ctx context.Context, event string, msg transport.Message) error {
return nil
}
func (t *benchTransport) Subscribe(ctx context.Context, event string, opts ...transport.SubscribeOption) (transport.Subscription, error) {
return nil, nil
}
func (t *benchTransport) RegisterEvent(ctx context.Context, event string) error {
return nil
}
func (t *benchTransport) UnregisterEvent(ctx context.Context, event string) error {
return nil
}
func (t *benchTransport) Close(ctx context.Context) error {
return nil
}