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