Skip to content

Commit

Permalink
chore(parsers): Add additional parser benchmarks (#14276)
Browse files Browse the repository at this point in the history
(cherry picked from commit 6fd4168)
  • Loading branch information
powersj committed Nov 13, 2023
1 parent faf3f95 commit d8159f7
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 7 deletions.
60 changes: 60 additions & 0 deletions plugins/parsers/dropwizard/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,63 @@ func TestDropWizard(t *testing.T) {
})
}
}

const benchmarkData = `{
"version": "3.0.0",
"gauges" : {
"benchmark,tags_host=myhost,tags_sdkver=3.11.5,tags_platform=python": {
"value": 5.0
},
"benchmark,tags_host=myhost,tags_sdkver=3.11.4,tags_platform=python": {
"value": 4.0
}
}
}
`

func TestBenchmarkData(t *testing.T) {
plugin := &Parser{}
require.NoError(t, plugin.Init())

expected := []telegraf.Metric{
metric.New(
"benchmark",
map[string]string{
"metric_type": "gauge",
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.5",
},
map[string]interface{}{
"value": 5.0,
},
time.Unix(0, 0),
),
metric.New(
"benchmark",
map[string]string{
"metric_type": "gauge",
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.4",
},
map[string]interface{}{
"value": 4.0,
},
time.Unix(0, 0),
),
}

actual, err := plugin.Parse([]byte(benchmarkData))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}

func BenchmarkParsing(b *testing.B) {
plugin := &Parser{}
require.NoError(b, plugin.Init())

for n := 0; n < b.N; n++ {
_, _ = plugin.Parse([]byte(benchmarkData))
}
}
51 changes: 47 additions & 4 deletions plugins/parsers/form_urlencoded/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package form_urlencoded

import (
"testing"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -35,14 +39,14 @@ func TestParseLineValidFormData(t *testing.T) {
MetricName: "form_urlencoded_test",
}

metric, err := parser.ParseLine(validFormData)
metrics, err := parser.ParseLine(validFormData)
require.NoError(t, err)
require.Equal(t, "form_urlencoded_test", metric.Name())
require.Equal(t, map[string]string{}, metric.Tags())
require.Equal(t, "form_urlencoded_test", metrics.Name())
require.Equal(t, map[string]string{}, metrics.Tags())
require.Equal(t, map[string]interface{}{
"field1": float64(42),
"field2": float64(69),
}, metric.Fields())
}, metrics.Fields())
}

func TestParseValidFormDataWithTags(t *testing.T) {
Expand Down Expand Up @@ -170,3 +174,42 @@ func TestParseInvalidFormDataEmptyString(t *testing.T) {
require.NoError(t, err)
require.Empty(t, metrics)
}

const benchmarkData = `tags_host=myhost&tags_platform=python&tags_sdkver=3.11.5&value=5`

func TestBenchmarkData(t *testing.T) {
plugin := &Parser{
MetricName: "benchmark",
TagKeys: []string{"tags_host", "tags_platform", "tags_sdkver"},
}

expected := []telegraf.Metric{
metric.New(
"benchmark",
map[string]string{
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.5",
},
map[string]interface{}{
"value": 5.0,
},
time.Unix(0, 0),
),
}

actual, err := plugin.Parse([]byte(benchmarkData))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}

func BenchmarkParsing(b *testing.B) {
plugin := &Parser{
MetricName: "benchmark",
TagKeys: []string{"source", "tags_platform", "tags_sdkver"},
}

for n := 0; n < b.N; n++ {
_, _ = plugin.Parse([]byte(benchmarkData))
}
}
55 changes: 55 additions & 0 deletions plugins/parsers/grok/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,3 +1184,58 @@ func TestMultilineNilMetric(t *testing.T) {
require.NoError(t, err)
require.Empty(t, actual)
}

const benchmarkData = `benchmark 5 1653643421 source=myhost tags_platform=python tags_sdkver=3.11.5
benchmark 4 1653643422 source=myhost tags_platform=python tags_sdkver=3.11.4
`

func TestBenchmarkData(t *testing.T) {
plugin := &Parser{
//nolint:lll // conditionally long lines allowed
Patterns: []string{"%{WORD:measurement:measurement} %{NUMBER:value:float} %{NUMBER:timestamp:ts-epoch} source=%{WORD:source:tag} tags_platform=%{WORD:tags_platform:tag} tags_sdkver=%{GREEDYDATA:tags_sdkver:tag}"},
}
require.NoError(t, plugin.Init())

expected := []telegraf.Metric{
metric.New(
"benchmark",
map[string]string{
"source": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.5",
},
map[string]interface{}{
"value": 5.0,
},
time.Unix(1653643421, 0),
),
metric.New(
"benchmark",
map[string]string{
"source": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.4",
},
map[string]interface{}{
"value": 4.0,
},
time.Unix(1653643422, 0),
),
}

actual, err := plugin.Parse([]byte(benchmarkData))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, actual)
}

