Add json output to stress test tool
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
6393165b09
commit
9fcca96771
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -25,6 +26,47 @@ import (
|
|||||||
|
|
||||||
const imageName = "docker.io/library/alpine:latest"
|
const imageName = "docker.io/library/alpine:latest"
|
||||||
|
|
||||||
|
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"`
|
||||||
|
Seconds float64 `json:"seconds"`
|
||||||
|
ContainersPerSecond float64 `json:"containersPerSecond"`
|
||||||
|
SecondsPerContainer float64 `json:"secondsPerContainer"`
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// morr power!
|
// morr power!
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
@ -56,11 +98,18 @@ func main() {
|
|||||||
Name: "exec",
|
Name: "exec",
|
||||||
Usage: "add execs to the stress tests",
|
Usage: "add execs to the stress tests",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "json,j",
|
||||||
|
Usage: "output results in json format",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
app.Before = func(context *cli.Context) error {
|
app.Before = func(context *cli.Context) error {
|
||||||
if context.GlobalBool("debug") {
|
if context.GlobalBool("debug") {
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
}
|
}
|
||||||
|
if context.GlobalBool("json") {
|
||||||
|
logrus.SetLevel(logrus.WarnLevel)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
app.Action = func(context *cli.Context) error {
|
app.Action = func(context *cli.Context) error {
|
||||||
@ -69,6 +118,7 @@ func main() {
|
|||||||
Duration: context.GlobalDuration("duration"),
|
Duration: context.GlobalDuration("duration"),
|
||||||
Concurrency: context.GlobalInt("concurrent"),
|
Concurrency: context.GlobalInt("concurrent"),
|
||||||
Exec: context.GlobalBool("exec"),
|
Exec: context.GlobalBool("exec"),
|
||||||
|
Json: context.GlobalBool("json"),
|
||||||
}
|
}
|
||||||
return test(config)
|
return test(config)
|
||||||
}
|
}
|
||||||
@ -83,6 +133,7 @@ type config struct {
|
|||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
Address string
|
Address string
|
||||||
Exec bool
|
Exec bool
|
||||||
|
Json bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c config) newClient() (*containerd.Client, error) {
|
func (c config) newClient() (*containerd.Client, error) {
|
||||||
@ -119,13 +170,14 @@ func test(c config) error {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
workers []*worker
|
workers []*worker
|
||||||
start = time.Now()
|
r = &run{}
|
||||||
)
|
)
|
||||||
logrus.Info("starting stress test run...")
|
logrus.Info("starting stress test run...")
|
||||||
args := oci.WithProcessArgs("true")
|
args := oci.WithProcessArgs("true")
|
||||||
if c.Exec {
|
if c.Exec {
|
||||||
args = oci.WithProcessArgs("sleep", "10")
|
args = oci.WithProcessArgs("sleep", "10")
|
||||||
}
|
}
|
||||||
|
// create the workers along with their spec
|
||||||
for i := 0; i < c.Concurrency; i++ {
|
for i := 0; i < c.Concurrency; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
spec, err := oci.GenerateSpec(ctx, client,
|
spec, err := oci.GenerateSpec(ctx, client,
|
||||||
@ -145,27 +197,31 @@ func test(c config) error {
|
|||||||
doExec: c.Exec,
|
doExec: c.Exec,
|
||||||
}
|
}
|
||||||
workers = append(workers, w)
|
workers = append(workers, w)
|
||||||
|
}
|
||||||
|
// start the timer and run the worker
|
||||||
|
r.start()
|
||||||
|
for _, w := range workers {
|
||||||
go w.run(ctx, tctx)
|
go w.run(ctx, tctx)
|
||||||
}
|
}
|
||||||
|
// wait and end the timer
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
r.end()
|
||||||
|
|
||||||
var (
|
results := r.gather(workers)
|
||||||
total int
|
logrus.Infof("ending test run in %0.3f seconds", results.Seconds)
|
||||||
failures int
|
|
||||||
end = time.Now().Sub(start).Seconds()
|
logrus.WithField("failures", r.failures).Infof(
|
||||||
)
|
|
||||||
logrus.Infof("ending test run in %0.3f seconds", end)
|
|
||||||
for _, w := range workers {
|
|
||||||
total += w.count
|
|
||||||
failures += w.failures
|
|
||||||
}
|
|
||||||
logrus.WithField("failures", failures).Infof(
|
|
||||||
"create/start/delete %d containers in %0.3f seconds (%0.3f c/sec) or (%0.3f sec/c)",
|
"create/start/delete %d containers in %0.3f seconds (%0.3f c/sec) or (%0.3f sec/c)",
|
||||||
total,
|
results.Total,
|
||||||
end,
|
results.Seconds,
|
||||||
float64(total)/end,
|
results.ContainersPerSecond,
|
||||||
end/float64(total),
|
results.SecondsPerContainer,
|
||||||
)
|
)
|
||||||
|
if c.Json {
|
||||||
|
if err := json.NewEncoder(os.Stdout).Encode(results); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user