forked from knative/serving
/
main.go
284 lines (252 loc) · 7.94 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright 2019 The Knative 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 main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"strconv"
"sync"
"testing"
"time"
v1 "k8s.io/api/apps/v1"
"github.com/google/mako/go/quickstore"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"
"knative.dev/pkg/test/mako"
"knative.dev/serving/pkg/apis/serving"
"knative.dev/serving/test/performance"
"golang.org/x/sync/errgroup"
"knative.dev/pkg/injection/sharedmain"
pkgTest "knative.dev/pkg/test"
"knative.dev/serving/pkg/apis/autoscaling"
ktest "knative.dev/serving/pkg/testing/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
parallelCount = flag.Int("parallel", 0, "The count of ksvcs we want to run scale-from-zero in parallel")
)
const (
testNamespace = "default"
serviceName = "perftest-scalefromzero"
helloWorldExpectedOutput = "Hello World!"
helloWorldImage = "helloworld"
waitToServe = 2 * time.Minute
)
func clientsFromConfig() (*test.Clients, error) {
cfg, err := sharedmain.GetConfig("", "")
if err != nil {
return nil, fmt.Errorf("error building kubeconfig: %v", err)
}
return test.NewClientsFromConfig(cfg, testNamespace)
}
func createServices(clients *test.Clients, count int) ([]*v1test.ResourceObjects, func(), error) {
testNames := make([]*test.ResourceNames, count)
// Initialize our service names.
for i := 0; i < count; i++ {
testNames[i] = &test.ResourceNames{
Service: test.AppendRandomString(fmt.Sprintf("%s-%02d", serviceName, i)),
// The crd.go helpers will convert to the actual image path.
Image: helloWorldImage,
}
}
cleanupNames := func() {
for i := 0; i < count; i++ {
test.TearDown(clients, *testNames[i])
}
}
objs := make([]*v1test.ResourceObjects, count)
begin := time.Now()
sos := []ktest.ServiceOption{
// We set a small resource alloc so that we can pack more pods into the cluster.
ktest.WithResourceRequirements(corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("50m"),
corev1.ResourceMemory: resource.MustParse("50Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("10m"),
corev1.ResourceMemory: resource.MustParse("20Mi"),
},
}),
ktest.WithConfigAnnotations(map[string]string{
autoscaling.WindowAnnotationKey: "7s",
}),
}
g := errgroup.Group{}
for i := 0; i < count; i++ {
ndx := i
g.Go(func() error {
var err error
if objs[ndx], err = v1test.CreateServiceReady(&testing.T{}, clients, testNames[ndx], sos...); err != nil {
return fmt.Errorf("%02d: failed to create Ready service: %v", ndx, err)
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, nil, err
}
log.Printf("Created all the services in %v", time.Since(begin))
return objs, cleanupNames, nil
}
func waitForScaleToZero(ctx context.Context, objs []*v1test.ResourceObjects) error {
g := errgroup.Group{}
for i := 0; i < len(objs); i++ {
idx := i
ro := objs[i]
g.Go(func() error {
log.Printf("%02d: waiting for deployment to scale to zero", idx)
selector := labels.SelectorFromSet(labels.Set{
serving.ServiceLabelKey: ro.Service.Name,
})
if err := performance.WaitForScaleToZero(ctx, testNamespace, selector, 2*time.Minute); err != nil {
m := fmt.Sprintf("%02d: failed waiting for deployment to scale to zero: %v", idx, err)
log.Println(m)
return errors.New(m)
}
return nil
})
}
return g.Wait()
}
func parallelScaleFromZero(ctx context.Context, clients *test.Clients, objs []*v1test.ResourceObjects, q *quickstore.Quickstore) {
count := len(objs)
// Get the key for saving latency and error metrics in the benchmark.
lk := "l" + strconv.Itoa(count)
dlk := "dl" + strconv.Itoa(count)
ek := "e" + strconv.Itoa(count)
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
ndx := i
go func() {
defer wg.Done()
sdur, ddur, err := runScaleFromZero(ctx, clients, ndx, objs[ndx])
if err == nil {
q.AddSamplePoint(mako.XTime(time.Now()), map[string]float64{
lk: sdur.Seconds(),
})
q.AddSamplePoint(mako.XTime(time.Now()), map[string]float64{
dlk: ddur.Seconds(),
})
} else {
// Add 1 to the error metric whenever there is an error.
q.AddSamplePoint(mako.XTime(time.Now()), map[string]float64{
ek: 1,
})
// By reporting errors like this, the error strings show up on
// the details page for each Mako run.
q.AddError(mako.XTime(time.Now()), err.Error())
}
}()
}
wg.Wait()
}
func runScaleFromZero(ctx context.Context, clients *test.Clients, idx int, ro *v1test.ResourceObjects) (
time.Duration, time.Duration, error) {
selector := labels.SelectorFromSet(labels.Set{
serving.ServiceLabelKey: ro.Service.Name,
})
watcher, err := clients.KubeClient.Kube.AppsV1().Deployments(testNamespace).Watch(
metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
m := fmt.Sprintf("%02d: unable to watch the deployment for the service: %v", idx, err)
log.Println(m)
return 0, 0, errors.New(m)
}
defer watcher.Stop()
ddch := watcher.ResultChan()
sdch := make(chan struct{})
errch := make(chan error)
go func() {
log.Printf("%02d: waiting for endpoint to serve request", idx)
url := ro.Route.Status.URL.URL()
_, err := pkgTest.WaitForEndpointStateWithTimeout(
clients.KubeClient,
log.Printf,
url,
pkgTest.MatchesAllOf(pkgTest.IsStatusOK, pkgTest.MatchesBody(helloWorldExpectedOutput)),
"HelloWorldServesText",
test.ServingFlags.ResolvableDomain, waitToServe,
)
if err != nil {
m := fmt.Sprintf("%02d: the endpoint for Route %q at %q didn't serve the expected text %q: %v", idx, ro.Route.Name, url, helloWorldExpectedOutput, err)
log.Println(m)
errch <- errors.New(m)
return
}
sdch <- struct{}{}
}()
start := time.Now()
// Get the duration that takes to change deployment spec.
var dd time.Duration
for {
select {
case event := <-ddch:
if event.Type == watch.Modified {
dm := event.Object.(*v1.Deployment)
if *dm.Spec.Replicas != 0 && dd == 0 {
dd = time.Since(start)
}
}
case <-sdch:
return time.Since(start), dd, nil
case err := <-errch:
return 0, 0, err
}
}
}
func testScaleFromZero(clients *test.Clients, count int) {
parallelTag := fmt.Sprintf("parallel=%d", count)
mc, err := mako.Setup(context.Background(), parallelTag)
if err != nil {
log.Fatalf("failed to setup mako: %v", err)
}
q, qclose, ctx := mc.Quickstore, mc.ShutDownFunc, mc.Context
defer qclose(ctx)
// Create the services once.
objs, cleanup, err := createServices(clients, count)
// Wrap fatalf in a helper or our sidecar will live forever, also wrap cleanup.
fatalf := func(f string, args ...interface{}) {
cleanup()
qclose(ctx)
log.Fatalf(f, args...)
}
if err != nil {
fatalf("Failed to create services: %v", err)
}
defer cleanup()
// Wait all services scaling to zero.
if err := waitForScaleToZero(ctx, objs); err != nil {
fatalf("Failed to wait for all services to scale to zero: %v", err)
}
parallelScaleFromZero(ctx, clients, objs, q)
if err := mc.StoreAndHandleResult(); err != nil {
fatalf("Failed to store and handle benchmarking result: %v", err)
}
}
func main() {
flag.Parse()
clients, err := clientsFromConfig()
if err != nil {
log.Fatalf("Failed to setup clients: %v", err)
}
testScaleFromZero(clients, *parallelCount)
}