2023-01-04 07:19:18 +01:00
|
|
|
package streamaggr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-04 18:12:06 +01:00
|
|
|
"strconv"
|
2023-01-04 07:19:18 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2024-03-04 13:50:46 +01:00
|
|
|
"time"
|
2023-01-04 07:19:18 +01:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
|
|
|
|
)
|
|
|
|
|
2024-03-03 23:45:48 +01:00
|
|
|
var benchOutputs = []string{
|
|
|
|
"total",
|
|
|
|
"total_prometheus",
|
|
|
|
"increase",
|
|
|
|
"increase_prometheus",
|
2024-05-13 16:40:37 +02:00
|
|
|
"rate_sum",
|
|
|
|
"rate_avg",
|
2024-03-03 23:45:48 +01:00
|
|
|
"count_series",
|
|
|
|
"count_samples",
|
|
|
|
"unique_samples",
|
|
|
|
"sum_samples",
|
|
|
|
"last",
|
|
|
|
"min",
|
|
|
|
"max",
|
|
|
|
"avg",
|
|
|
|
"stddev",
|
|
|
|
"stdvar",
|
|
|
|
"histogram_bucket",
|
|
|
|
"quantiles(0, 0.5, 1)",
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAggregatorsPush(b *testing.B) {
|
|
|
|
for _, output := range benchOutputs {
|
2023-01-04 07:19:18 +01:00
|
|
|
b.Run(fmt.Sprintf("output=%s", output), func(b *testing.B) {
|
|
|
|
benchmarkAggregatorsPush(b, output)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 23:45:48 +01:00
|
|
|
func BenchmarkAggregatorsFlushSerial(b *testing.B) {
|
2024-03-04 18:12:06 +01:00
|
|
|
outputs := []string{
|
|
|
|
"total", "sum_samples", "count_samples", "min",
|
|
|
|
"max", "avg", "increase", "count_series",
|
|
|
|
"last", "stddev", "stdvar", "total_prometheus", "increase_prometheus",
|
2023-01-04 07:19:18 +01:00
|
|
|
}
|
2024-04-02 22:16:24 +02:00
|
|
|
pushFunc := func(_ []prompbmarshal.TimeSeries) {}
|
2024-03-04 18:12:06 +01:00
|
|
|
a := newBenchAggregators(outputs, pushFunc)
|
2023-01-04 07:19:18 +01:00
|
|
|
defer a.MustStop()
|
2024-03-04 18:12:06 +01:00
|
|
|
_ = a.Push(benchSeries, nil)
|
2023-01-04 07:19:18 +01:00
|
|
|
|
2024-03-04 18:12:06 +01:00
|
|
|
b.ResetTimer()
|
2024-03-03 23:45:48 +01:00
|
|
|
b.ReportAllocs()
|
2024-03-04 18:12:06 +01:00
|
|
|
b.SetBytes(int64(len(benchSeries) * len(outputs)))
|
2024-03-03 23:45:48 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, aggr := range a.as {
|
2024-03-04 18:12:06 +01:00
|
|
|
aggr.flush(pushFunc, time.Hour, false)
|
2024-03-03 23:45:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkAggregatorsPush(b *testing.B, output string) {
|
2024-04-02 22:16:24 +02:00
|
|
|
pushFunc := func(_ []prompbmarshal.TimeSeries) {}
|
2024-03-04 18:12:06 +01:00
|
|
|
a := newBenchAggregators([]string{output}, pushFunc)
|
2024-03-03 23:45:48 +01:00
|
|
|
defer a.MustStop()
|
|
|
|
|
|
|
|
const loops = 100
|
2024-02-29 01:39:00 +01:00
|
|
|
|
2024-03-04 18:12:06 +01:00
|
|
|
b.ResetTimer()
|
2023-01-04 07:19:18 +01:00
|
|
|
b.ReportAllocs()
|
2024-02-29 01:39:00 +01:00
|
|
|
b.SetBytes(int64(len(benchSeries) * loops))
|
2023-01-04 07:19:18 +01:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-07-25 01:44:09 +02:00
|
|
|
var matchIdxs []byte
|
2023-01-04 07:19:18 +01:00
|
|
|
for pb.Next() {
|
2024-02-29 01:39:00 +01:00
|
|
|
for i := 0; i < loops; i++ {
|
2024-03-03 23:45:48 +01:00
|
|
|
matchIdxs = a.Push(benchSeries, matchIdxs)
|
2024-02-29 01:39:00 +01:00
|
|
|
}
|
2023-01-04 07:19:18 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-04 18:12:06 +01:00
|
|
|
func newBenchAggregators(outputs []string, pushFunc PushFunc) *Aggregators {
|
|
|
|
outputsQuoted := make([]string, len(outputs))
|
|
|
|
for i := range outputs {
|
|
|
|
outputsQuoted[i] = strconv.Quote(outputs[i])
|
|
|
|
}
|
2024-03-03 23:45:48 +01:00
|
|
|
config := fmt.Sprintf(`
|
|
|
|
- match: http_requests_total
|
|
|
|
interval: 24h
|
2024-03-04 18:12:06 +01:00
|
|
|
by: [job]
|
|
|
|
outputs: [%s]
|
|
|
|
`, strings.Join(outputsQuoted, ","))
|
|
|
|
|
2024-03-04 04:42:55 +01:00
|
|
|
a, err := newAggregatorsFromData([]byte(config), pushFunc, nil)
|
2024-03-03 23:45:48 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("unexpected error when initializing aggregators: %s", err))
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2024-02-29 01:39:00 +01:00
|
|
|
func newBenchSeries(seriesCount int) []prompbmarshal.TimeSeries {
|
|
|
|
a := make([]string, seriesCount)
|
|
|
|
for j := 0; j < seriesCount; j++ {
|
2024-03-04 16:37:04 +01:00
|
|
|
s := fmt.Sprintf(`http_requests_total{path="/foo/%d",job="foo_%d",instance="bar",pod="pod-123232312",namespace="kube-foo-bar",node="node-123-3434-443",`+
|
2024-03-04 18:12:06 +01:00
|
|
|
`some_other_label="foo-bar-baz",environment="prod",label1="value1",label2="value2",label3="value3"} %d`, j, j%100, j*1000)
|
2024-02-29 01:39:00 +01:00
|
|
|
a = append(a, s)
|
2023-01-04 07:19:18 +01:00
|
|
|
}
|
|
|
|
metrics := strings.Join(a, "\n")
|
|
|
|
return mustParsePromMetrics(metrics)
|
|
|
|
}
|
|
|
|
|
2024-02-29 01:39:00 +01:00
|
|
|
const seriesCount = 10_000
|
2023-01-04 07:19:18 +01:00
|
|
|
|
2024-02-29 01:39:00 +01:00
|
|
|
var benchSeries = newBenchSeries(seriesCount)
|