|
| 1 | +/* |
| 2 | +Copyright 2023. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package metric_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "strconv" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/sustainable-computing-io/kepler/pkg/collector/metric" |
| 23 | +) |
| 24 | + |
| 25 | +func benchmarkNtesting(b *testing.B, continerNumber int) { |
| 26 | + var containerMetrics map[string]*metric.ContainerMetrics |
| 27 | + nodeMetrics := metric.NewNodeMetrics() |
| 28 | + containerMetrics = make(map[string]*metric.ContainerMetrics) |
| 29 | + for i := 0; i < continerNumber; i++ { |
| 30 | + containerMetrics["container"+strconv.Itoa(i)] = createMockContainerMetrics("container"+strconv.Itoa(i), "podA", "test") |
| 31 | + } |
| 32 | + b.ReportAllocs() |
| 33 | + b.ResetTimer() |
| 34 | + nodeMetrics.AddNodeResUsageFromContainerResUsage(containerMetrics) |
| 35 | + b.StopTimer() |
| 36 | +} |
| 37 | + |
| 38 | +func BenchmarkAddNodeResUsageFromContainerResUsageWith1000Contianer(b *testing.B) { |
| 39 | + benchmarkNtesting(b, 1000) |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkAddNodeResUsageFromContainerResUsageWith2000Contianer(b *testing.B) { |
| 43 | + benchmarkNtesting(b, 2000) |
| 44 | +} |
| 45 | + |
| 46 | +func BenchmarkAddNodeResUsageFromContainerResUsageWith5000Contianer(b *testing.B) { |
| 47 | + benchmarkNtesting(b, 5000) |
| 48 | +} |
| 49 | + |
| 50 | +func BenchmarkAddNodeResUsageFromContainerResUsageWith10000Contianer(b *testing.B) { |
| 51 | + benchmarkNtesting(b, 10000) |
| 52 | +} |
| 53 | + |
| 54 | +// see usageMetrics for the list of used metrics. For the sake of visibility we add all metrics, but only few of them will be used. |
| 55 | +func createMockContainerMetrics(containerName, podName, namespace string) *metric.ContainerMetrics { |
| 56 | + containerMetrics := metric.NewContainerMetrics(containerName, podName, namespace) |
| 57 | + // bpf - cpu time |
| 58 | + _ = containerMetrics.CPUTime.AddNewDelta(10) // config.CPUTime |
| 59 | + return containerMetrics |
| 60 | +} |
0 commit comments