From 780605936013ed6d9d0b0063c160bce0eea2f411 Mon Sep 17 00:00:00 2001 From: Eugene Ma Date: Mon, 4 Nov 2024 17:20:54 -0800 Subject: [PATCH] add benchmarks from Arthur --- .../exporter_test.go | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/exporter/prometheusremotewriteexporter/exporter_test.go b/exporter/prometheusremotewriteexporter/exporter_test.go index 87ead36d4ad7..d8647057a8f3 100644 --- a/exporter/prometheusremotewriteexporter/exporter_test.go +++ b/exporter/prometheusremotewriteexporter/exporter_test.go @@ -5,10 +5,13 @@ package prometheusremotewriteexporter import ( "context" + "fmt" "io" "net/http" "net/http/httptest" "net/url" + "strconv" + "strings" "sync" "testing" "time" @@ -1177,3 +1180,60 @@ func TestRetries(t *testing.T) { }) } } + +func BenchmarkExecute(b *testing.B) { + for _, numSample := range []int{100, 1000, 10000} { + b.Run(fmt.Sprintf("numSample=%d", numSample), func(b *testing.B) { + benchmarkExecute(b, numSample) + }) + } +} + +func benchmarkExecute(b *testing.B, numSample int) { + mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + })) + defer mockServer.Close() + endpointURL, err := url.Parse(mockServer.URL) + require.NoError(b, err) + + // Create the prwExporter + exporter := &prwExporter{ + endpointURL: endpointURL, + client: http.DefaultClient, + } + + generateSamples := func(n int) []prompb.Sample { + samples := make([]prompb.Sample, 0, n) + for i := 0; i < n; i++ { + samples = append(samples, prompb.Sample{ + Timestamp: int64(i), + Value: float64(i), + }) + } + return samples + } + + reqs := make([]*prompb.WriteRequest, 0, b.N) + const labelValue = "abcdefg'hijlmn234!@#$%^&*()_+~`\"{}[],./<>?hello0123hiOlá你好Dzieńdobry9Zd8ra765v4stvuyte" + for n := 0; n < b.N; n++ { + num := strings.Repeat(strconv.Itoa(n), 16) + req := &prompb.WriteRequest{ + Timeseries: []prompb.TimeSeries{{ + Samples: generateSamples(numSample), + Labels: []prompb.Label{ + {Name: "__name__", Value: "test_metric"}, + {Name: "test_label_name_" + num, Value: labelValue + num}, + }}}, + } + reqs = append(reqs, req) + } + + ctx := context.Background() + b.ReportAllocs() + b.ResetTimer() + for _, req := range reqs { + err := exporter.execute(ctx, req) + require.NoError(b, err) + } +}