/
pod.go
152 lines (126 loc) · 5.08 KB
/
pod.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
/*
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 (
"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 (
defaultPodBenchmarkTimeoutSeconds = 60
)
var _ = framework.KubeDescribe("PodSandbox", func() {
f := framework.NewDefaultCRIFramework()
var c internalapi.RuntimeService
BeforeEach(func() {
c = f.CRIClient.CRIRuntimeClient
})
Context("benchmark about operations on PodSandbox", func() {
It("benchmark about lifecycle of PodSandbox", func() {
timeout := defaultPodBenchmarkTimeoutSeconds
if framework.TestContext.BenchmarkingParams.ContainerBenchmarkTimeoutSeconds > 0 {
timeout = framework.TestContext.BenchmarkingParams.ContainerBenchmarkTimeoutSeconds
}
// Setup sampling config from TestContext:
samplingConfig := gmeasure.SamplingConfig{
N: framework.TestContext.BenchmarkingParams.PodsNumber,
NumParallel: framework.TestContext.BenchmarkingParams.PodsNumberParallel,
}
if samplingConfig.N < 1 {
samplingConfig.N = 1
}
if samplingConfig.NumParallel < 1 {
samplingConfig.NumParallel = 1
}
// Setup results reporting channel:
resultsSet := LifecycleBenchmarksResultsSet{
OperationsNames: []string{"CreatePod", "StatusPod", "StopPod", "RemovePod"},
NumParallel: samplingConfig.NumParallel,
Datapoints: make([]LifecycleBenchmarkDatapoint, 0),
}
resultsManager := NewLifecycleBenchmarksResultsManager(
resultsSet,
timeout,
)
resultsChannel := resultsManager.StartResultsConsumer()
experiment := gmeasure.NewExperiment("PodLifecycle")
experiment.Sample(func(idx int) {
var lastStartTime, lastEndTime int64
var podID string
var err error
durations := make([]int64, len(resultsSet.OperationsNames))
podSandboxName := "PodSandbox-for-creating-performance-test-" + framework.NewUUID()
uid := framework.DefaultUIDPrefix + framework.NewUUID()
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
config := &runtimeapi.PodSandboxConfig{
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
Linux: &runtimeapi.LinuxPodSandboxConfig{},
Labels: framework.DefaultPodLabels,
}
By("Creating a pod")
startTime := time.Now().UnixNano()
lastStartTime = startTime
podID, err = c.RunPodSandbox(config, framework.TestContext.RuntimeHandler)
lastEndTime = time.Now().UnixNano()
durations[0] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to create PodSandbox: %v", err)
By("Get Pod status")
lastStartTime = time.Now().UnixNano()
_, err = c.PodSandboxStatus(podID, true)
lastEndTime = time.Now().UnixNano()
durations[1] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to get PodStatus: %v", err)
By("Stop PodSandbox")
lastStartTime = time.Now().UnixNano()
err = c.StopPodSandbox(podID)
lastEndTime = time.Now().UnixNano()
durations[2] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to stop PodSandbox: %v", err)
By("Remove PodSandbox")
lastStartTime = time.Now().UnixNano()
err = c.RemovePodSandbox(podID)
lastEndTime = time.Now().UnixNano()
durations[3] = lastEndTime - lastStartTime
framework.ExpectNoError(err, "failed to remove PodSandbox: %v", err)
res := LifecycleBenchmarkDatapoint{
StartTime: startTime,
EndTime: lastEndTime,
OperationsDurationsNs: durations,
MetaInfo: map[string]string{"podId": podID, "podSandboxName": podSandboxName},
}
resultsChannel <- &res
}, 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, "newf_pod_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 out dir provided, skipping writing benchmarking results.")
glog.Infof("Benchmark results were: %+v", resultsManager.resultsSet)
}
})
})
})