lib/streamaggr: properly generate pushSample.key in benchmarks

This commit is contained in:
Aliaksandr Valialkin 2024-03-04 17:57:36 +02:00
parent 93a057e4e6
commit 9728aaf5d9
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 16 additions and 12 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" "github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils" "github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
@ -18,7 +19,7 @@ func BenchmarkDedupAggr(b *testing.B) {
} }
func BenchmarkDedupAggrFlushSerial(b *testing.B) { func BenchmarkDedupAggrFlushSerial(b *testing.B) {
as := newLastAggrState() as := newTotalAggrState(time.Hour, true, true)
benchSamples := newBenchSamples(100_000) benchSamples := newBenchSamples(100_000)
da := newDedupAggr() da := newDedupAggr()
@ -70,12 +71,16 @@ func newBenchSamples(count int) []pushSample {
Value: "process_cpu_seconds_total", Value: "process_cpu_seconds_total",
}, },
} }
labelsLen := len(labels)
samples := make([]pushSample, count) samples := make([]pushSample, count)
var keyBuf []byte var keyBuf []byte
for i := range samples { for i := range samples {
sample := &samples[i] sample := &samples[i]
labels[0].Value = fmt.Sprintf("host-%d", i) labels = append(labels[:labelsLen], prompbmarshal.Label{
keyBuf = lc.Compress(keyBuf[:0], labels) Name: "app",
Value: fmt.Sprintf("app-%d", i%10),
})
keyBuf = compressLabels(keyBuf[:0], &lc, labels[:labelsLen], labels[labelsLen:])
sample.key = string(keyBuf) sample.key = string(keyBuf)
sample.value = float64(i) sample.value = float64(i)
} }

View file

@ -759,7 +759,7 @@ func (a *aggregator) Push(tss []prompbmarshal.TimeSeries, matchIdxs []byte) {
outputLabels.Labels = append(outputLabels.Labels, labels.Labels...) outputLabels.Labels = append(outputLabels.Labels, labels.Labels...)
} }
buf = a.compressLabels(buf[:0], inputLabels.Labels, outputLabels.Labels) buf = compressLabels(buf[:0], &a.lc, inputLabels.Labels, outputLabels.Labels)
key := bytesutil.InternBytes(buf) key := bytesutil.InternBytes(buf)
for _, sample := range ts.Samples { for _, sample := range ts.Samples {
if math.IsNaN(sample.Value) { if math.IsNaN(sample.Value) {
@ -782,19 +782,18 @@ func (a *aggregator) Push(tss []prompbmarshal.TimeSeries, matchIdxs []byte) {
} }
} }
func (a *aggregator) compressLabels(dst []byte, inputLabels, outputLabels []prompbmarshal.Label) []byte { func compressLabels(dst []byte, lc *promutils.LabelsCompressor, inputLabels, outputLabels []prompbmarshal.Label) []byte {
bb := bbPool.Get() bb := bbPool.Get()
bb.B = a.lc.Compress(bb.B, inputLabels) bb.B = lc.Compress(bb.B, inputLabels)
dst = encoding.MarshalVarUint64(dst, uint64(len(bb.B))) dst = encoding.MarshalVarUint64(dst, uint64(len(bb.B)))
dst = append(dst, bb.B...) dst = append(dst, bb.B...)
bbPool.Put(bb) bbPool.Put(bb)
dst = a.lc.Compress(dst, outputLabels) dst = lc.Compress(dst, outputLabels)
return dst return dst
} }
func (a *aggregator) decompressLabels(dst []prompbmarshal.Label, key string) []prompbmarshal.Label { func decompressLabels(dst []prompbmarshal.Label, lc *promutils.LabelsCompressor, key string) []prompbmarshal.Label {
dst = a.lc.Decompress(dst, bytesutil.ToUnsafeBytes(key)) return lc.Decompress(dst, bytesutil.ToUnsafeBytes(key))
return dst
} }
func getOutputKey(key string) string { func getOutputKey(key string) string {
@ -966,7 +965,7 @@ func (ctx *flushCtx) flushSeries() {
func (ctx *flushCtx) appendSeries(key, suffix string, timestamp int64, value float64) { func (ctx *flushCtx) appendSeries(key, suffix string, timestamp int64, value float64) {
labelsLen := len(ctx.labels) labelsLen := len(ctx.labels)
samplesLen := len(ctx.samples) samplesLen := len(ctx.samples)
ctx.labels = ctx.a.decompressLabels(ctx.labels, key) ctx.labels = decompressLabels(ctx.labels, &ctx.a.lc, key)
if !ctx.a.keepMetricNames { if !ctx.a.keepMetricNames {
ctx.labels = addMetricSuffix(ctx.labels, labelsLen, ctx.a.suffix, suffix) ctx.labels = addMetricSuffix(ctx.labels, labelsLen, ctx.a.suffix, suffix)
} }
@ -989,7 +988,7 @@ func (ctx *flushCtx) appendSeries(key, suffix string, timestamp int64, value flo
func (ctx *flushCtx) appendSeriesWithExtraLabel(key, suffix string, timestamp int64, value float64, extraName, extraValue string) { func (ctx *flushCtx) appendSeriesWithExtraLabel(key, suffix string, timestamp int64, value float64, extraName, extraValue string) {
labelsLen := len(ctx.labels) labelsLen := len(ctx.labels)
samplesLen := len(ctx.samples) samplesLen := len(ctx.samples)
ctx.labels = ctx.a.decompressLabels(ctx.labels, key) ctx.labels = decompressLabels(ctx.labels, &ctx.a.lc, key)
if !ctx.a.keepMetricNames { if !ctx.a.keepMetricNames {
ctx.labels = addMetricSuffix(ctx.labels, labelsLen, ctx.a.suffix, suffix) ctx.labels = addMetricSuffix(ctx.labels, labelsLen, ctx.a.suffix, suffix)
} }