Add exec support to stress test tool
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
2556c594ec
commit
723f37d846
@ -7,6 +7,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -51,6 +52,10 @@ func main() {
|
|||||||
Value: 1 * time.Minute,
|
Value: 1 * time.Minute,
|
||||||
Usage: "set the duration of the stress test",
|
Usage: "set the duration of the stress test",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "exec",
|
||||||
|
Usage: "add execs to the stress tests",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
app.Before = func(context *cli.Context) error {
|
app.Before = func(context *cli.Context) error {
|
||||||
if context.GlobalBool("debug") {
|
if context.GlobalBool("debug") {
|
||||||
@ -63,6 +68,7 @@ func main() {
|
|||||||
Address: context.GlobalString("address"),
|
Address: context.GlobalString("address"),
|
||||||
Duration: context.GlobalDuration("duration"),
|
Duration: context.GlobalDuration("duration"),
|
||||||
Concurrency: context.GlobalInt("concurrent"),
|
Concurrency: context.GlobalInt("concurrent"),
|
||||||
|
Exec: context.GlobalBool("exec"),
|
||||||
}
|
}
|
||||||
return test(config)
|
return test(config)
|
||||||
}
|
}
|
||||||
@ -76,6 +82,7 @@ type config struct {
|
|||||||
Concurrency int
|
Concurrency int
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
Address string
|
Address string
|
||||||
|
Exec bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c config) newClient() (*containerd.Client, error) {
|
func (c config) newClient() (*containerd.Client, error) {
|
||||||
@ -115,12 +122,17 @@ func test(c config) error {
|
|||||||
start = time.Now()
|
start = time.Now()
|
||||||
)
|
)
|
||||||
logrus.Info("starting stress test run...")
|
logrus.Info("starting stress test run...")
|
||||||
|
args := oci.WithProcessArgs("true")
|
||||||
|
if c.Exec {
|
||||||
|
args = oci.WithProcessArgs("sleep", "10")
|
||||||
|
}
|
||||||
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,
|
||||||
&containers.Container{},
|
&containers.Container{},
|
||||||
oci.WithImageConfig(image),
|
oci.WithImageConfig(image),
|
||||||
oci.WithProcessArgs("true"))
|
args,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -130,6 +142,7 @@ func test(c config) error {
|
|||||||
spec: spec,
|
spec: spec,
|
||||||
image: image,
|
image: image,
|
||||||
client: client,
|
client: client,
|
||||||
|
doExec: c.Exec,
|
||||||
}
|
}
|
||||||
workers = append(workers, w)
|
workers = append(workers, w)
|
||||||
go w.run(ctx, tctx)
|
go w.run(ctx, tctx)
|
||||||
@ -157,15 +170,15 @@ func test(c config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type worker struct {
|
type worker struct {
|
||||||
id int
|
id int
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
count int
|
count int
|
||||||
failures int
|
failures int
|
||||||
waitContext context.Context
|
|
||||||
|
|
||||||
client *containerd.Client
|
client *containerd.Client
|
||||||
image containerd.Image
|
image containerd.Image
|
||||||
spec *specs.Spec
|
spec *specs.Spec
|
||||||
|
doExec bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *worker) run(ctx, tctx context.Context) {
|
func (w *worker) run(ctx, tctx context.Context) {
|
||||||
@ -173,12 +186,6 @@ func (w *worker) run(ctx, tctx context.Context) {
|
|||||||
w.wg.Done()
|
w.wg.Done()
|
||||||
logrus.Infof("worker %d finished", w.id)
|
logrus.Infof("worker %d finished", w.id)
|
||||||
}()
|
}()
|
||||||
wctx, cancel := context.WithCancel(ctx)
|
|
||||||
w.waitContext = wctx
|
|
||||||
go func() {
|
|
||||||
<-tctx.Done()
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tctx.Done():
|
case <-tctx.Done():
|
||||||
@ -222,10 +229,20 @@ func (w *worker) runContainer(ctx context.Context, id string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := task.Start(ctx); err != nil {
|
if err := task.Start(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if w.doExec {
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
if err := w.exec(ctx, i, task); err != nil {
|
||||||
|
w.failures++
|
||||||
|
logrus.WithError(err).Error("exec failure")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
status := <-statusC
|
status := <-statusC
|
||||||
_, _, err = status.Result()
|
_, _, err = status.Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -237,6 +254,25 @@ func (w *worker) runContainer(ctx context.Context, id string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *worker) exec(ctx context.Context, i int, t containerd.Task) error {
|
||||||
|
pSpec := *w.spec.Process
|
||||||
|
pSpec.Args = []string{"true"}
|
||||||
|
process, err := t.Exec(ctx, strconv.Itoa(i), &pSpec, cio.NullIO)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer process.Delete(ctx)
|
||||||
|
status, err := process.Wait(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := process.Start(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
<-status
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (w *worker) getID() string {
|
func (w *worker) getID() string {
|
||||||
return fmt.Sprintf("%d-%d", w.id, w.count)
|
return fmt.Sprintf("%d-%d", w.id, w.count)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user