Add load tester flag to log the cmd output

This commit is contained in:
stefanprodan
2019-01-21 13:36:08 +02:00
parent acc72d207f
commit dbf26ddf53
3 changed files with 16 additions and 7 deletions
+8 -5
View File
@@ -9,16 +9,19 @@ import (
"time"
)
var VERSION = "0.0.2"
var (
logLevel string
port string
timeout time.Duration
logLevel string
port string
timeout time.Duration
logCmdOutput bool
)
func init() {
flag.StringVar(&logLevel, "log-level", "debug", "Log level can be: debug, info, warning, error.")
flag.StringVar(&port, "port", "9090", "Port to listen on.")
flag.DurationVar(&timeout, "timeout", time.Hour, "Command exec timeout.")
flag.BoolVar(&logCmdOutput, "log-cmd-output", true, "Log command output to stderr")
}
func main() {
@@ -32,10 +35,10 @@ func main() {
stopCh := signals.SetupSignalHandler()
taskRunner := loadtester.NewTaskRunner(logger, timeout)
taskRunner := loadtester.NewTaskRunner(logger, timeout, logCmdOutput)
go taskRunner.Start(100*time.Millisecond, stopCh)
logger.Infof("Starting HTTP server on port %s", port)
logger.Infof("Starting load tester v%s API on port %s", VERSION, port)
loadtester.ListenAndServe(port, time.Minute, logger, taskRunner, stopCh)
}
+7 -1
View File
@@ -3,6 +3,7 @@ package loadtester
import (
"context"
"encoding/hex"
"fmt"
"go.uber.org/zap"
"hash/fnv"
"os/exec"
@@ -17,6 +18,7 @@ type TaskRunner struct {
todoTasks *sync.Map
runningTasks *sync.Map
totalExecs uint64
logCmdOutput bool
}
type Task struct {
@@ -30,12 +32,13 @@ func (t Task) Hash() string {
return hex.EncodeToString(fnvBytes[:])
}
func NewTaskRunner(logger *zap.SugaredLogger, timeout time.Duration) *TaskRunner {
func NewTaskRunner(logger *zap.SugaredLogger, timeout time.Duration, logCmdOutput bool) *TaskRunner {
return &TaskRunner{
logger: logger,
todoTasks: new(sync.Map),
runningTasks: new(sync.Map),
timeout: timeout,
logCmdOutput: logCmdOutput,
}
}
@@ -74,6 +77,9 @@ func (tr *TaskRunner) runAll() {
if err != nil {
tr.logger.With("canary", t.Canary).Errorf("command failed %s %v %s", t.Command, err, out)
} else {
if tr.logCmdOutput {
fmt.Printf("%s\n", out)
}
tr.logger.With("canary", t.Canary).Infof("command finished %s", t.Command)
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func TestTaskRunner_Start(t *testing.T) {
stop := make(chan struct{})
logger, _ := logging.NewLogger("debug")
tr := NewTaskRunner(logger, time.Hour)
tr := NewTaskRunner(logger, time.Hour, false)
go tr.Start(10*time.Millisecond, stop)