lib/promscrape/discovery: add a benchmark for measuring the performance of creating pod meta-labels

This commit is contained in:
Aliaksandr Valialkin 2022-11-29 18:12:22 -08:00
parent 002c028f22
commit 2524d94fe1
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1
3 changed files with 51 additions and 11 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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)))
}
}
})
}