Skip to content

Commit c82ba64

Browse files
committed
Added baseline benchmark for PrometheusCollector_Collect.
``` /tmp/GoLand/___BenchmarkPrometheusCollector_Collect_in_github_com_google_cadvisor_metrics.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkPrometheusCollector_Collect\E$ -test.run ^$ -test.benchtime 10s goos: linux goarch: amd64 pkg: github.com/google/cadvisor/metrics cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz BenchmarkPrometheusCollector_Collect BenchmarkPrometheusCollector_Collect-12 748 14929127 ns/op 8253796 B/op 207706 allocs/op PASS Process finished with the exit code 0 ``` Signed-off-by: Bartlomiej Plotka <[email protected]>
1 parent 8b96dc6 commit c82ba64

File tree

2 files changed

+468
-415
lines changed

2 files changed

+468
-415
lines changed

metrics/prometheus_bench_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package metrics
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/google/cadvisor/container"
8+
info "github.com/google/cadvisor/info/v1"
9+
v2 "github.com/google/cadvisor/info/v2"
10+
"github.com/prometheus/client_golang/prometheus"
11+
dto "github.com/prometheus/client_model/go"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
var mfsTmp []*dto.MetricFamily
16+
17+
type benchSubcontainersInfoProvider struct {
18+
testSubcontainersInfoProvider
19+
20+
containers map[string]*info.ContainerInfo
21+
}
22+
23+
func (p benchSubcontainersInfoProvider) GetRequestedContainersInfo(string, v2.RequestOptions) (map[string]*info.ContainerInfo, error) {
24+
return p.containers, nil
25+
}
26+
27+
func BenchmarkPrometheusCollector_Collect(b *testing.B) {
28+
b.ReportAllocs()
29+
30+
// Generate bigger dataset for realistic situation (we can assume node has ~20 pods).
31+
// Reproducing https://github.com/kubernetes/kubernetes/issues/104459.
32+
infoProvider := benchSubcontainersInfoProvider{containers: make(map[string]*info.ContainerInfo, 20)}
33+
for i:=0; i<=20; i++ {
34+
name := fmt.Sprintf("container-%v", i)
35+
infoProvider.containers[name] = genContainerInfo(name)
36+
}
37+
38+
reg := prometheus.NewRegistry()
39+
reg.MustRegister(NewPrometheusCollector(infoProvider, func(container *info.ContainerInfo) map[string]string {
40+
s := DefaultContainerLabels(container)
41+
s["zone.name"] = "hello"
42+
return s
43+
}, container.AllMetrics, now, v2.RequestOptions{}))
44+
45+
var err error
46+
b.ResetTimer()
47+
for i := 0; i < b.N; i++ {
48+
mfsTmp, err = reg.Gather()
49+
require.NoError(b, err)
50+
}
51+
}

0 commit comments

Comments
 (0)