-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main.go
369 lines (321 loc) · 11.4 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright 2022 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"
"os"
"strconv"
"sync"
"testing"
"time"
vegeta "github.com/tsenart/vegeta/v12/lib"
v1 "k8s.io/api/apps/v1"
netapi "knative.dev/networking/pkg/apis/networking"
"knative.dev/pkg/environment"
"knative.dev/pkg/injection"
"knative.dev/pkg/signals"
"knative.dev/serving/test/performance/performance"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/watch"
"knative.dev/pkg/test/spoof"
"knative.dev/serving/pkg/apis/serving"
"golang.org/x/sync/errgroup"
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"
)
const (
benchmarkName = "Knative Serving scale from zero"
namespace = "default"
serviceName = "perftest-scalefromzero"
helloWorldExpectedOutput = "Hello World!"
helloWorldImage = "helloworld"
waitToServe = 5 * time.Minute
)
var (
parallelCount = flag.Int("parallel", 0, "The count of ksvcs we want to run scale-from-zero in parallel")
// Map the above to our benchmark targets.
slas = map[int]struct {
p95min time.Duration
p95max time.Duration
latencyMax time.Duration
}{
1: {
p95min: 0,
p95max: 50 * time.Millisecond,
latencyMax: 50 * time.Millisecond,
},
5: {
p95min: 0,
p95max: 50 * time.Millisecond,
latencyMax: 50 * time.Millisecond,
},
25: {
// Scaling 25 services in parallel will hit some API limits, which cause a step
// in the deployment updated time which adds to the total time until a service is ready
p95min: 0,
p95max: 2 * time.Second,
latencyMax: 5 * time.Second,
},
100: {
// Scaling 100 services in parallel will hit some API limits, which cause a step
// in the deployment updated time which adds to the total time until a service is ready
p95min: 0,
p95max: 25 * time.Second,
latencyMax: 25 * time.Second,
},
}
)
func main() {
log.Println("Starting scale from zero test")
ctx := signals.NewContext()
ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
defer cancel()
// To make testing.T work properly
testing.Init()
env := environment.ClientConfig{}
// The local domain is directly resolvable by the test
flag.Set("resolvabledomain", "true")
flag.Parse()
cfg, err := env.GetRESTConfig()
if err != nil {
log.Fatalf("failed to get kubeconfig %s", err)
}
ctx, _ = injection.EnableInjectionOrDie(ctx, cfg)
clients, err := test.NewClients(cfg, namespace)
if err != nil {
log.Fatal("Failed to setup clients: ", err)
}
influxReporter, err := performance.NewInfluxReporter(map[string]string{"parallel": strconv.Itoa(*parallelCount)})
if err != nil {
log.Fatalf("Failed to create InfluxReporter: %v", err)
}
defer influxReporter.FlushAndShutdown()
// We use vegeta.Metrics here as a metrics collector because it already contains logic to calculate percentiles
vegetaReporter := performance.NewVegetaReporter()
// Create the services once.
services, cleanup, err := createServices(clients, *parallelCount)
if err != nil {
log.Fatalf("Failed to create services: %v", err)
}
defer cleanup()
// Wrap fatalf in a helper to clean up created resources
fatalf := func(f string, args ...interface{}) {
cleanup()
vegetaReporter.StopAndCollectMetrics()
log.Fatalf(f, args...)
}
// Wait all services scaling to zero.
if err := waitForScaleToZero(ctx, services); err != nil {
fatalf("Failed to wait for all services to scale to zero: %v", err)
}
parallelScaleFromZero(ctx, clients, services, influxReporter, vegetaReporter)
metricResults := vegetaReporter.StopAndCollectMetrics()
// Report the results
influxReporter.AddDataPointsForMetrics(metricResults, benchmarkName)
_ = vegeta.NewTextReporter(metricResults).Report(os.Stdout)
sla := slas[*parallelCount]
if err := checkSLA(metricResults, sla.p95min, sla.p95max, sla.latencyMax, *parallelCount); err != nil {
// make sure to still write the stats
influxReporter.FlushAndShutdown()
log.Fatalf(err.Error())
}
log.Println("Scale from zero test completed")
}
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",
}),
ktest.WithServiceLabel(netapi.VisibilityLabelKey, serving.VisibilityClusterLocal),
}
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: %w", ndx, err)
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, nil, err
}
log.Print("Created all the services in ", 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, namespace, 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, reporter *performance.InfluxReporter, vegetaReporter *performance.VegetaReporter) {
count := len(objs)
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
ndx := i
go func() {
defer wg.Done()
serviceReadyDuration, deploymentUpdatedDuration, err := runScaleFromZero(ctx, clients, ndx, objs[ndx])
if err == nil {
vegetaReporter.AddResult(&vegeta.Result{Latency: serviceReadyDuration})
reporter.AddDataPoint(benchmarkName, map[string]interface{}{
"service-ready-latency": serviceReadyDuration.Milliseconds(),
})
reporter.AddDataPoint(benchmarkName, map[string]interface{}{
"deployment-updated-latency": deploymentUpdatedDuration.Milliseconds(),
})
} else {
// Add 1 to the error metric whenever there is an error.
reporter.AddDataPoint(benchmarkName, map[string]interface{}{
"errors": 1,
})
}
}()
}
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.AppsV1().Deployments(namespace).Watch(
ctx, metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
msg := fmt.Sprintf("%02d: unable to watch the deployment for the service: %v", idx, err)
log.Println(msg)
return 0, 0, errors.New(msg)
}
defer watcher.Stop()
deploymentChangeChan := watcher.ResultChan()
serviceReadyChan := make(chan struct{})
errorChan := make(chan error)
go func() {
log.Printf("%02d: waiting for endpoint to serve request", idx)
url := ro.Route.Status.URL.URL()
_, err := pkgTest.WaitForEndpointStateWithTimeout(
ctx,
clients.KubeClient,
log.Printf,
url,
spoof.MatchesAllOf(spoof.IsStatusOK, spoof.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)
errorChan <- errors.New(m)
return
}
serviceReadyChan <- struct{}{}
}()
start := time.Now()
// Get the duration that takes to change deployment spec.
var dd time.Duration
for {
select {
case event := <-deploymentChangeChan:
if event.Type == watch.Modified {
dm := event.Object.(*v1.Deployment)
if *dm.Spec.Replicas != 0 && dd == 0 {
dd = time.Since(start)
}
}
case <-serviceReadyChan:
since := time.Since(start)
log.Printf("Service is ready after: name: %s, deployment-updated: %vms, service-ready-since-deployment: %vms, service-ready-total: %vms",
ro.Service.Name, dd.Milliseconds(), (since - dd).Milliseconds(), since.Milliseconds())
return since, dd, nil
case err := <-errorChan:
log.Println("Service scaling failed: ", ro.Service.Name, err.Error())
return 0, 0, err
}
}
}
func checkSLA(results *vegeta.Metrics, p95min time.Duration, p95max time.Duration, latencyMax time.Duration, parallel int) error {
// SLA 1: The p95 latency hitting the target has to be between the range defined
if results.Latencies.P95 >= p95min && results.Latencies.P95 <= p95max {
log.Printf("SLA 1 passed. P95 latency is in %d-%dms time range", p95min, p95max)
} else {
return fmt.Errorf("SLA 1 failed. P95 latency is not in %d-%dms time range: %s", p95min, p95max, results.Latencies.P95)
}
// SLA 2: The max latency hitting the target has to be between the range defined
if results.Latencies.Max <= latencyMax {
log.Printf("SLA 2 passed. Max latency is below or equal to %dms", latencyMax)
} else {
return fmt.Errorf("SLA 2 failed. Max latency is higher than %dms: %s", latencyMax, results.Latencies.Max)
}
// SLA 3: making sure the defined vegeta total requests is met, the defined vegeta total requests should equal to the count of ksvcs we want to run scale-from-zero in parallel
if results.Requests == uint64(parallel) {
log.Printf("SLA 3 passed. total requests is %d", results.Requests)
} else {
return fmt.Errorf("SLA 3 failed. total requests is %d, expected total requests is %d", results.Requests, uint64(parallel))
}
return nil
}