chore(parsers.xpath): Add benchmark for protocol-buffer format (#14312)

This commit is contained in:
Sven Rebhan 2023-11-16 17:05:04 +01:00 committed by GitHub
parent 33c4e76d91
commit e79191a9b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 5 deletions

View File

@ -1643,3 +1643,38 @@ func BenchmarkParsingJSON(b *testing.B) {
_, _ = plugin.Parse([]byte(benchmarkDataJSON))
}
}
func BenchmarkParsingProtobuf(b *testing.B) {
plugin := &Parser{
DefaultMetricName: "benchmark",
Format: "xpath_protobuf",
ProtobufMessageDef: "benchmark.proto",
ProtobufMessageType: "benchmark.BenchmarkData",
ProtobufImportPaths: []string{".", "./testcases/protobuf_benchmark"},
NativeTypes: true,
Configs: []Config{
{
Selection: "//data",
Timestamp: "timestamp",
TimestampFmt: "unix_ns",
Tags: map[string]string{
"source": "source",
"tags_sdkver": "tags_sdkver",
"tags_platform": "tags_platform",
},
Fields: map[string]string{
"value": "value",
},
},
},
Log: testutil.Logger{Name: "parsers.xpath", Quiet: true},
}
require.NoError(b, plugin.Init())
benchmarkData, err := os.ReadFile(filepath.Join("testcases", "protobuf_benchmark", "message.bin"))
require.NoError(b, err)
for n := 0; n < b.N; n++ {
_, _ = plugin.Parse(benchmarkData)
}
}

View File

@ -0,0 +1,15 @@
syntax = "proto3";
package benchmark;
message Entry {
string source = 1;
string tags_sdkver = 2;
string tags_platform = 3;
double value = 4;
uint64 timestamp = 5;
}
message BenchmarkData {
repeated Entry data = 1;
}

View File

@ -0,0 +1,2 @@
benchmark,source=myhost,tags_platform=python,tags_sdkver=3.11.5 value=5.0 1653643421000000000
benchmark,source=myhost,tags_platform=python,tags_sdkver=3.11.4 value=4.0 1653643421000000000

View File

@ -0,0 +1,24 @@
[[inputs.file]]
files = ["./testcases/protobuf_benchmark/message.bin"]
data_format = "xpath_protobuf"
xpath_protobuf_file = "benchmark.proto"
xpath_protobuf_type = "benchmark.BenchmarkData"
xpath_protobuf_import_paths = [".", "./testcases/protobuf_benchmark"]
xpath_native_types = true
[[inputs.file.xpath]]
metric_name = "'benchmark'"
metric_selection = "//data"
timestamp = "timestamp"
timestamp_format = "unix_ns"
[inputs.file.xpath.tags]
source = "source"
tags_sdkver = "tags_sdkver"
tags_platform = "tags_platform"
[inputs.file.xpath.fields]
value = "value"

View File

@ -10,7 +10,8 @@ var _ telegraf.Logger = &Logger{}
// Logger defines a logging structure for plugins.
type Logger struct {
Name string // Name is the plugin name, will be printed in the `[]`.
Name string // Name is the plugin name, will be printed in the `[]`.
Quiet bool
}
// Errorf logs an error message, patterned after log.Printf.
@ -25,12 +26,16 @@ func (l Logger) Error(args ...interface{}) {
// Debugf logs a debug message, patterned after log.Printf.
func (l Logger) Debugf(format string, args ...interface{}) {
log.Printf("D! ["+l.Name+"] "+format, args...)
if !l.Quiet {
log.Printf("D! ["+l.Name+"] "+format, args...)
}
}
// Debug logs a debug message, patterned after log.Print.
func (l Logger) Debug(args ...interface{}) {
log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...)
if !l.Quiet {
log.Print(append([]interface{}{"D! [" + l.Name + "] "}, args...)...)
}
}
// Warnf logs a warning message, patterned after log.Printf.
@ -45,10 +50,14 @@ func (l Logger) Warn(args ...interface{}) {
// Infof logs an information message, patterned after log.Printf.
func (l Logger) Infof(format string, args ...interface{}) {
log.Printf("I! ["+l.Name+"] "+format, args...)
if !l.Quiet {
log.Printf("I! ["+l.Name+"] "+format, args...)
}
}
// Info logs an information message, patterned after log.Print.
func (l Logger) Info(args ...interface{}) {
log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...)
if !l.Quiet {
log.Print(append([]interface{}{"I! [" + l.Name + "] "}, args...)...)
}
}