Add prom timer to stress

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-12-12 13:10:30 -05:00
parent ca5f16c33e
commit 4d55298aab
2 changed files with 40 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -15,6 +16,7 @@ import (
"github.com/containerd/containerd/containers" "github.com/containerd/containerd/containers"
"github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci" "github.com/containerd/containerd/oci"
metrics "github.com/docker/go-metrics"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -97,6 +99,10 @@ func main() {
Name: "json,j", Name: "json,j",
Usage: "output results in json format", Usage: "output results in json format",
}, },
cli.StringFlag{
Name: "metrics,m",
Usage: "address to serve the metrics API",
},
} }
app.Before = func(context *cli.Context) error { app.Before = func(context *cli.Context) error {
if context.GlobalBool("debug") { if context.GlobalBool("debug") {
@ -113,7 +119,11 @@ 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"), JSON: context.GlobalBool("json"),
Metrics: context.GlobalString("metrics"),
}
if config.Metrics != "" {
return serve(config)
} }
return test(config) return test(config)
} }
@ -128,13 +138,23 @@ type config struct {
Duration time.Duration Duration time.Duration
Address string Address string
Exec bool Exec bool
Json bool JSON bool
Metrics string
} }
func (c config) newClient() (*containerd.Client, error) { func (c config) newClient() (*containerd.Client, error) {
return containerd.New(c.Address) return containerd.New(c.Address)
} }
func serve(c config) error {
go func() {
if err := http.ListenAndServe(c.Metrics, metrics.Handler()); err != nil {
logrus.WithError(err).Error("listen and serve")
}
}()
return test(c)
}
func test(c config) error { func test(c config) error {
var ( var (
wg sync.WaitGroup wg sync.WaitGroup
@ -212,7 +232,7 @@ func test(c config) error {
results.ContainersPerSecond, results.ContainersPerSecond,
results.SecondsPerContainer, results.SecondsPerContainer,
) )
if c.Json { if c.JSON {
if err := json.NewEncoder(os.Stdout).Encode(results); err != nil { if err := json.NewEncoder(os.Stdout).Encode(results); err != nil {
return err return err
} }

View File

@ -8,14 +8,26 @@ import (
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
"time"
"github.com/Sirupsen/logrus"
"github.com/containerd/containerd" "github.com/containerd/containerd"
"github.com/containerd/containerd/cio" "github.com/containerd/containerd/cio"
"github.com/containerd/containerd/oci" "github.com/containerd/containerd/oci"
metrics "github.com/docker/go-metrics"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
) )
var ct metrics.Timer
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 outputing
ct = ns.NewTimer("run", "Run time of a full container during the test")
metrics.Register(ns)
}
type worker struct { type worker struct {
id int id int
wg *sync.WaitGroup wg *sync.WaitGroup
@ -43,6 +55,7 @@ func (w *worker) run(ctx, tctx context.Context) {
w.count++ w.count++
id := w.getID() id := w.getID()
logrus.Debugf("starting container %s", id) logrus.Debugf("starting container %s", id)
start := time.Now()
if err := w.runContainer(ctx, id); err != nil { if err := w.runContainer(ctx, id); err != nil {
if err != context.DeadlineExceeded || if err != context.DeadlineExceeded ||
!strings.Contains(err.Error(), context.DeadlineExceeded.Error()) { !strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
@ -50,7 +63,10 @@ func (w *worker) run(ctx, tctx context.Context) {
logrus.WithError(err).Errorf("running container %s", id) logrus.WithError(err).Errorf("running container %s", id)
} }
continue
} }
// only log times are success so we don't scew the results from failures that go really fast
ct.UpdateSince(start)
} }
} }