diff --git a/Makefile.common b/Makefile.common index eaee9f0..528d404 100644 --- a/Makefile.common +++ b/Makefile.common @@ -66,6 +66,11 @@ test: @echo ">> running all tests" $(GO) test -race $(pkgs) +.PHONY: bench +bench: + @echo ">> running all benchmarks" + $(GO) test -bench . -race $(pkgs) + .PHONY: format format: @echo ">> formatting code" diff --git a/pkg/mapper/mapper_benchmark_test.go b/pkg/mapper/mapper_benchmark_test.go new file mode 100644 index 0000000..1511ee2 --- /dev/null +++ b/pkg/mapper/mapper_benchmark_test.go @@ -0,0 +1,797 @@ +// Copyright 2013 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mapper + +import ( + "fmt" + "testing" +) + +var ( + ruleTemplateSingleMatchGlob = ` +- match: metric%d.* + name: "metric_single" + labels: + name: "$1" +` + ruleTemplateSingleMatchRegex = ` +- match: metric%d\.([^.]*) + name: "metric_single" + labels: + name: "$1" +` + + ruleTemplateMultipleMatchGlob = ` +- match: metric%d.*.*.*.*.*.*.*.*.*.*.*.* + name: "metric_multi" + labels: + name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" +` + + ruleTemplateMultipleMatchRegex = ` +- match: metric%d\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) + name: "metric_multi" + labels: + name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" +` +) + +func duplicateRules(count int, template string) string { + rules := "" + for i := 0; i < count; i++ { + rules += fmt.Sprintf(template, i) + } + return rules +} + +func BenchmarkGlob(b *testing.B) { + config := `--- +mappings: +- match: test.dispatcher.*.*.succeeded + name: "dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "succeeded" + job: "test_dispatcher" +- match: test.my-dispatch-host01.name.dispatcher.*.*.* + name: "host_dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "$3" + job: "test_dispatcher" +- match: request_time.*.*.*.*.*.*.*.*.*.*.*.* + name: "tyk_http_request" + labels: + method_and_path: "${1}" + response_code: "${2}" + apikey: "${3}" + apiversion: "${4}" + apiname: "${5}" + apiid: "${6}" + ipv4_t1: "${7}" + ipv4_t2: "${8}" + ipv4_t3: "${9}" + ipv4_t4: "${10}" + orgid: "${11}" + oauthid: "${12}" +- match: "*.*" + name: "catchall" + labels: + first: "$1" + second: "$2" + third: "$3" + job: "-" + ` + mappings := []string{ + "test.dispatcher.FooProcessor.send.succeeded", + "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded", + "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.", + "foo.bar", + "foo.bar.baz", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlobNoOrdering(b *testing.B) { + config := `--- +defaults: + glob_disable_ordering: true +mappings: +- match: test.dispatcher.*.*.succeeded + name: "dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "succeeded" + job: "test_dispatcher" +- match: test.my-dispatch-host01.name.dispatcher.*.*.* + name: "host_dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "$3" + job: "test_dispatcher" +- match: request_time.*.*.*.*.*.*.*.*.*.*.*.* + name: "tyk_http_request" + labels: + method_and_path: "${1}" + response_code: "${2}" + apikey: "${3}" + apiversion: "${4}" + apiname: "${5}" + apiid: "${6}" + ipv4_t1: "${7}" + ipv4_t2: "${8}" + ipv4_t3: "${9}" + ipv4_t4: "${10}" + orgid: "${11}" + oauthid: "${12}" +- match: "*.*" + name: "catchall" + labels: + first: "$1" + second: "$2" + third: "$3" + job: "-" + ` + mappings := []string{ + "test.dispatcher.FooProcessor.send.succeeded", + "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded", + "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.", + "foo.bar", + "foo.bar.baz", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlobNoOrderingWithBacktracking(b *testing.B) { + config := `--- +defaults: + glob_disable_ordering: true +mappings: +- match: test.dispatcher.*.*.succeeded + name: "dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "succeeded" + job: "test_dispatcher" +- match: test.dispatcher.*.received.* + name: "dispatch_events_wont_match" + labels: + processor: "$1" + action: "received" + result: "$2" + job: "test_dispatcher" +- match: test.my-dispatch-host01.name.dispatcher.*.*.* + name: "host_dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "$3" + job: "test_dispatcher" +- match: request_time.*.*.*.*.*.*.*.*.*.*.*.* + name: "tyk_http_request" + labels: + method_and_path: "${1}" + response_code: "${2}" + apikey: "${3}" + apiversion: "${4}" + apiname: "${5}" + apiid: "${6}" + ipv4_t1: "${7}" + ipv4_t2: "${8}" + ipv4_t3: "${9}" + ipv4_t4: "${10}" + orgid: "${11}" + oauthid: "${12}" +- match: "*.*" + name: "catchall" + labels: + first: "$1" + second: "$2" + third: "$3" + job: "-" + ` + mappings := []string{ + "test.dispatcher.FooProcessor.send.succeeded", + "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded", + "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.", + "foo.bar", + "foo.bar.baz", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegex(b *testing.B) { + config := `--- +defaults: + match_type: regex +mappings: +- match: test\.dispatcher\.([^.]*)\.([^.]*)\.([^.]*) + name: "dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "$3" + job: "test_dispatcher" +- match: test.my-dispatch-host01.name.dispatcher\.([^.]*)\.([^.]*)\.([^.]*) + name: "host_dispatch_events" + labels: + processor: "$1" + action: "$2" + result: "$3" + job: "test_dispatcher" +- match: request_time\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) + name: "tyk_http_request" + labels: + method_and_path: "${1}" + response_code: "${2}" + apikey: "${3}" + apiversion: "${4}" + apiname: "${5}" + apiid: "${6}" + ipv4_t1: "${7}" + ipv4_t2: "${8}" + ipv4_t3: "${9}" + ipv4_t4: "${10}" + orgid: "${11}" + oauthid: "${12}" +- match: \.([^.]*)\.([^.]*) + name: "catchall" + labels: + first: "$1" + second: "$2" + third: "$3" + job: "-" + ` + mappings := []string{ + "test.dispatcher.FooProcessor.send.succeeded", + "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded", + "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.", + "foo.bar", + "foo.bar.baz", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlobSingleMatch(b *testing.B) { + config := `--- +mappings: +- match: metric.* + name: "metric_one" + labels: + name: "$1" + ` + mappings := []string{ + "metric.aaa", + "metric.bbb", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegexSingleMatch(b *testing.B) { + config := `--- +mappings: +- match: metric\.([^.]*) + name: "metric_one" + match_type: regex + labels: + name: "$1" + ` + mappings := []string{ + "metric.aaa", + "metric.bbb", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlobMultipleCaptures(b *testing.B) { + config := `--- +mappings: +- match: metric.*.*.*.*.*.*.*.*.*.*.*.* + name: "metric_multi" + labels: + name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" + ` + mappings := []string{ + "metric.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegexMultipleCaptures(b *testing.B) { + config := `--- +mappings: +- match: metric\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) + name: "metric_multi" + match_type: regex + labels: + name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" + ` + mappings := []string{ + "metric.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlobMultipleCapturesNoFormat(b *testing.B) { + config := `--- +mappings: +- match: metric.*.*.*.*.*.*.*.*.*.*.*.* + name: "metric_multi" + labels: + name: "not_relevant" + ` + mappings := []string{ + "metric.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegexMultipleCapturesNoFormat(b *testing.B) { + config := `--- +mappings: +- match: metric\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) + name: "metric_multi" + match_type: regex + labels: + name: "not_relevant" + ` + mappings := []string{ + "metric.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlobMultipleCapturesDifferentLabels(b *testing.B) { + config := `--- +mappings: +- match: metric.*.*.*.*.*.*.*.*.*.*.*.* + name: "metric_multi" + labels: + label1: "$1" + label2: "$2" + label3: "$3" + label4: "$4" + label5: "$5" + label6: "$6" + label7: "$7" + label8: "$8" + label9: "$9" + label10: "$10" + label11: "$11" + label12: "$12" + ` + mappings := []string{ + "metric.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegexMultipleCapturesDifferentLabels(b *testing.B) { + config := `--- +mappings: +- match: metric\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) + name: "metric_multi" + match_type: regex + labels: + label1: "$1" + label2: "$2" + label3: "$3" + label4: "$4" + label5: "$5" + label6: "$6" + label7: "$7" + label8: "$8" + label9: "$9" + label10: "$10" + label11: "$11" + label12: "$12" + ` + mappings := []string{ + "metric.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlob10Rules(b *testing.B) { + config := `--- +mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) + mappings := []string{ + "metric100.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegex10RulesAverage(b *testing.B) { + config := `--- +defaults: + match_type: regex +mappings:` + duplicateRules(10, ruleTemplateSingleMatchRegex) + mappings := []string{ + "metric5.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlob100Rules(b *testing.B) { + config := `--- +mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) + mappings := []string{ + "metric100.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlob100RulesNoMatch(b *testing.B) { + config := `--- +mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) + mappings := []string{ + "metricnomatchy.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlob100RulesNoOrderingNoMatch(b *testing.B) { + config := `--- +defaults: + glob_disable_ordering: true +mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) + mappings := []string{ + "metricnomatchy.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegex100RulesAverage(b *testing.B) { + config := `--- +defaults: + match_type: regex +mappings:` + duplicateRules(100, ruleTemplateSingleMatchRegex) + mappings := []string{ + "metric50.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegex100RulesWorst(b *testing.B) { + config := `--- +defaults: + match_type: regex +mappings:` + duplicateRules(100, ruleTemplateSingleMatchRegex) + mappings := []string{ + "metric100.a", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkGlob100RulesMultipleCaptures(b *testing.B) { + config := `--- +mappings:` + duplicateRules(100, ruleTemplateMultipleMatchGlob) + mappings := []string{ + "metric50.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegex100RulesMultipleCapturesAverage(b *testing.B) { + config := `--- +defaults: + match_type: regex +mappings:` + duplicateRules(100, ruleTemplateMultipleMatchRegex) + mappings := []string{ + "metric50.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} + +func BenchmarkRegex100RulesMultipleCapturesWorst(b *testing.B) { + config := `--- +defaults: + match_type: regex +mappings:` + duplicateRules(100, ruleTemplateMultipleMatchRegex) + mappings := []string{ + "metric100.a.b.c.d.e.f.g.h.i.j.k.l", + } + + mapper := MetricMapper{} + err := mapper.InitFromYAMLString(config) + if err != nil { + b.Fatalf("Config load error: %s %s", config, err) + } + + var dummyMetricType MetricType + b.ResetTimer() + for j := 0; j < b.N; j++ { + for _, metric := range mappings { + mapper.GetMapping(metric, dummyMetricType) + } + } +} diff --git a/pkg/mapper/mapper_test.go b/pkg/mapper/mapper_test.go index ace47e4..2a66259 100644 --- a/pkg/mapper/mapper_test.go +++ b/pkg/mapper/mapper_test.go @@ -14,7 +14,6 @@ package mapper import ( - "fmt" "testing" ) @@ -25,35 +24,6 @@ type mappings map[string]struct { notPresent bool } -var ( - ruleTemplateSingleMatchGlob = ` -- match: metric%d.* - name: "metric_single" - labels: - name: "$1" -` - ruleTemplateSingleMatchRegex = ` -- match: metric%d\.([^.]*) - name: "metric_single" - labels: - name: "$1" -` - - ruleTemplateMultipleMatchGlob = ` -- match: metric%d.*.*.*.*.*.*.*.*.*.*.*.* - name: "metric_multi" - labels: - name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" -` - - ruleTemplateMultipleMatchRegex = ` -- match: metric%d\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) - name: "metric_multi" - labels: - name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" -` -) - func TestMetricMapperYAML(t *testing.T) { scenarios := []struct { config string @@ -636,786 +606,6 @@ mappings: } } -func duplicateRules(count int, template string) string { - rules := "" - for i := 0; i < count; i++ { - rules += fmt.Sprintf(template, i) - } - return rules -} -func BenchmarkGlob(b *testing.B) { - config := `--- -mappings: -- match: test.dispatcher.*.*.succeeded - name: "dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "succeeded" - job: "test_dispatcher" -- match: test.my-dispatch-host01.name.dispatcher.*.*.* - name: "host_dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "$3" - job: "test_dispatcher" -- match: request_time.*.*.*.*.*.*.*.*.*.*.*.* - name: "tyk_http_request" - labels: - method_and_path: "${1}" - response_code: "${2}" - apikey: "${3}" - apiversion: "${4}" - apiname: "${5}" - apiid: "${6}" - ipv4_t1: "${7}" - ipv4_t2: "${8}" - ipv4_t3: "${9}" - ipv4_t4: "${10}" - orgid: "${11}" - oauthid: "${12}" -- match: "*.*" - name: "catchall" - labels: - first: "$1" - second: "$2" - third: "$3" - job: "-" - ` - mappings := mappings{ - "test.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar.baz": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlobNoOrdering(b *testing.B) { - config := `--- -defaults: - glob_disable_ordering: true -mappings: -- match: test.dispatcher.*.*.succeeded - name: "dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "succeeded" - job: "test_dispatcher" -- match: test.my-dispatch-host01.name.dispatcher.*.*.* - name: "host_dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "$3" - job: "test_dispatcher" -- match: request_time.*.*.*.*.*.*.*.*.*.*.*.* - name: "tyk_http_request" - labels: - method_and_path: "${1}" - response_code: "${2}" - apikey: "${3}" - apiversion: "${4}" - apiname: "${5}" - apiid: "${6}" - ipv4_t1: "${7}" - ipv4_t2: "${8}" - ipv4_t3: "${9}" - ipv4_t4: "${10}" - orgid: "${11}" - oauthid: "${12}" -- match: "*.*" - name: "catchall" - labels: - first: "$1" - second: "$2" - third: "$3" - job: "-" - ` - mappings := mappings{ - "test.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar.baz": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlobNoOrderingWithBacktracking(b *testing.B) { - config := `--- -defaults: - glob_disable_ordering: true -mappings: -- match: test.dispatcher.*.*.succeeded - name: "dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "succeeded" - job: "test_dispatcher" -- match: test.dispatcher.*.received.* - name: "dispatch_events_wont_match" - labels: - processor: "$1" - action: "received" - result: "$2" - job: "test_dispatcher" -- match: test.my-dispatch-host01.name.dispatcher.*.*.* - name: "host_dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "$3" - job: "test_dispatcher" -- match: request_time.*.*.*.*.*.*.*.*.*.*.*.* - name: "tyk_http_request" - labels: - method_and_path: "${1}" - response_code: "${2}" - apikey: "${3}" - apiversion: "${4}" - apiname: "${5}" - apiid: "${6}" - ipv4_t1: "${7}" - ipv4_t2: "${8}" - ipv4_t3: "${9}" - ipv4_t4: "${10}" - orgid: "${11}" - oauthid: "${12}" -- match: "*.*" - name: "catchall" - labels: - first: "$1" - second: "$2" - third: "$3" - job: "-" - ` - mappings := mappings{ - "test.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar.baz": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegex(b *testing.B) { - config := `--- -defaults: - match_type: regex -mappings: -- match: test\.dispatcher\.([^.]*)\.([^.]*)\.([^.]*) - name: "dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "$3" - job: "test_dispatcher" -- match: test.my-dispatch-host01.name.dispatcher\.([^.]*)\.([^.]*)\.([^.]*) - name: "host_dispatch_events" - labels: - processor: "$1" - action: "$2" - result: "$3" - job: "test_dispatcher" -- match: request_time\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) - name: "tyk_http_request" - labels: - method_and_path: "${1}" - response_code: "${2}" - apikey: "${3}" - apiversion: "${4}" - apiname: "${5}" - apiid: "${6}" - ipv4_t1: "${7}" - ipv4_t2: "${8}" - ipv4_t3: "${9}" - ipv4_t4: "${10}" - orgid: "${11}" - oauthid: "${12}" -- match: \.([^.]*)\.([^.]*) - name: "catchall" - labels: - first: "$1" - second: "$2" - third: "$3" - job: "-" - ` - mappings := mappings{ - "test.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "test.my-dispatch-host01.name.dispatcher.FooProcessor.send.succeeded": { - name: "ignored-in-test-dont-set-me", - }, - "request_time.get/threads/1/posts.200.00000000.nonversioned.discussions.a11bbcdf0ac64ec243658dc64b7100fb.172.20.0.1.12ba97b7eaa1a50001000001.": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar": { - name: "ignored-in-test-dont-set-me", - }, - "foo.bar.baz": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlobSingleMatch(b *testing.B) { - config := `--- -mappings: -- match: metric.* - name: "metric_one" - labels: - name: "$1" - ` - mappings := mappings{ - "metric.aaa": {}, - "metric.bbb": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegexSingleMatch(b *testing.B) { - config := `--- -mappings: -- match: metric\.([^.]*) - name: "metric_one" - match_type: regex - labels: - name: "$1" - ` - mappings := mappings{ - "metric.aaa": {}, - "metric.bbb": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlobMultipleCaptures(b *testing.B) { - config := `--- -mappings: -- match: metric.*.*.*.*.*.*.*.*.*.*.*.* - name: "metric_multi" - labels: - name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" - ` - mappings := mappings{ - "metric.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegexMultipleCaptures(b *testing.B) { - config := `--- -mappings: -- match: metric\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) - name: "metric_multi" - match_type: regex - labels: - name: "$1-$2-$3.$4-$5-$6.$7-$8-$9.$10-$11-$12" - ` - mappings := mappings{ - "metric.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlobMultipleCapturesNoFormat(b *testing.B) { - config := `--- -mappings: -- match: metric.*.*.*.*.*.*.*.*.*.*.*.* - name: "metric_multi" - labels: - name: "not_relevant" - ` - mappings := mappings{ - "metric.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegexMultipleCapturesNoFormat(b *testing.B) { - config := `--- -mappings: -- match: metric\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) - name: "metric_multi" - match_type: regex - labels: - name: "not_relevant" - ` - mappings := mappings{ - "metric.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlobMultipleCapturesDifferentLabels(b *testing.B) { - config := `--- -mappings: -- match: metric.*.*.*.*.*.*.*.*.*.*.*.* - name: "metric_multi" - labels: - label1: "$1" - label2: "$2" - label3: "$3" - label4: "$4" - label5: "$5" - label6: "$6" - label7: "$7" - label8: "$8" - label9: "$9" - label10: "$10" - label11: "$11" - label12: "$12" - ` - mappings := mappings{ - "metric.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegexMultipleCapturesDifferentLabels(b *testing.B) { - config := `--- -mappings: -- match: metric\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)\.([^.]*) - name: "metric_multi" - match_type: regex - labels: - label1: "$1" - label2: "$2" - label3: "$3" - label4: "$4" - label5: "$5" - label6: "$6" - label7: "$7" - label8: "$8" - label9: "$9" - label10: "$10" - label11: "$11" - label12: "$12" - ` - mappings := mappings{ - "metric.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlob10Rules(b *testing.B) { - config := `--- -mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) - mappings := mappings{ - "metric100.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegex10RulesAverage(b *testing.B) { - config := `--- -defaults: - match_type: regex -mappings:` + duplicateRules(10, ruleTemplateSingleMatchRegex) - mappings := mappings{ - "metric5.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlob100Rules(b *testing.B) { - config := `--- -mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) - mappings := mappings{ - "metric100.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlob100RulesNoMatch(b *testing.B) { - config := `--- -mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) - mappings := mappings{ - "metricnomatchy.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlob100RulesNoOrderingNoMatch(b *testing.B) { - config := `--- -defaults: - glob_disable_ordering: true -mappings:` + duplicateRules(100, ruleTemplateSingleMatchGlob) - mappings := mappings{ - "metricnomatchy.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegex100RulesAverage(b *testing.B) { - config := `--- -defaults: - match_type: regex -mappings:` + duplicateRules(100, ruleTemplateSingleMatchRegex) - mappings := mappings{ - "metric50.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegex100RulesWorst(b *testing.B) { - config := `--- -defaults: - match_type: regex -mappings:` + duplicateRules(100, ruleTemplateSingleMatchRegex) - mappings := mappings{ - "metric100.a": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkGlob100RulesMultipleCaptures(b *testing.B) { - config := `--- -mappings:` + duplicateRules(100, ruleTemplateMultipleMatchGlob) - mappings := mappings{ - "metric50.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegex100RulesMultipleCapturesAverage(b *testing.B) { - config := `--- -defaults: - match_type: regex -mappings:` + duplicateRules(100, ruleTemplateMultipleMatchRegex) - mappings := mappings{ - "metric50.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - -func BenchmarkRegex100RulesMultipleCapturesWorst(b *testing.B) { - config := `--- -defaults: - match_type: regex -mappings:` + duplicateRules(100, ruleTemplateMultipleMatchRegex) - mappings := mappings{ - "metric100.a.b.c.d.e.f.g.h.i.j.k.l": {}, - } - - mapper := MetricMapper{} - err := mapper.InitFromYAMLString(config) - if err != nil { - b.Fatalf("Config load error: %s %s", config, err) - } - - var dummyMetricType MetricType - b.ResetTimer() - for j := 0; j < b.N; j++ { - for metric := range mappings { - mapper.GetMapping(metric, dummyMetricType) - } - } -} - func TestAction(t *testing.T) { scenarios := []struct { config string