Skip to content

Commit 1149bfc

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 1149bfc

File tree

2 files changed

+470
-415
lines changed

2 files changed

+470
-415
lines changed

metrics/prometheus_bench_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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), which produces:
31+
// * 84 metric families, with 5567 series in total.
32+
// * 1081124 bytes (~1MB) of DTO struct.
33+
// Reproducing https://github.com/kubernetes/kubernetes/issues/104459.
34+
infoProvider := benchSubcontainersInfoProvider{containers: make(map[string]*info.ContainerInfo, 20)}
35+
for i:=0; i<=20; i++ {
36+
name := fmt.Sprintf("container-%v", i)
37+
infoProvider.containers[name] = genContainerInfo(name)
38+
}
39+
40+
reg := prometheus.NewRegistry()
41+
reg.MustRegister(NewPrometheusCollector(infoProvider, func(container *info.ContainerInfo) map[string]string {
42+
s := DefaultContainerLabels(container)
43+
s["zone.name"] = "hello"
44+
return s
45+
}, container.AllMetrics, now, v2.RequestOptions{}))
46+
47+
var err error
48+
b.ResetTimer()
49+
for i := 0; i < b.N; i++ {
50+
mfsTmp, err = reg.Gather()
51+
require.NoError(b, err)
52+
}
53+
}

0 commit comments

Comments
 (0)