Skip to content

Commit

Permalink
add benchmarks from Arthur
Browse files Browse the repository at this point in the history
  • Loading branch information
edma2 committed Nov 5, 2024
1 parent b0e078b commit 7806059
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package prometheusremotewriteexporter

import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 7806059

Please sign in to comment.