-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathinternal_benchmark_test.go
More file actions
307 lines (277 loc) · 8.71 KB
/
Copy pathinternal_benchmark_test.go
File metadata and controls
307 lines (277 loc) · 8.71 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
package posthog
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
json "github.com/goccy/go-json"
)
// BenchmarkJSONMarshalBatch benchmarks JSON serialization at various batch sizes
// Uses pre-serialized messages to match production behavior
func BenchmarkJSONMarshalBatch(b *testing.B) {
sizes := []int{1, 10, 50, 100, 250}
for _, size := range sizes {
b.Run(fmt.Sprintf("batch_%d", size), func(b *testing.B) {
// Pre-generate and pre-serialize messages (as in production)
msgs := make([]json.RawMessage, size)
for i := range msgs {
data, _ := json.Marshal(generateVariedCapture(i).APIfy())
msgs[i] = json.RawMessage(data)
}
payload := batch{ApiKey: "test", Messages: msgs}
b.ResetTimer()
for i := 0; i < b.N; i++ {
json.Marshal(payload)
}
})
}
}
// BenchmarkJSONMarshalBatchWithCardinality benchmarks JSON serialization with various property cardinalities
// Uses pre-serialized messages to match production behavior
func BenchmarkJSONMarshalBatchWithCardinality(b *testing.B) {
cardinalities := []struct {
name string
cardinality PropertyCardinality
}{
{"low_10-100_props", CardinalityLow},
{"medium_500-2000_props", CardinalityMedium},
{"high_2000-10000_props", CardinalityHigh},
}
batchSize := 10 // Use smaller batch for high cardinality to avoid excessive memory
for _, tc := range cardinalities {
b.Run(tc.name, func(b *testing.B) {
// Pre-generate and pre-serialize messages with specific cardinality
msgs := make([]json.RawMessage, batchSize)
for i := range msgs {
capture := Capture{
DistinctId: fmt.Sprintf("user_%d", i),
Event: "test_event",
Properties: generatePropertiesWithCardinality(i, tc.cardinality),
Groups: generateGroupsWithCardinality(i, tc.cardinality),
}
data, _ := json.Marshal(capture.APIfy())
msgs[i] = json.RawMessage(data)
}
payload := batch{ApiKey: "test", Messages: msgs}
b.ResetTimer()
for i := 0; i < b.N; i++ {
json.Marshal(payload)
}
})
}
}
// BenchmarkPrepareForSend_Cardinality benchmarks prepareForSend at various property cardinalities
// This validates that the combined APIfy + serialization isn't a bottleneck
func BenchmarkPrepareForSend_Cardinality(b *testing.B) {
cardinalities := []struct {
name string
cardinality PropertyCardinality
}{
{"low_10-100_props", CardinalityLow},
{"medium_500-2000_props", CardinalityMedium},
{"high_2000-10000_props", CardinalityHigh},
}
for _, tc := range cardinalities {
b.Run(tc.name, func(b *testing.B) {
// Pre-generate captures with this cardinality
captures := make([]Capture, 100)
for i := range captures {
captures[i] = Capture{
Type: "capture",
DistinctId: fmt.Sprintf("user_%d", i),
Event: "test_event",
Properties: generatePropertiesWithCardinality(i, tc.cardinality),
Groups: generateGroupsWithCardinality(i, tc.cardinality),
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
prepareForSend(captures[i%100])
}
})
}
}
// BenchmarkHTTPUpload benchmarks HTTP upload isolated from serialization
func BenchmarkHTTPUpload(b *testing.B) {
// Pre-serialize payloads
payloadSizes := []int{1024, 10240, 102400} // 1KB, 10KB, 100KB
for _, size := range payloadSizes {
b.Run(fmt.Sprintf("payload_%dKB", size/1024), func(b *testing.B) {
payload := make([]byte, size)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.WriteHeader(200)
}))
defer server.Close()
client := &http.Client{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
req, _ := http.NewRequest("POST", server.URL, bytes.NewReader(payload))
resp, _ := client.Do(req)
if resp != nil {
resp.Body.Close()
}
}
})
}
}
// BenchmarkHTTPUploadWithRealPayload benchmarks HTTP upload with realistic JSON payloads
func BenchmarkHTTPUploadWithRealPayload(b *testing.B) {
cardinalities := []struct {
name string
cardinality PropertyCardinality
}{
{"low_cardinality", CardinalityLow},
{"medium_cardinality", CardinalityMedium},
}
for _, tc := range cardinalities {
b.Run(tc.name, func(b *testing.B) {
// Pre-generate and pre-serialize messages (as in production)
msgs := make([]json.RawMessage, 10)
for i := range msgs {
capture := Capture{
DistinctId: fmt.Sprintf("user_%d", i),
Event: "test_event",
Properties: generatePropertiesWithCardinality(i, tc.cardinality),
}
data, _ := json.Marshal(capture.APIfy())
msgs[i] = json.RawMessage(data)
}
payload, _ := json.Marshal(batch{ApiKey: "test", Messages: msgs})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.Copy(io.Discard, r.Body)
w.WriteHeader(200)
}))
defer server.Close()
client := &http.Client{}
b.ResetTimer()
b.SetBytes(int64(len(payload)))
for i := 0; i < b.N; i++ {
req, _ := http.NewRequest("POST", server.URL, bytes.NewReader(payload))
req.Header.Set("Content-Type", "application/json")
resp, _ := client.Do(req)
if resp != nil {
resp.Body.Close()
}
}
})
}
}
// BenchmarkPrepareVsMarshaling compares the cost of prepareForSend vs actual marshaling
func BenchmarkPrepareVsMarshaling(b *testing.B) {
cardinalities := []struct {
name string
cardinality PropertyCardinality
}{
{"low", CardinalityLow},
{"medium", CardinalityMedium},
{"high", CardinalityHigh},
}
for _, tc := range cardinalities {
// Pre-generate capture
capture := Capture{
Type: "capture",
DistinctId: "user_1",
Event: "test_event",
Properties: generatePropertiesWithCardinality(42, tc.cardinality),
Groups: generateGroupsWithCardinality(42, tc.cardinality),
}
apiMsg := capture.APIfy()
b.Run(tc.name+"_prepare", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
prepareForSend(capture)
}
})
b.Run(tc.name+"_marshal", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
json.Marshal(apiMsg)
}
})
}
}
// BenchmarkPropertyIteration benchmarks the cost of iterating over properties
func BenchmarkPropertyIteration(b *testing.B) {
cardinalities := []struct {
name string
cardinality PropertyCardinality
}{
{"low", CardinalityLow},
{"medium", CardinalityMedium},
{"high", CardinalityHigh},
}
for _, tc := range cardinalities {
props := generatePropertiesWithCardinality(42, tc.cardinality)
b.Run(tc.name, func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
total := 0
for k, v := range props {
total += len(k)
_ = v // access value
}
}
})
}
}
// legacyBatch represents the old batch structure with []APIMessage for comparison benchmarks.
// In the old flow, messages were serialized together with the batch wrapper in a single Marshal call.
type legacyBatch struct {
ApiKey string `json:"api_key"`
HistoricalMigration bool `json:"historical_migration,omitempty"`
Messages []APIMessage `json:"batch"`
}
// BenchmarkOldVsNewSerializationFlow compares serialization approaches:
// - Old: APIfy all messages, then marshal entire batch (single Marshal call)
// - New: prepareForSend each message (marshal individually), then marshal batch wrapper
func BenchmarkOldVsNewSerializationFlow(b *testing.B) {
batchSizes := []int{10, 50, 100, 250}
cardinalities := []struct {
name string
cardinality PropertyCardinality
}{
{"low", CardinalityLow},
{"medium", CardinalityMedium},
}
for _, size := range batchSizes {
for _, tc := range cardinalities {
// Pre-generate captures
captures := make([]Capture, size)
for i := range captures {
captures[i] = Capture{
Type: "capture",
DistinctId: fmt.Sprintf("user_%d", i),
Event: "test_event",
Properties: generatePropertiesWithCardinality(i, tc.cardinality),
Groups: generateGroupsWithCardinality(i, tc.cardinality),
}
}
// Old flow: APIfy then marshal entire batch
b.Run(fmt.Sprintf("old_batch_%d_%s", size, tc.name), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
apiMsgs := make([]APIMessage, size)
for j, c := range captures {
apiMsgs[j] = c.APIfy()
}
json.Marshal(legacyBatch{ApiKey: "test", Messages: apiMsgs})
}
})
// New flow: prepareForSend (marshal individually) then marshal batch wrapper
b.Run(fmt.Sprintf("new_batch_%d_%s", size, tc.name), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
rawMsgs := make([]json.RawMessage, size)
for j, c := range captures {
data, _, _ := prepareForSend(c)
rawMsgs[j] = data
}
json.Marshal(batch{ApiKey: "test", Messages: rawMsgs})
}
})
}
}
}