From e79191a9b4836a200f6c0c6dcb49af710cc4023b Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Thu, 16 Nov 2023 17:05:04 +0100 Subject: [PATCH] chore(parsers.xpath): Add benchmark for protocol-buffer format (#14312) --- plugins/parsers/xpath/parser_test.go | 35 ++++++++++++++++++ .../protobuf_benchmark/benchmark.proto | 15 ++++++++ .../testcases/protobuf_benchmark/expected.out | 2 + .../testcases/protobuf_benchmark/message.bin | Bin 0 -> 90 bytes .../protobuf_benchmark/telegraf.conf | 24 ++++++++++++ testutil/log.go | 19 +++++++--- 6 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 plugins/parsers/xpath/testcases/protobuf_benchmark/benchmark.proto create mode 100644 plugins/parsers/xpath/testcases/protobuf_benchmark/expected.out create mode 100644 plugins/parsers/xpath/testcases/protobuf_benchmark/message.bin create mode 100644 plugins/parsers/xpath/testcases/protobuf_benchmark/telegraf.conf diff --git a/plugins/parsers/xpath/parser_test.go b/plugins/parsers/xpath/parser_test.go index a9c0b01a7..7a7bbdad2 100644 --- a/plugins/parsers/xpath/parser_test.go +++ b/plugins/parsers/xpath/parser_test.go @@ -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) + } +} diff --git a/plugins/parsers/xpath/testcases/protobuf_benchmark/benchmark.proto b/plugins/parsers/xpath/testcases/protobuf_benchmark/benchmark.proto new file mode 100644 index 000000000..74e0f307a --- /dev/null +++ b/plugins/parsers/xpath/testcases/protobuf_benchmark/benchmark.proto @@ -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; +} diff --git a/plugins/parsers/xpath/testcases/protobuf_benchmark/expected.out b/plugins/parsers/xpath/testcases/protobuf_benchmark/expected.out new file mode 100644 index 000000000..8263f7ceb --- /dev/null +++ b/plugins/parsers/xpath/testcases/protobuf_benchmark/expected.out @@ -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 diff --git a/plugins/parsers/xpath/testcases/protobuf_benchmark/message.bin b/plugins/parsers/xpath/testcases/protobuf_benchmark/message.bin new file mode 100644 index 0000000000000000000000000000000000000000..432e9e3607b30695f2542f4f31954fa0fad51026 GIT binary patch literal 90 zcmdjO%*Zb;5n?meGc?pQm0~NXEXl~vQ)B=G5eJQiBj49O{JZO?7#F%?6EwvF GNQwb^TpTC> literal 0 HcmV?d00001 diff --git a/plugins/parsers/xpath/testcases/protobuf_benchmark/telegraf.conf b/plugins/parsers/xpath/testcases/protobuf_benchmark/telegraf.conf new file mode 100644 index 000000000..405975dea --- /dev/null +++ b/plugins/parsers/xpath/testcases/protobuf_benchmark/telegraf.conf @@ -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" diff --git a/testutil/log.go b/testutil/log.go index c81370e23..65fb8a882 100644 --- a/testutil/log.go +++ b/testutil/log.go @@ -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...)...) + } }