forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
325 lines (296 loc) · 7.55 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
/*
Copyright The containerd 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"
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/plugin"
metrics "github.com/docker/go-metrics"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const imageName = "docker.io/library/alpine:latest"
var (
ct metrics.LabeledTimer
execTimer metrics.LabeledTimer
errCounter metrics.LabeledCounter
binarySizeGauge metrics.LabeledGauge
)
func init() {
ns := metrics.NewNamespace("stress", "", nil)
// if you want more fine grained metrics then you can drill down with the metrics in prom that
// containerd is outputting
ct = ns.NewLabeledTimer("run", "Run time of a full container during the test", "commit")
execTimer = ns.NewLabeledTimer("exec", "Run time of an exec process during the test", "commit")
binarySizeGauge = ns.NewLabeledGauge("binary_size", "Binary size of compiled binaries", metrics.Bytes, "name")
errCounter = ns.NewLabeledCounter("errors", "Errors encountered running the stress tests", "err")
metrics.Register(ns)
// set higher ulimits
if err := setRlimit(); err != nil {
panic(err)
}
}
type run struct {
total int
failures int
started time.Time
ended time.Time
}
func (r *run) start() {
r.started = time.Now()
}
func (r *run) end() {
r.ended = time.Now()
}
func (r *run) seconds() float64 {
return r.ended.Sub(r.started).Seconds()
}
func (r *run) gather(workers []*worker) *result {
for _, w := range workers {
r.total += w.count
r.failures += w.failures
}
sec := r.seconds()
return &result{
Total: r.total,
Seconds: sec,
ContainersPerSecond: float64(r.total) / sec,
SecondsPerContainer: sec / float64(r.total),
}
}
type result struct {
Total int `json:"total"`
Failures int `json:"failures"`
Seconds float64 `json:"seconds"`
ContainersPerSecond float64 `json:"containersPerSecond"`
SecondsPerContainer float64 `json:"secondsPerContainer"`
ExecTotal int `json:"execTotal"`
ExecFailures int `json:"execFailures"`
}
func main() {
// morr power!
runtime.GOMAXPROCS(runtime.NumCPU())
app := cli.NewApp()
app.Name = "containerd-stress"
app.Description = "stress test a containerd daemon"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "set debug output in the logs",
},
cli.StringFlag{
Name: "address,a",
Value: "/run/containerd/containerd.sock",
Usage: "path to the containerd socket",
},
cli.IntFlag{
Name: "concurrent,c",
Value: 1,
Usage: "set the concurrency of the stress test",
},
cli.DurationFlag{
Name: "duration,d",
Value: 1 * time.Minute,
Usage: "set the duration of the stress test",
},
cli.BoolFlag{
Name: "exec",
Usage: "add execs to the stress tests",
},
cli.BoolFlag{
Name: "json,j",
Usage: "output results in json format",
},
cli.StringFlag{
Name: "metrics,m",
Usage: "address to serve the metrics API",
},
cli.StringFlag{
Name: "runtime",
Usage: "set the runtime to stress test",
Value: plugin.RuntimeLinuxV1,
},
}
app.Before = func(context *cli.Context) error {
if context.GlobalBool("json") {
logrus.SetLevel(logrus.WarnLevel)
}
if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
app.Commands = []cli.Command{
densityCommand,
}
app.Action = func(context *cli.Context) error {
config := config{
Address: context.GlobalString("address"),
Duration: context.GlobalDuration("duration"),
Concurrency: context.GlobalInt("concurrent"),
Exec: context.GlobalBool("exec"),
JSON: context.GlobalBool("json"),
Metrics: context.GlobalString("metrics"),
Runtime: context.GlobalString("runtime"),
}
if config.Metrics != "" {
return serve(config)
}
return test(config)
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type config struct {
Concurrency int
Duration time.Duration
Address string
Exec bool
JSON bool
Metrics string
Runtime string
}
func (c config) newClient() (*containerd.Client, error) {
return containerd.New(c.Address, containerd.WithDefaultRuntime(c.Runtime))
}
func serve(c config) error {
go func() {
if err := http.ListenAndServe(c.Metrics, metrics.Handler()); err != nil {
logrus.WithError(err).Error("listen and serve")
}
}()
checkBinarySizes()
return test(c)
}
func test(c config) error {
var (
wg sync.WaitGroup
ctx = namespaces.WithNamespace(context.Background(), "stress")
)
client, err := c.newClient()
if err != nil {
return err
}
defer client.Close()
if err := cleanup(ctx, client); err != nil {
return err
}
logrus.Infof("pulling %s", imageName)
image, err := client.Pull(ctx, imageName, containerd.WithPullUnpack)
if err != nil {
return err
}
tctx, cancel := context.WithTimeout(ctx, c.Duration)
go func() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGTERM, syscall.SIGINT)
<-s
cancel()
}()
var (
workers []*worker
r = &run{}
)
logrus.Info("starting stress test run...")
v, err := client.Version(ctx)
if err != nil {
return err
}
// create the workers along with their spec
for i := 0; i < c.Concurrency; i++ {
wg.Add(1)
w := &worker{
id: i,
wg: &wg,
image: image,
client: client,
commit: v.Revision,
}
workers = append(workers, w)
}
var exec *execWorker
if c.Exec {
for i := c.Concurrency; i < c.Concurrency+c.Concurrency; i++ {
wg.Add(1)
exec = &execWorker{
worker: worker{
id: i,
wg: &wg,
image: image,
client: client,
commit: v.Revision,
},
}
go exec.exec(ctx, tctx)
}
}
// start the timer and run the worker
r.start()
for _, w := range workers {
go w.run(ctx, tctx)
}
// wait and end the timer
wg.Wait()
r.end()
results := r.gather(workers)
if c.Exec {
results.ExecTotal = exec.count
results.ExecFailures = exec.failures
}
logrus.Infof("ending test run in %0.3f seconds", results.Seconds)
logrus.WithField("failures", r.failures).Infof(
"create/start/delete %d containers in %0.3f seconds (%0.3f c/sec) or (%0.3f sec/c)",
results.Total,
results.Seconds,
results.ContainersPerSecond,
results.SecondsPerContainer,
)
if c.JSON {
if err := json.NewEncoder(os.Stdout).Encode(results); err != nil {
return err
}
}
return nil
}
// cleanup cleans up any containers in the "stress" namespace before the test run
func cleanup(ctx context.Context, client *containerd.Client) error {
containers, err := client.Containers(ctx)
if err != nil {
return err
}
for _, c := range containers {
task, err := c.Task(ctx, nil)
if err == nil {
task.Delete(ctx, containerd.WithProcessKill)
}
if err := c.Delete(ctx, containerd.WithSnapshotCleanup); err != nil {
if derr := c.Delete(ctx); derr == nil {
continue
}
return err
}
}
return nil
}