From 2524d94fe168ffaef3a29e54d70ff626d7d991b2 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 29 Nov 2022 18:12:22 -0800 Subject: [PATCH] lib/promscrape/discovery: add a benchmark for measuring the performance of creating pod meta-labels --- .../discovery/kubernetes/node_test.go | 21 +++++++----- .../discovery/kubernetes/pod_test.go | 7 ++-- .../discovery/kubernetes/pod_timing_test.go | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 lib/promscrape/discovery/kubernetes/pod_timing_test.go diff --git a/lib/promscrape/discovery/kubernetes/node_test.go b/lib/promscrape/discovery/kubernetes/node_test.go index b42b85270..eca21b7d7 100644 --- a/lib/promscrape/discovery/kubernetes/node_test.go +++ b/lib/promscrape/discovery/kubernetes/node_test.go @@ -289,6 +289,18 @@ func TestParseNodeListSuccess(t *testing.T) { } func getSortedLabelss(objectsByKey map[string]object) [][]prompbmarshal.Label { + gw := newTestGroupWatcher() + var result [][]prompbmarshal.Label + for _, o := range objectsByKey { + labelss := o.getTargetLabels(gw) + for _, labels := range labelss { + result = append(result, discoveryutils.GetSortedLabels(labels)) + } + } + return result +} + +func newTestGroupWatcher() *groupWatcher { var gw groupWatcher gw.m = map[string]*urlWatcher{ "node": { @@ -308,14 +320,7 @@ func getSortedLabelss(objectsByKey map[string]object) [][]prompbmarshal.Label { }, } gw.attachNodeMetadata = true - var result [][]prompbmarshal.Label - for _, o := range objectsByKey { - labelss := o.getTargetLabels(&gw) - for _, labels := range labelss { - result = append(result, discoveryutils.GetSortedLabels(labels)) - } - } - return result + return &gw } func areEqualLabelss(a, b [][]prompbmarshal.Label) bool { diff --git a/lib/promscrape/discovery/kubernetes/pod_test.go b/lib/promscrape/discovery/kubernetes/pod_test.go index a3c7997af..5d5bc81d8 100644 --- a/lib/promscrape/discovery/kubernetes/pod_test.go +++ b/lib/promscrape/discovery/kubernetes/pod_test.go @@ -26,8 +26,7 @@ func TestParsePodListFailure(t *testing.T) { f(`{"items":[{"metadata":{"labels":[1]}}]}`) } -func TestParsePodListSuccess(t *testing.T) { - data := ` +const testPodsList = ` { "kind": "PodList", "apiVersion": "v1", @@ -229,7 +228,9 @@ func TestParsePodListSuccess(t *testing.T) { ] } ` - r := bytes.NewBufferString(data) + +func TestParsePodListSuccess(t *testing.T) { + r := bytes.NewBufferString(testPodsList) objectsByKey, meta, err := parsePodList(r) if err != nil { t.Fatalf("unexpected error: %s", err) diff --git a/lib/promscrape/discovery/kubernetes/pod_timing_test.go b/lib/promscrape/discovery/kubernetes/pod_timing_test.go new file mode 100644 index 000000000..5d8d7b260 --- /dev/null +++ b/lib/promscrape/discovery/kubernetes/pod_timing_test.go @@ -0,0 +1,34 @@ +package kubernetes + +import ( + "bytes" + "fmt" + "testing" +) + +func BenchmarkPodGetTargetLabels(b *testing.B) { + r := bytes.NewBufferString(testPodsList) + objectsByKey, _, err := parsePodList(r) + if err != nil { + panic(fmt.Errorf("BUG: unexpected error: %s", err)) + } + var o object + for _, srcObject := range objectsByKey { + o = srcObject + break + } + if o == nil { + panic(fmt.Errorf("BUG: expecting at least a single pod object")) + } + gw := newTestGroupWatcher() + b.ReportAllocs() + b.SetBytes(1) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + labelss := o.getTargetLabels(gw) + if len(labelss) != 1 { + panic(fmt.Errorf("BUG: unexpected number of labelss returned: %d; want 1", len(labelss))) + } + } + }) +}