/
container.go
159 lines (134 loc) · 5.53 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package benchmark
import (
"fmt"
"path"
"time"
"github.com/golang/glog"
"github.com/kubernetes-sigs/cri-tools/pkg/framework"
. "github.com/onsi/ginkgo"
"github.com/onsi/gomega/gmeasure"
internalapi "k8s.io/cri-api/pkg/apis"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
)
const (
defaultContainerBenchmarkTimeoutSeconds = 60
)
var _ = framework.KubeDescribe("Container", func() {
f := framework.NewDefaultCRIFramework()
var rc internalapi.RuntimeService
var ic internalapi.ImageManagerService
BeforeEach(func() {
rc = f.CRIClient.CRIRuntimeClient
ic = f.CRIClient.CRIImageClient
})
Context("benchmark about operations on Container", func() {
It("benchmark about basic operations on Container", func() {
timeout := defaultContainerBenchmarkTimeoutSeconds
if framework.TestContext.BenchmarkingParams.ContainerBenchmarkTimeoutSeconds > 0 {
timeout = framework.TestContext.BenchmarkingParams.ContainerBenchmarkTimeoutSeconds
}
// Setup sampling config from TestContext:
samplingConfig := gmeasure.SamplingConfig{
N: framework.TestContext.BenchmarkingParams.ContainersNumber,
NumParallel: framework.TestContext.BenchmarkingParams.ContainersNumberParallel,
}
if samplingConfig.N <= 0 {
Skip("skipping container lifecycle benchmarks since container number option was not set")
}
if samplingConfig.NumParallel < 1 {
samplingConfig.NumParallel = 1
}
// Setup results reporting channel:
resultsSet := LifecycleBenchmarksResultsSet{
OperationsNames: []string{"CreateContainer", "StartContainer", "StatusContainer", "StopContainer", "RemoveContainer"},
NumParallel: samplingConfig.NumParallel,
Datapoints: make([]LifecycleBenchmarkDatapoint, 0),
}
resultsManager := NewLifecycleBenchmarksResultsManager(
resultsSet,
timeout,
)
resultsChannel := resultsManager.StartResultsConsumer()
experiment := gmeasure.NewExperiment("ContainerOps")
experiment.Sample(func(idx int) {
var podID string
var podConfig *runtimeapi.PodSandboxConfig
var containerID string
var lastStartTime, lastEndTime int64
var err error
durations := make([]int64, len(resultsSet.OperationsNames))
podID, podConfig = framework.CreatePodSandboxForContainer(rc)
By(fmt.Sprintf("CreatingContainer %d", idx))
startTime := time.Now().UnixNano()
lastStartTime = startTime
containerID = framework.CreateDefaultContainer(rc, ic, podID, podConfig, "Benchmark-container-")
lastEndTime = time.Now().UnixNano()
durations[0] = lastEndTime - lastStartTime
By(fmt.Sprintf("StartingContainer %d", idx))
lastStartTime = time.Now().UnixNano()
err = rc.StartContainer(containerID)
lastEndTime = time.Now().UnixNano()
durations[1] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to start Container: %v", err)
By(fmt.Sprintf("ContainerStatus %d", idx))
lastStartTime = time.Now().UnixNano()
_, err = rc.ContainerStatus(containerID, true)
lastEndTime = time.Now().UnixNano()
durations[2] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to get Container status: %v", err)
By(fmt.Sprintf("ContainerStop %d", idx))
lastStartTime = time.Now().UnixNano()
err = rc.StopContainer(containerID, framework.DefaultStopContainerTimeout)
lastEndTime = time.Now().UnixNano()
durations[3] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to stop Container: %v", err)
By(fmt.Sprintf("ContainerRemove %d", idx))
lastStartTime = time.Now().UnixNano()
err = rc.RemoveContainer(containerID)
lastEndTime = time.Now().UnixNano()
durations[4] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to remove Container: %v", err)
res := LifecycleBenchmarkDatapoint{
SampleIndex: idx,
StartTime: startTime,
EndTime: lastEndTime,
OperationsDurationsNs: durations,
MetaInfo: map[string]string{"podId": podID, "containerId": containerID},
}
resultsChannel <- &res
By(fmt.Sprintf("stop PodSandbox %d", idx))
rc.StopPodSandbox(podID)
By(fmt.Sprintf("delete PodSandbox %d", idx))
rc.RemovePodSandbox(podID)
}, samplingConfig)
// Send nil and give the manager a minute to process any already-queued results:
resultsChannel <- nil
err := resultsManager.AwaitAllResults(60)
if err != nil {
glog.Errorf("Results manager failed to await all results: %s", err)
}
if framework.TestContext.BenchmarkingOutputDir != "" {
filepath := path.Join(framework.TestContext.BenchmarkingOutputDir, "container_benchmark_data.json")
err = resultsManager.WriteResultsFile(filepath)
if err != nil {
glog.Errorf("Error occurred while writing benchmark results to file %s: %s", filepath, err)
}
} else {
glog.Infof("No benchmarking output dir provided, skipping writing benchmarking results file.")
glog.Infof("Benchmark results were: %+v", resultsManager.resultsSet)
}
})
})
})