Skip to content

Commit

Permalink
cmd: add --report-summary flag
Browse files Browse the repository at this point in the history
Add --report-summary flag for "k6 run", the parameter is output file to
write report summary in JSON format. Empty string means output to
standard out.

Close #355
  • Loading branch information
cuonglm committed Sep 23, 2019
1 parent bb81bf0 commit 5675864
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
17 changes: 12 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,20 @@ func configFlagSet() *pflag.FlagSet {
flags.Bool("no-usage-report", false, "don't send anonymous stats to the developers")
flags.Bool("no-thresholds", false, "don't run thresholds")
flags.Bool("no-summary", false, "don't show the summary at the end of the test")
reportSummaryUsage := "output summary to file in json format, empty string means output to standard out"
flags.StringP("report-summary", "r", "", reportSummaryUsage)
return flags
}

type Config struct {
lib.Options

Out []string `json:"out" envconfig:"out"`
Linger null.Bool `json:"linger" envconfig:"linger"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"no_usage_report"`
NoThresholds null.Bool `json:"noThresholds" envconfig:"no_thresholds"`
NoSummary null.Bool `json:"noSummary" envconfig:"no_summary"`
Out []string `json:"out" envconfig:"out"`
Linger null.Bool `json:"linger" envconfig:"linger"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"no_usage_report"`
NoThresholds null.Bool `json:"noThresholds" envconfig:"no_thresholds"`
NoSummary null.Bool `json:"noSummary" envconfig:"no_summary"`
ReportSummary null.String `json:"reportSummary" envconfig:"report_summary"`

Collectors struct {
InfluxDB influxdb.Config `json:"influxdb"`
Expand Down Expand Up @@ -93,6 +96,9 @@ func (c Config) Apply(cfg Config) Config {
if cfg.NoSummary.Valid {
c.NoSummary = cfg.NoSummary
}
if cfg.ReportSummary.Valid {
c.ReportSummary = cfg.ReportSummary
}
c.Collectors.InfluxDB = c.Collectors.InfluxDB.Apply(cfg.Collectors.InfluxDB)
c.Collectors.Cloud = c.Collectors.Cloud.Apply(cfg.Collectors.Cloud)
c.Collectors.Kafka = c.Collectors.Kafka.Apply(cfg.Collectors.Kafka)
Expand All @@ -119,6 +125,7 @@ func getConfig(flags *pflag.FlagSet) (Config, error) {
NoUsageReport: getNullBool(flags, "no-usage-report"),
NoThresholds: getNullBool(flags, "no-thresholds"),
NoSummary: getNullBool(flags, "no-summary"),
ReportSummary: getNullString(flags, "report-summary"),
}, nil
}

Expand Down
20 changes: 17 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -455,9 +456,22 @@ a commandline interface for interacting with it.`,
Metrics: engine.Metrics,
Time: engine.Executor.GetTime(),
}
// TODO: add json summary later
switch {
default:
if conf.ReportSummary.Valid {
var w io.Writer = stdout
if conf.ReportSummary.String != "" {
f, err := os.Create(conf.ReportSummary.String)
if err != nil {
panic(err)
}
w = f
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
}
ui.SummarizeJSON(w, data)
} else {
ui.Summarize(stdout, "", data)
}

Expand Down

0 comments on commit 5675864

Please sign in to comment.