Scraping: Add benchmark for protobuf format

Extract helper function textToProto().

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2024-08-20 20:12:02 +01:00 committed by bwplotka
parent 8f4557b0b1
commit b4ef38cfc8

View file

@ -1295,6 +1295,45 @@ func BenchmarkScrapeLoopAppendOM(b *testing.B) {
}
}
func BenchmarkScrapeLoopAppendProto(b *testing.B) {
ctx, sl := simpleTestScrapeLoop(b)
slApp := sl.appender(ctx)
// Construct a metrics string to parse
sb := bytes.Buffer{}
fmt.Fprintf(&sb, "type: GAUGE\n")
fmt.Fprintf(&sb, "help: \"metric_a help text\"\n")
fmt.Fprintf(&sb, "name: \"metric_a\"\n")
for i := 0; i < 100; i++ {
fmt.Fprintf(&sb, `metric: <
label: <
name: "foo"
value: "%d"
>
label: <
name: "bar"
value: "%d"
>
gauge: <
value: 1
>
>
`, i, i*100)
}
// From text to proto message.
pb := bytes.Buffer{}
require.NoError(b, textToProto(sb.String(), &pb))
ts := time.Time{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ts = ts.Add(time.Second)
_, _, _, _ = sl.append(slApp, pb.Bytes(), "application/vnd.google.protobuf", ts)
}
}
func TestSetOptionsHandlingStaleness(t *testing.T) {
s := teststorage.New(t, 600000)
defer s.Close()
@ -2454,18 +2493,7 @@ metric: <
buf := &bytes.Buffer{}
if test.contentType == "application/vnd.google.protobuf" {
// In case of protobuf, we have to create the binary representation.
pb := &dto.MetricFamily{}
// From text to proto message.
require.NoError(t, proto.UnmarshalText(test.scrapeText, pb))
// From proto message to binary protobuf.
protoBuf, err := proto.Marshal(pb)
require.NoError(t, err)
// Write first length, then binary protobuf.
varintBuf := binary.AppendUvarint(nil, uint64(len(protoBuf)))
buf.Write(varintBuf)
buf.Write(protoBuf)
require.NoError(t, textToProto(test.scrapeText, buf))
} else {
buf.WriteString(test.scrapeText)
}
@ -2480,6 +2508,26 @@ metric: <
}
}
func textToProto(text string, buf *bytes.Buffer) error {
// In case of protobuf, we have to create the binary representation.
pb := &dto.MetricFamily{}
// From text to proto message.
err := proto.UnmarshalText(text, pb)
if err != nil {
return err
}
// From proto message to binary protobuf.
protoBuf, err := proto.Marshal(pb)
if err != nil {
return err
}
// Write first length, then binary protobuf.
varintBuf := binary.AppendUvarint(nil, uint64(len(protoBuf)))
buf.Write(varintBuf)
buf.Write(protoBuf)
return nil
}
func TestScrapeLoopAppendExemplarSeries(t *testing.T) {
scrapeText := []string{`metric_total{n="1"} 1 # {t="1"} 1.0 10000
# EOF`, `metric_total{n="1"} 2 # {t="2"} 2.0 20000