From d8d455856c6fde6e5a29e34edee7f6e28cad7160 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 1 Oct 2022 10:37:07 +0300 Subject: [PATCH] lib/promrelabel: add a benchmark for realistic Kubernetes relabeling The benchmark name is BenchmarkApplyRelabelConfigs/kubernetes This benchmark has been copied from https://github.com/Arnoways/prometheus/blob/d521933053bdf68d252e365da9376706d04addcc/model/relabel/relabel_test.go#L505 See also https://github.com/prometheus/prometheus/pull/11147 --- lib/promrelabel/relabel_timing_test.go | 449 +++++++++++++------------ 1 file changed, 234 insertions(+), 215 deletions(-) diff --git a/lib/promrelabel/relabel_timing_test.go b/lib/promrelabel/relabel_timing_test.go index eaba87047f..df588db3b4 100644 --- a/lib/promrelabel/relabel_timing_test.go +++ b/lib/promrelabel/relabel_timing_test.go @@ -270,22 +270,149 @@ func BenchmarkMatchRegexOrValuesMismatchUnoptimized(b *testing.B) { } func BenchmarkApplyRelabelConfigs(b *testing.B) { + b.Run("kubernetes", func(b *testing.B) { + // See https://github.com/Arnoways/prometheus/blob/d521933053bdf68d252e365da9376706d04addcc/model/relabel/relabel_test.go#L505 + pcs := mustParseRelabelConfigs(` +- source_labels: + - __meta_kubernetes_pod_container_port_name + regex: .*-metrics + action: keep +- source_labels: + - __meta_kubernetes_pod_label_name + action: drop + regex: "" +- source_labels: + - __meta_kubernetes_pod_phase + regex: Succeeded|Failed + action: drop +- source_labels: + - __meta_kubernetes_pod_annotation_prometheus_io_scrape + regex: "false" + action: drop +- source_labels: + - __meta_kubernetes_pod_annotation_prometheus_io_scheme + target_label: __scheme__ + regex: (https?) + replacement: $1 + action: replace +- source_labels: + - __meta_kubernetes_pod_annotation_prometheus_io_path + target_label: __metrics_path__ + regex: (.+) + replacement: $1 + action: replace +- source_labels: + - __address__ + - __meta_kubernetes_pod_annotation_prometheus_io_port + target_label: __address__ + regex: (.+?)(\:\d+)?;(\d+) + replacement: $1:$3 + action: replace +- regex: __meta_kubernetes_pod_annotation_prometheus_io_param_(.+) + replacement: __param_$1 + action: labelmap +- regex: __meta_kubernetes_pod_label_prometheus_io_label_(.+) + action: labelmap +- regex: __meta_kubernetes_pod_annotation_prometheus_io_label_(.+) + action: labelmap +- source_labels: + - __meta_kubernetes_namespace + - __meta_kubernetes_pod_label_name + separator: / + target_label: job + replacement: $1 + action: replace +- source_labels: + - __meta_kubernetes_namespace + target_label: namespace + action: replace +- source_labels: + - __meta_kubernetes_pod_name + target_label: pod + action: replace +- source_labels: + - __meta_kubernetes_pod_container_name + target_label: container + action: replace +- source_labels: + - __meta_kubernetes_pod_name + - __meta_kubernetes_pod_container_name + - __meta_kubernetes_pod_container_port_name + separator: ':' + target_label: instance + action: replace +- target_label: cluster + replacement: dev-us-central-0 +- source_labels: + - __meta_kubernetes_namespace + regex: hosted-grafana + action: drop +- source_labels: + - __address__ + target_label: __tmp_hash + modulus: 3 + action: hashmod +- source_labels: + - __tmp_hash + regex: ^0$ + action: keep +- regex: __tmp_hash + action: labeldrop +`) + labelsOrig := labelsFromStrings( + "__address__", "10.132.183.40:80", + "__meta_kubernetes_namespace", "loki-boltdb-shipper", + "__meta_kubernetes_pod_annotation_promtail_loki_boltdb_shipper_hash", "50523b9759094a144adcec2eae0aa4ad", + "__meta_kubernetes_pod_annotationpresent_promtail_loki_boltdb_shipper_hash", "true", + "__meta_kubernetes_pod_container_init", "false", + "__meta_kubernetes_pod_container_name", "promtail", + "__meta_kubernetes_pod_container_port_name", "http-metrics", + "__meta_kubernetes_pod_container_port_number", "80", + "__meta_kubernetes_pod_container_port_protocol", "TCP", + "__meta_kubernetes_pod_controller_kind", "DaemonSet", + "__meta_kubernetes_pod_controller_name", "promtail-loki-boltdb-shipper", + "__meta_kubernetes_pod_host_ip", "10.128.0.178", + "__meta_kubernetes_pod_ip", "10.132.183.40", + "__meta_kubernetes_pod_label_controller_revision_hash", "555b77cd7d", + "__meta_kubernetes_pod_label_name", "promtail-loki-boltdb-shipper", + "__meta_kubernetes_pod_label_pod_template_generation", "45", + "__meta_kubernetes_pod_labelpresent_controller_revision_hash", "true", + "__meta_kubernetes_pod_labelpresent_name", "true", + "__meta_kubernetes_pod_labelpresent_pod_template_generation", "true", + "__meta_kubernetes_pod_name", "promtail-loki-boltdb-shipper-jgtr7", + "__meta_kubernetes_pod_node_name", "gke-dev-us-central-0-main-n2s8-2-14d53341-9hkr", + "__meta_kubernetes_pod_phase", "Running", + "__meta_kubernetes_pod_ready", "true", + "__meta_kubernetes_pod_uid", "4c586419-7f6c-448d-aeec-ca4fa5b05e60", + "__metrics_path__", "/metrics", + "__scheme__", "http", + "__scrape_interval__", "15s", + "__scrape_timeout__", "10s", + "job", "kubernetes-pods", + ) + b.ReportAllocs() + b.SetBytes(1) + b.RunParallel(func(pb *testing.PB) { + var labels []prompbmarshal.Label + for pb.Next() { + labels = append(labels[:0], labelsOrig...) + labels = pcs.Apply(labels, 0, false) + if len(labels) != 0 { + panic(fmt.Errorf("BUG: expecting empty labels")) + } + } + }) + }) b.Run("replace-label-copy", func(b *testing.B) { pcs := mustParseRelabelConfigs(` - action: replace source_labels: [id] target_label: __name__ `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -294,7 +421,7 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { labels = append(labels[:0], labelsOrig...) labels = pcs.Apply(labels, 0, true) if len(labels) != len(labelsOrig) { - panic(fmt.Errorf("unexpected number of labels; got %d; want %d; labels:\n%#v", len(labels), len(labelsOrig), labels)) + panic(fmt.Errorf("unexpected number of labels; got %d; want %d; labels:\n%#v", len(labels), len(labelsOrig), labelsOrig)) } if labels[0].Name != "__name__" { panic(fmt.Errorf("unexpected label name; got %q; want %q", labels[0].Name, "__name__")) @@ -317,16 +444,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { target_label: __name__ replacement: foobar `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -358,12 +479,9 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { target_label: aaa replacement: foobar `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -396,16 +514,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { target_label: id regex: "(foobar)-.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -438,16 +550,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { target_label: id regex: "(foobar)-.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -479,16 +585,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { source_labels: ["non-existing-label"] regex: "(foobar)-.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -520,16 +620,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { source_labels: [id] regex: "yes" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "yes", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "yes", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -549,16 +643,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { source_labels: [id] regex: "(foobar)-.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -578,16 +666,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { source_labels: ["non-existing-label"] regex: "(foobar)-.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -607,16 +689,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { source_labels: [id] regex: "yes" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "yes", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "yes", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -648,16 +724,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { source_labels: [id] regex: "(foobar)-.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -688,16 +758,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labeldrop regex: "non-existing-label" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -728,16 +792,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labeldrop regex: id `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -762,16 +820,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labeldrop regex: "id.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -796,16 +848,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labeldrop regex: ".*id.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -830,16 +876,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelkeep regex: "non-existing-label" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -858,16 +898,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelkeep regex: id `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -892,16 +926,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelkeep regex: "id.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -926,16 +954,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelkeep regex: ".*id.*" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -960,12 +982,9 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelmap regex: "a(.*)" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "foo", - Value: "bar", - }, - } + labelsOrig := labelsFromStrings( + "foo", "bar", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -990,12 +1009,9 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelmap regex: "a(.*)" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "aabc", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "aabc", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -1026,12 +1042,9 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { - action: labelmap regex: "(.*)bc" `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "aabc", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "aabc", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -1064,16 +1077,10 @@ func BenchmarkApplyRelabelConfigs(b *testing.B) { target_label: id modulus: 23 `) - labelsOrig := []prompbmarshal.Label{ - { - Name: "__name__", - Value: "metric", - }, - { - Name: "id", - Value: "foobar-random-string-here", - }, - } + labelsOrig := labelsFromStrings( + "__name__", "metric", + "id", "foobar-random-string-here", + ) b.ReportAllocs() b.SetBytes(1) b.RunParallel(func(pb *testing.PB) { @@ -1108,3 +1115,15 @@ func mustParseRelabelConfigs(config string) *ParsedConfigs { } return pcs } + +func labelsFromStrings(ss ...string) []prompbmarshal.Label { + labelsLen := len(ss) / 2 + labels := make([]prompbmarshal.Label, 0, labelsLen) + for i := 0; i < len(ss); i += 2 { + labels = append(labels, prompbmarshal.Label{ + Name: ss[i], + Value: ss[i+1], + }) + } + return labels +}