Skip to content

Commit 79e6a7f

Browse files
committed
Add OTLP write handler benchmark
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent 006cab3 commit 79e6a7f

File tree

1 file changed

+122
-2
lines changed

1 file changed

+122
-2
lines changed

pkg/util/push/otlp_test.go

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,133 @@ func TestOTLPConvertToPromTS(t *testing.T) {
220220
}
221221
}
222222

223+
func BenchmarkOTLPWriteHandler(b *testing.B) {
224+
cfg := distributor.OTLPConfig{
225+
ConvertAllAttributes: false,
226+
DisableTargetInfo: false,
227+
}
228+
overrides, err := validation.NewOverrides(querier.DefaultLimitsConfig(), nil)
229+
require.NoError(b, err)
230+
231+
exportRequest := generateOTLPWriteRequest()
232+
mockPushFunc := func(context.Context, *cortexpb.WriteRequest) (*cortexpb.WriteResponse, error) {
233+
return &cortexpb.WriteResponse{}, nil
234+
}
235+
handler := OTLPHandler(10000, overrides, cfg, nil, mockPushFunc)
236+
237+
b.Run("json with no compression", func(b *testing.B) {
238+
ctx := context.Background()
239+
ctx = user.InjectOrgID(ctx, "user-1")
240+
body, err := exportRequest.MarshalJSON()
241+
require.NoError(b, err)
242+
buf := bytes.NewBuffer(body) // To reuse body
243+
244+
req, err := http.NewRequestWithContext(ctx, "", "", bytes.NewReader(buf.Bytes()))
245+
require.NoError(b, err)
246+
req.Header.Set("Content-Type", jsonContentType)
247+
248+
b.ResetTimer()
249+
b.ReportAllocs()
250+
for i := 0; i < b.N; i++ {
251+
recorder := httptest.NewRecorder()
252+
handler.ServeHTTP(recorder, req)
253+
254+
resp := recorder.Result()
255+
require.Equal(b, http.StatusOK, resp.StatusCode)
256+
req.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
257+
}
258+
})
259+
b.Run("json with gzip", func(b *testing.B) {
260+
ctx := context.Background()
261+
ctx = user.InjectOrgID(ctx, "user-1")
262+
body, err := exportRequest.MarshalJSON()
263+
require.NoError(b, err)
264+
265+
var gzipBody bytes.Buffer
266+
gz := gzip.NewWriter(&gzipBody)
267+
_, err = gz.Write(body)
268+
require.NoError(b, err)
269+
require.NoError(b, gz.Close())
270+
buf := bytes.NewBuffer(gzipBody.Bytes()) // To reuse body
271+
272+
req, err := http.NewRequestWithContext(ctx, "", "", bytes.NewReader(buf.Bytes()))
273+
require.NoError(b, err)
274+
req.Header.Set("Content-Type", jsonContentType)
275+
req.Header.Set("Content-Encoding", "gzip")
276+
277+
b.ResetTimer()
278+
b.ReportAllocs()
279+
for i := 0; i < b.N; i++ {
280+
recorder := httptest.NewRecorder()
281+
handler.ServeHTTP(recorder, req)
282+
283+
resp := recorder.Result()
284+
require.Equal(b, http.StatusOK, resp.StatusCode)
285+
req.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
286+
}
287+
})
288+
b.Run("proto with no compression", func(b *testing.B) {
289+
ctx := context.Background()
290+
ctx = user.InjectOrgID(ctx, "user-1")
291+
body, err := exportRequest.MarshalProto()
292+
require.NoError(b, err)
293+
buf := bytes.NewBuffer(body) // To reuse body
294+
295+
req, err := http.NewRequestWithContext(ctx, "", "", bytes.NewReader(buf.Bytes()))
296+
require.NoError(b, err)
297+
req.Header.Set("Content-Type", pbContentType)
298+
299+
b.ResetTimer()
300+
b.ReportAllocs()
301+
for i := 0; i < b.N; i++ {
302+
303+
recorder := httptest.NewRecorder()
304+
handler.ServeHTTP(recorder, req)
305+
306+
resp := recorder.Result()
307+
require.Equal(b, http.StatusOK, resp.StatusCode)
308+
req.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
309+
}
310+
})
311+
b.Run("proto with gzip", func(b *testing.B) {
312+
ctx := context.Background()
313+
ctx = user.InjectOrgID(ctx, "user-1")
314+
body, err := exportRequest.MarshalProto()
315+
require.NoError(b, err)
316+
317+
var gzipBody bytes.Buffer
318+
gz := gzip.NewWriter(&gzipBody)
319+
_, err = gz.Write(body)
320+
require.NoError(b, err)
321+
require.NoError(b, gz.Close())
322+
buf := bytes.NewBuffer(gzipBody.Bytes()) // To reuse body
323+
324+
req, err := http.NewRequestWithContext(ctx, "", "", bytes.NewReader(buf.Bytes()))
325+
require.NoError(b, err)
326+
req.Header.Set("Content-Type", pbContentType)
327+
req.Header.Set("Content-Encoding", "gzip")
328+
329+
b.ResetTimer()
330+
b.ReportAllocs()
331+
for i := 0; i < b.N; i++ {
332+
333+
recorder := httptest.NewRecorder()
334+
handler.ServeHTTP(recorder, req)
335+
336+
resp := recorder.Result()
337+
require.Equal(b, http.StatusOK, resp.StatusCode)
338+
req.Body = io.NopCloser(bytes.NewReader(buf.Bytes()))
339+
}
340+
})
341+
}
342+
223343
func TestOTLPWriteHandler(t *testing.T) {
224344
cfg := distributor.OTLPConfig{
225345
ConvertAllAttributes: false,
226346
DisableTargetInfo: false,
227347
}
228348

229-
exportRequest := generateOTLPWriteRequest(t)
349+
exportRequest := generateOTLPWriteRequest()
230350

231351
tests := []struct {
232352
description string
@@ -368,7 +488,7 @@ func TestOTLPWriteHandler(t *testing.T) {
368488
}
369489
}
370490

371-
func generateOTLPWriteRequest(t *testing.T) pmetricotlp.ExportRequest {
491+
func generateOTLPWriteRequest() pmetricotlp.ExportRequest {
372492
d := pmetric.NewMetrics()
373493

374494
// Generate One Counter, One Gauge, One Histogram, One Exponential-Histogram

0 commit comments

Comments
 (0)