Skip to content

Commit

Permalink
Add text report text-errors flag
Browse files Browse the repository at this point in the history
Closes #566

Signed-off-by: Tomás Senart <tsenart@gmail.com>
  • Loading branch information
muff1nman authored and tsenart committed Jul 15, 2023
1 parent dd4d39f commit 8bd8a18
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ require (
require (
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c // indirect
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c h1:aqg5Vm5dwtvL+YgDpBcK1ITf3o96N/K7/wsRXQnUTEs=
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c/go.mod h1:owqhoLW1qZoYLZzLnBw+QkPP9WZnjlSWihhxAJC1+/M=
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92 h1:OfRzdxCzDhp+rsKWXuOO2I/quKMJ/+TQwVbIP/gltZg=
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92/go.mod h1:7/OT02F6S6I7v6WXb+IjhMuZEYfH/RJ5RwEWnEo5BMg=
github.com/streadway/quantile v0.0.0-20220407130108-4246515d968d h1:X4+kt6zM/OVO6gbJdAfJR60MGPsqCzbtXNnjoGqdfAs=
github.com/streadway/quantile v0.0.0-20220407130108-4246515d968d/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
36 changes: 29 additions & 7 deletions lib/reporters.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,24 @@ func NewHistogramReporter(h *Histogram) Reporter {
}
}

// TextReporterOpt is a functional option for configuring a TextReporter.
type TextReporterOpt func(*textReporterOpts)

// TextReporterErrors configures a TextReporter to write out potentially long errors.
// True by default.
func TextReporterErrors(v bool) TextReporterOpt {
return func(o *textReporterOpts) {
o.errors = v
}
}

type textReporterOpts struct {
errors bool
}

// NewTextReporter returns a Reporter that writes out Metrics as aligned,
// formatted text.
func NewTextReporter(m *Metrics) Reporter {
func NewTextReporter(m *Metrics, options ...TextReporterOpt) Reporter {
const fmtstr = "Requests\t[total, rate, throughput]\t%d, %.2f, %.2f\n" +
"Duration\t[total, attack, wait]\t%s, %s, %s\n" +
"Latencies\t[min, mean, 50, 90, 95, 99, max]\t%s, %s, %s, %s, %s, %s, %s\n" +
Expand All @@ -63,6 +78,11 @@ func NewTextReporter(m *Metrics) Reporter {
"Success\t[ratio]\t%.2f%%\n" +
"Status Codes\t[code:count]\t"

opts := textReporterOpts{errors: true}
for _, opt := range options {
opt(&opts)
}

return func(w io.Writer) (err error) {
tw := tabwriter.NewWriter(w, 0, 8, 2, ' ', tabwriter.StripEscape)
if _, err = fmt.Fprintf(tw, fmtstr,
Expand Down Expand Up @@ -98,14 +118,16 @@ func NewTextReporter(m *Metrics) Reporter {
}
}

if _, err = fmt.Fprintln(tw, "\nError Set:"); err != nil {
return err
}

for _, e := range m.Errors {
if _, err = fmt.Fprintln(tw, e); err != nil {
if opts.errors {
if _, err = fmt.Fprintln(tw, "\nError Set:"); err != nil {
return err
}

for _, e := range m.Errors {
if _, err = fmt.Fprintln(tw, e); err != nil {
return err
}
}
}

return tw.Flush()
Expand Down
7 changes: 4 additions & 3 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func reportCmd() command {
every := fs.Duration("every", 0, "Report interval")
output := fs.String("output", "stdout", "Output file")
buckets := fs.String("buckets", "", "Histogram buckets, e.g.: \"[0,1ms,10ms]\"")
textErrors := fs.Bool("text-errors", true, "Show errors in the text report")

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "%s\n", reportUsage)
Expand All @@ -53,11 +54,11 @@ func reportCmd() command {
if len(files) == 0 {
files = append(files, "stdin")
}
return report(files, *typ, *output, *every, *buckets)
return report(files, *typ, *output, *every, *buckets, *textErrors)
}}
}

func report(files []string, typ, output string, every time.Duration, bucketsStr string) error {
func report(files []string, typ, output string, every time.Duration, bucketsStr string, textErrors bool) error {
if len(typ) < 4 {
return fmt.Errorf("invalid report type: %s", typ)
}
Expand All @@ -84,7 +85,7 @@ func report(files []string, typ, output string, every time.Duration, bucketsStr
return fmt.Errorf("The plot reporter has been deprecated and succeeded by the vegeta plot command")
case "text":
var m vegeta.Metrics
rep, report = vegeta.NewTextReporter(&m), &m
rep, report = vegeta.NewTextReporter(&m, vegeta.TextReporterErrors(textErrors)), &m
case "json":
var m vegeta.Metrics
if bucketsStr != "" {
Expand Down

0 comments on commit 8bd8a18

Please sign in to comment.