Skip to content

Commit

Permalink
Add ability to display intermediate result while the test is running
Browse files Browse the repository at this point in the history
  • Loading branch information
andriisoldatenko committed Feb 9, 2021
1 parent a379c37 commit 04dc127
Show file tree
Hide file tree
Showing 14 changed files with 1,202 additions and 12 deletions.
69 changes: 57 additions & 12 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"syscall"
"time"

"github.com/eiannone/keyboard"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
Expand Down Expand Up @@ -251,6 +252,15 @@ a commandline interface for interacting with it.`,
os.Exit(externalAbortErrorCode)
}()

// start reading user input
if !runtimeOptions.NoSummary.Bool {
go func() {
err := readInput(globalCtx, initRunner, engine, executionState, logger)
if err != nil {
logger.Error(err)
}
}()
}
// Initialize the engine
initBar.Modify(pb.WithConstProgress(0, "Init VUs..."))
engineRun, engineWait, err := engine.Init(globalCtx, runCtx)
Expand Down Expand Up @@ -293,17 +303,7 @@ a commandline interface for interacting with it.`,

// Handle the end-of-test summary.
if !runtimeOptions.NoSummary.Bool {
summaryResult, err := initRunner.HandleSummary(globalCtx, &lib.Summary{
Metrics: engine.Metrics,
RootGroup: engine.ExecutionScheduler.GetRunner().GetDefaultGroup(),
TestRunDuration: executionState.GetCurrentTestRunDuration(),
})
if err == nil {
err = handleSummaryResult(afero.NewOsFs(), stdout, stderr, summaryResult)
}
if err != nil {
logger.WithError(err).Error("failed to handle the end-of-test summary")
}
printSummaryResults(globalCtx, initRunner, engine, executionState, logger)
}

if conf.Linger.Bool {
Expand Down Expand Up @@ -443,7 +443,7 @@ func handleSummaryResult(fs afero.Fs, stdOut, stdErr io.Writer, result map[strin
case "stderr":
return stdErr, nil
default:
return fs.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
return fs.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o666)
}
}

Expand All @@ -457,3 +457,48 @@ func handleSummaryResult(fs afero.Fs, stdOut, stdErr io.Writer, result map[strin

return consolidateErrorMessage(errs, "Could not save some summary information:")
}

func printSummaryResults(globalCtx context.Context, runner lib.Runner, engine *core.Engine, executionState *lib.ExecutionState, log *logrus.Logger) {
summaryResult, err := runner.HandleSummary(globalCtx, &lib.Summary{
Metrics: engine.Metrics,
RootGroup: engine.ExecutionScheduler.GetRunner().GetDefaultGroup(),
TestRunDuration: executionState.GetCurrentTestRunDuration(),
})
if err == nil {
err = handleSummaryResult(afero.NewOsFs(), stdout, stderr, summaryResult)
}
if err != nil {
log.WithError(err).Error("failed to handle the end-of-test summary")
}
}

func readInput(globalCtx context.Context, runner lib.Runner, engine *core.Engine, executionState *lib.ExecutionState, log *logrus.Logger) error {
keysEvents, err := keyboard.GetKeys(1)
if err != nil {
return err
}
defer func() {
_ = keyboard.Close()
}()
fmt.Println("Press R to get summary or esc to run Ctrl-C/Ctrl-Z")
// Starting loop...
forLoop:
for {
select {
case event := <-keysEvents:
if event.Err != nil {
return event.Err
}
if event.Rune == 'R' {
printSummaryResults(globalCtx, runner, engine, executionState, log)
}

if event.Key == keyboard.KeyEsc {
// Exiting loop!
break forLoop
}
break
}
}
return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/eapache/go-resiliency v1.1.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20160609142408-bb955e01b934 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
github.com/fatih/color v1.5.0
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/eapache/go-xerial-snappy v0.0.0-20160609142408-bb955e01b934 h1:oGLoaV
github.com/eapache/go-xerial-snappy v0.0.0-20160609142408-bb955e01b934/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 h1:jdjd5e68T4R/j4PWxfZqcKY8KtT9oo8IPNVuV4bSXDQ=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807/go.mod h1:Xoiu5VdKMvbRgHuY7+z64lhu/7lvax/22nzASF6GrO8=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/eiannone/keyboard/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/eiannone/keyboard/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions vendor/github.com/eiannone/keyboard/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions vendor/github.com/eiannone/keyboard/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 04dc127

Please sign in to comment.