forked from launchdarkly/go-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_processor_benchmark_test.go
183 lines (162 loc) · 5.15 KB
/
event_processor_benchmark_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
package ldclient
import (
"fmt"
"testing"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldtime"
"gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
ldevents "gopkg.in/launchdarkly/go-sdk-events.v1"
)
// These benchmarks cover the DefaultEventProcessor logic for digesting analytics event inputs and producing
// output event data, but not actually sending the event data anywhere.
//
// In the current implementation, event processor tasks are divided between several goroutines. Therefore,
// timing of these operations will have more variability than other benchmarks. However, execution time
// should still be roughly proportional to the volume of work, and allocations should be fairly consistent.
type mockEventSender struct {
sentCh chan struct{}
}
func (m *mockEventSender) SendEventData(kind ldevents.EventDataKind, data []byte, eventCount int) ldevents.EventSenderResult {
m.sentCh <- struct{}{} // allows benchmarks to detect that the event payload has been generated and fake-sent
return ldevents.EventSenderResult{Success: true}
}
type eventsBenchmarkEnv struct {
eventProcessor ldevents.EventProcessor
mockEventSender *mockEventSender
targetFeatureKey string
users []lduser.User
variations []ldvalue.Value
}
func newEventsBenchmarkEnv() *eventsBenchmarkEnv {
return &eventsBenchmarkEnv{}
}
func (env *eventsBenchmarkEnv) setUp(bc eventsBenchmarkCase) {
env.mockEventSender = &mockEventSender{sentCh: make(chan struct{}, 10)}
config := ldevents.EventsConfiguration{
Capacity: bc.bufferSize,
EventSender: env.mockEventSender,
}
env.eventProcessor = ldevents.NewDefaultEventProcessor(config)
env.targetFeatureKey = "flag-key"
env.variations = make([]ldvalue.Value, bc.numVariations)
for i := 0; i < bc.numVariations; i++ {
env.variations[i] = ldvalue.Int(i)
}
env.users = make([]lduser.User, bc.numUsers)
for i := 0; i < bc.numUsers; i++ {
env.users[i] = lduser.NewUser(makeEvalBenchmarkTargetUserKey(i))
}
}
func (env *eventsBenchmarkEnv) tearDown() {
env.eventProcessor.Close()
env.eventProcessor = nil
}
func (env *eventsBenchmarkEnv) waitUntilEventsSent() {
<-env.mockEventSender.sentCh
}
type eventsBenchmarkCase struct {
bufferSize int
numEvents int
numVariations int
numUsers int
}
var eventsBenchmarkCases = []eventsBenchmarkCase{
eventsBenchmarkCase{
bufferSize: 1000,
numEvents: 100,
numVariations: 2,
numUsers: 10,
},
eventsBenchmarkCase{
bufferSize: 1000,
numEvents: 100,
numVariations: 2,
numUsers: 100,
},
eventsBenchmarkCase{
bufferSize: 1000,
numEvents: 1000,
numVariations: 2,
numUsers: 10,
},
eventsBenchmarkCase{
bufferSize: 1000,
numEvents: 1000,
numVariations: 2,
numUsers: 100,
},
}
func benchmarkEvents(b *testing.B, cases []eventsBenchmarkCase, action func(*eventsBenchmarkEnv, eventsBenchmarkCase)) {
env := newEventsBenchmarkEnv()
for _, bc := range cases {
env.setUp(bc)
b.Run(fmt.Sprintf("%+v", bc), func(b *testing.B) {
for i := 0; i < b.N; i++ {
action(env, bc)
}
})
env.tearDown()
}
}
func BenchmarkFeatureRequestEventsSummaryOnly(b *testing.B) {
benchmarkEvents(b, eventsBenchmarkCases, func(env *eventsBenchmarkEnv, bc eventsBenchmarkCase) {
for i := 0; i < bc.numEvents; i++ {
user := env.users[i%bc.numUsers]
variation := i % bc.numVariations
value := env.variations[variation]
event := ldevents.FeatureRequestEvent{
BaseEvent: ldevents.BaseEvent{
CreationDate: ldtime.UnixMillisNow(),
User: ldevents.EventUser{User: user},
},
Key: env.targetFeatureKey,
Variation: ldvalue.NewOptionalInt(variation),
Value: value,
}
env.eventProcessor.RecordFeatureRequestEvent(event)
}
env.eventProcessor.Flush()
env.waitUntilEventsSent()
})
}
func BenchmarkFeatureRequestEventsWithFullTracking(b *testing.B) {
benchmarkEvents(b, eventsBenchmarkCases, func(env *eventsBenchmarkEnv, bc eventsBenchmarkCase) {
for i := 0; i < bc.numEvents; i++ {
user := env.users[i%bc.numUsers]
variation := i % bc.numVariations
value := env.variations[variation]
event := ldevents.FeatureRequestEvent{
BaseEvent: ldevents.BaseEvent{
CreationDate: ldtime.UnixMillisNow(),
User: ldevents.EventUser{User: user},
},
Key: env.targetFeatureKey,
Variation: ldvalue.NewOptionalInt(variation),
Value: value,
TrackEvents: true,
}
env.eventProcessor.RecordFeatureRequestEvent(event)
}
env.eventProcessor.Flush()
env.waitUntilEventsSent()
})
}
func BenchmarkCustomEvents(b *testing.B) {
data := ldvalue.ObjectBuild().Set("eventData", ldvalue.String("value")).Build()
benchmarkEvents(b, eventsBenchmarkCases, func(env *eventsBenchmarkEnv, bc eventsBenchmarkCase) {
for i := 0; i < bc.numEvents; i++ {
user := env.users[i%bc.numUsers]
event := ldevents.CustomEvent{
BaseEvent: ldevents.BaseEvent{
CreationDate: ldtime.UnixMillisNow(),
User: ldevents.EventUser{User: user},
},
Key: "event-key",
Data: data,
}
env.eventProcessor.RecordCustomEvent(event)
}
env.eventProcessor.Flush()
env.waitUntilEventsSent()
})
}