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 Oct 28, 2019
1 parent 2e19265 commit 4af2999
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
21 changes: 16 additions & 5 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,24 @@ 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")
flags.StringP(
"report-summary",
"r",
"",
"output summary to file in json format, empty string means output to standard out",
)
return flags
}

type Config struct {
lib.Options

Out []string `json:"out" envconfig:"K6_OUT"`
Linger null.Bool `json:"linger" envconfig:"K6_LINGER"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"K6_NO_USAGE_REPORT"`
NoThresholds null.Bool `json:"noThresholds" envconfig:"K6_NO_THRESHOLDS"`
NoSummary null.Bool `json:"noSummary" envconfig:"K6_NO_SUMMARY"`
Out []string `json:"out" envconfig:"K6_OUT"`
Linger null.Bool `json:"linger" envconfig:"K6_LINGER"`
NoUsageReport null.Bool `json:"noUsageReport" envconfig:"K6_NO_USAGE_REPORT"`
NoThresholds null.Bool `json:"noThresholds" envconfig:"K6_NO_THRESHOLDS"`
NoSummary null.Bool `json:"noSummary" envconfig:"K6_NO_SUMMARY"`
ReportSummary null.String `json:"reportSummary" envconfig:"report_summary"`

Collectors struct {
InfluxDB influxdb.Config `json:"influxdb"`
Expand Down Expand Up @@ -94,6 +101,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 @@ -120,6 +130,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
21 changes: 18 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 @@ -454,9 +455,23 @@ 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 {
logrus.Error("failed to create report file")
return 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 4af2999

Please sign in to comment.