Skip to content

Commit

Permalink
cmd: make summary export works independent with no summary
Browse files Browse the repository at this point in the history
  • Loading branch information
cuonglm committed Oct 15, 2019
1 parent ba9d7d0 commit fc854f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
57 changes: 33 additions & 24 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ a commandline interface for interacting with it.`,
if conf.NoSummary.Valid {
engine.NoSummary = conf.NoSummary.Bool
}
if conf.SummaryExport.Valid {
engine.SummaryExport = conf.SummaryExport.String != ""
}

// Create a collector and assign it to the engine if requested.
fprintf(stdout, "%s collector\r", initBar.String())
Expand Down Expand Up @@ -446,36 +449,42 @@ a commandline interface for interacting with it.`,
logrus.Warn("No data generated, because no script iterations finished, consider making the test duration longer")
}

data := ui.SummaryData{
Opts: conf.Options,
Root: engine.Executor.GetRunner().GetDefaultGroup(),
Metrics: engine.Metrics,
Time: engine.Executor.GetTime(),
}

// Print the end-of-test summary.
if !conf.NoSummary.Bool {
fprintf(stdout, "\n")
data := ui.SummaryData{
Opts: conf.Options,
Root: engine.Executor.GetRunner().GetDefaultGroup(),
Metrics: engine.Metrics,
Time: engine.Executor.GetTime(),
}
if conf.SummaryExport.Valid {
var w io.Writer = stdout
if conf.SummaryExport.String != "" {
f, err := os.Create(conf.SummaryExport.String)
if err != nil {
logrus.Error("failed to create report file")
return err
}
w = f
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
ui.Summarize(stdout, "", data)
fprintf(stdout, "\n")
}

if conf.SummaryExport.Valid {
var w io.Writer
switch conf.SummaryExport.String {
case "": // disable, do nothing
case "/dev/stdout":
w = stdout
default:
f, err := os.Create(conf.SummaryExport.String)
if err != nil {
logrus.Error("failed to create report file")
return err
}
w = f
defer func() {
if err := f.Close(); err != nil {
logrus.WithError(err).Fatal("failed to close summary output file")
}
}()
}
if w != nil {
ui.SummarizeJSON(w, data)
} else {
ui.Summarize(stdout, "", data)
}

fprintf(stdout, "\n")
}

if conf.Linger.Bool {
Expand Down
13 changes: 7 additions & 6 deletions core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ const (
type Engine struct {
runLock sync.Mutex

Executor lib.Executor
Options lib.Options
Collectors []lib.Collector
NoThresholds bool
NoSummary bool
Executor lib.Executor
Options lib.Options
Collectors []lib.Collector
NoThresholds bool
NoSummary bool
SummaryExport bool

logger *logrus.Logger

Expand Down Expand Up @@ -386,7 +387,7 @@ func (e *Engine) processSamples(sampleContainers []stats.SampleContainer) {
defer e.MetricsLock.Unlock()

// TODO: run this and the below code in goroutines?
if !(e.NoSummary && e.NoThresholds) {
if !(e.NoSummary && e.NoThresholds && !e.SummaryExport) {
e.processSamplesForMetrics(sampleContainers)
}

Expand Down

0 comments on commit fc854f2

Please sign in to comment.