web/api: extend BenchmarkRespond with more types of data

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-07-13 15:36:38 +01:00
parent 657da2eb98
commit 54e1046616

View file

@ -3417,27 +3417,57 @@ func TestReturnAPIError(t *testing.T) {
var testResponseWriter = httptest.ResponseRecorder{} var testResponseWriter = httptest.ResponseRecorder{}
func BenchmarkRespond(b *testing.B) { func BenchmarkRespond(b *testing.B) {
b.ReportAllocs()
request, err := http.NewRequest(http.MethodGet, "/does-not-matter", nil)
require.NoError(b, err)
points := []promql.FPoint{} points := []promql.FPoint{}
for i := 0; i < 10000; i++ { for i := 0; i < 10000; i++ {
points = append(points, promql.FPoint{F: float64(i * 1000000), T: int64(i)}) points = append(points, promql.FPoint{F: float64(i * 1000000), T: int64(i)})
} }
response := &QueryData{ matrix := promql.Matrix{}
ResultType: parser.ValueTypeMatrix, for i := 0; i < 1000; i++ {
Result: promql.Matrix{ matrix = append(matrix, promql.Series{
promql.Series{ Metric: labels.FromStrings("__name__", fmt.Sprintf("series%v", i),
Floats: points, "label", fmt.Sprintf("series%v", i),
Metric: labels.EmptyLabels(), "label2", fmt.Sprintf("series%v", i)),
}, Floats: points[:10],
}, })
} }
b.ResetTimer() series := []labels.Labels{}
api := API{} for i := 0; i < 1000; i++ {
api.InstallCodec(JSONCodec{}) series = append(series, labels.FromStrings("__name__", fmt.Sprintf("series%v", i),
for n := 0; n < b.N; n++ { "label", fmt.Sprintf("series%v", i),
api.respond(&testResponseWriter, request, response, nil) "label2", fmt.Sprintf("series%v", i)))
}
cases := []struct {
name string
response interface{}
}{
{name: "10000 points no labels", response: &QueryData{
ResultType: parser.ValueTypeMatrix,
Result: promql.Matrix{
promql.Series{
Floats: points,
Metric: labels.EmptyLabels(),
},
},
}},
{name: "1000 labels", response: series},
{name: "1000 series 10 points", response: &QueryData{
ResultType: parser.ValueTypeMatrix,
Result: matrix,
}},
}
for _, c := range cases {
b.Run(c.name, func(b *testing.B) {
b.ReportAllocs()
request, err := http.NewRequest(http.MethodGet, "/does-not-matter", nil)
require.NoError(b, err)
b.ResetTimer()
api := API{}
api.InstallCodec(JSONCodec{})
for n := 0; n < b.N; n++ {
api.respond(&testResponseWriter, request, c.response, nil)
}
})
} }
} }