func BenchmarkParsing(b *testing.B) {
plugin := &Parser{
//nolint:lll // conditionally long lines allowed
Patterns: []string{"%{WORD:measurement:measurement} %{NUMBER:value:float} %{NUMBER:timestamp:ts-epoch} source=%{WORD:source:tag} tags_platform=%{WORD:tags_platform:tag} tags_sdkver=%{GREEDYDATA:tags_sdkver:tag}"},
}
require.NoError(b, plugin.Init())

for n := 0; n < b.N; n++ {
_, _ = plugin.Parse([]byte(benchmarkData))
}
}
54 changes: 54 additions & 0 deletions plugins/parsers/logfmt/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
)

Expand Down Expand Up @@ -282,3 +283,56 @@ func TestTags(t *testing.T) {
})
}
}

const benchmarkData = `tags_host=myhost tags_platform=python tags_sdkver=3.11.5 value=5
tags_host=myhost tags_platform=python tags_sdkver=3.11.4 value=4
`

func TestBenchmarkData(t *testing.T) {
plugin := &Parser{
TagKeys: []string{"tags_host", "tags_platform", "tags_sdkver"},
}
require.NoError(t, plugin.Init())

expected := []telegraf.Metric{
metric.New(
"",
map[string]string{
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.5",
},
map[string]interface{}{
"value": 5,
},
time.Unix(0, 0),
),
metric.New(
"",
map[string]string{
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.4",
},
map[string]interface{}{
"value": 4,
},
time.Unix(0, 0),
),
}

actual, err := plugin.Parse([]byte(benchmarkData))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}

func BenchmarkParsing(b *testing.B) {
plugin := &Parser{
TagKeys: []string{"tags_host", "tags_platform", "tags_sdkver"},
}
require.NoError(b, plugin.Init())

for n := 0; n < b.N; n++ {
_, _ = plugin.Parse([]byte(benchmarkData))
}
}
50 changes: 50 additions & 0 deletions plugins/parsers/nagios/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,53 @@ func TestParseThreshold(t *testing.T) {
require.Equal(t, tests[i].eErr, err)
}
}

const benchmarkData = `DISK OK - free space: / 3326 MB (56%); | /=2643MB;5948;5958;0;5968
/ 15272 MB (77%);
/boot 68 MB (69%);
`

func TestBenchmarkData(t *testing.T) {
plugin := &Parser{}

expected := []telegraf.Metric{
metric.New(
"nagios",
map[string]string{
"perfdata": "/",
"unit": "MB",
},
map[string]interface{}{
"critical_gt": 5958.0,
"critical_lt": 0.0,
"min": 0.0,
"max": 5968.0,
"value": 2643.0,
"warning_gt": 5948.0,
"warning_lt": 0.0,
},
time.Unix(0, 0),
),
metric.New(
"nagios_state",
map[string]string{},
map[string]interface{}{
"long_service_output": "/ 15272 MB (77%);\n/boot 68 MB (69%);",
"service_output": "DISK OK - free space: / 3326 MB (56%);",
},
time.Unix(0, 0),
),
}

actual, err := plugin.Parse([]byte(benchmarkData))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, actual, testutil.IgnoreTime())
}

func BenchmarkParsing(b *testing.B) {
plugin := &Parser{}

for n := 0; n < b.N; n++ {
_, _ = plugin.Parse([]byte(benchmarkData))
}
}
48 changes: 48 additions & 0 deletions plugins/parsers/opentsdb/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -306,3 +307,50 @@ func TestParse_DefaultTags(t *testing.T) {
})
}
}

const benchmarkData = `put benchmark_a 1653643420 4 tags_host=myhost tags_platform=python tags_sdkver=3.11.4
put benchmark_b 1653643420 5 tags_host=myhost tags_platform=python tags_sdkver=3.11.5
`

func TestBenchmarkData(t *testing.T) {
plugin := &Parser{}

expected := []telegraf.Metric{
metric.New(
"benchmark_a",
map[string]string{
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.4",
},
map[string]interface{}{
"value": 4.0,
},
time.Unix(1653643420, 0),
),
metric.New(
"benchmark_b",
map[string]string{
"tags_host": "myhost",
"tags_platform": "python",
"tags_sdkver": "3.11.5",
},
map[string]interface{}{
"value": 5.0,
},
time.Unix(1653643420, 0),
),
}

actual, err := plugin.Parse([]byte(benchmarkData))
require.NoError(t, err)
testutil.RequireMetricsEqual(t, expected, actual)
}

func BenchmarkParsing(b *testing.B) {
plugin := &Parser{}

for n := 0; n < b.N; n++ {
_, _ = plugin.Parse([]byte(benchmarkData))
}
}
Loading

0 comments on commit d8159f7

Please sign in to comment.