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

ui: fix linter
  • Loading branch information
cuonglm committed Sep 27, 2019
1 parent 2899fca commit d426d9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 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
6 changes: 3 additions & 3 deletions ui/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,13 @@ func Summarize(w io.Writer, indent string, data SummaryData) {
summarizeMetrics(w, indent+" ", data.Time, data.Opts.SummaryTimeUnit.String, data.Metrics)
}

func newJsonEncoder(w io.Writer) *json.Encoder {
func newJSONEncoder(w io.Writer) *json.Encoder {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
return encoder
}
func summarizeGroupJSON(w io.Writer, group *lib.Group) {
encoder := newJsonEncoder(w)
encoder := newJSONEncoder(w)
if err := encoder.Encode(group); err != nil {
panic(err)
}
Expand All @@ -423,7 +423,7 @@ func summarizeMetricsJSON(w io.Writer, t time.Duration, timeUnit string, metrics
}
}

encoder := newJsonEncoder(w)
encoder := newJSONEncoder(w)
if err := encoder.Encode(data); err != nil {
panic(err)
}
Expand Down

0 comments on commit d426d9b

Please sign in to comment.