From 8bd8a188529d9e0467307d6d9724180b85df0ad6 Mon Sep 17 00:00:00 2001 From: Andrew DeMaria <ademaria@cloudflare.com> Date: Tue, 27 Apr 2021 14:09:47 -0600 Subject: [PATCH] Add text report text-errors flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #566 Signed-off-by: Tomás Senart <tsenart@gmail.com> --- go.mod | 2 ++ go.sum | 4 ++++ lib/reporters.go | 36 +++++++++++++++++++++++++++++------- report.go | 7 ++++--- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index a7d3e530..9dd57f03 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9967e4ae..9d8494a5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/lib/reporters.go b/lib/reporters.go index aa7e4251..e6bcac46 100644 --- a/lib/reporters.go +++ b/lib/reporters.go @@ -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" + @@ -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, @@ -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() diff --git a/report.go b/report.go index e6ed6d6d..5f5b1f6d 100644 --- a/report.go +++ b/report.go @@ -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) @@ -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) } @@ -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 != "" {