From 8b16c126a34e08fcfcc43c98ebb032c42686cf40 Mon Sep 17 00:00:00 2001 From: Shmulik Regev Date: Wed, 9 Feb 2022 18:29:21 +0200 Subject: [PATCH] Added a summary flag to skip the live printing to the terminal --- main.go | 18 ++++++++++++------ print.go | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 3c1ef3c..57f4f89 100644 --- a/main.go +++ b/main.go @@ -36,8 +36,8 @@ var ( autoOpenBrowser = kingpin.Flag("auto-open-browser", "Specify whether auto open browser to show Web charts").Bool() clean = kingpin.Flag("clean", "Clean the histogram bar once its finished. Default is true").Default("true").NegatableBool() - - url = kingpin.Arg("url", "request url").Required().String() + summary = kingpin.Flag("summary", "Only print the summary without realtime reports").Default("false").NegatableBool() + url = kingpin.Arg("url", "request url").Required().String() ) func errAndExit(msg string) { @@ -166,6 +166,11 @@ func main() { return } + outStream := os.Stdout + if *summary { + outStream = os.Stderr + isTerminal = false + } // description var desc string desc = fmt.Sprintf("Benchmarking %s", *url) @@ -176,7 +181,7 @@ func main() { desc += fmt.Sprintf(" for %s", duration.String()) } desc += fmt.Sprintf(" using %d connection(s).", *concurrency) - fmt.Println(desc) + fmt.Fprintln(outStream,desc) // charts listener var ln net.Listener @@ -186,9 +191,9 @@ func main() { errAndExit(err.Error()) return } - fmt.Printf("@ Real-time charts is listening on http://%s\n", ln.Addr().String()) + fmt.Fprintln(outStream,"@ Real-time charts is listening on http://%s", ln.Addr().String()) } - fmt.Printf("\n") + fmt.Fprintln(outStream,"") // do request go requester.Run() @@ -208,6 +213,7 @@ func main() { } // terminal printer - printer := NewPrinter(*requests, *duration, !*clean) + printer := NewPrinter(*requests, *duration, !*clean, *summary) printer.PrintLoop(report.Snapshot, *interval, *seconds, report.Done()) + } diff --git a/print.go b/print.go index 705e407..37411cd 100644 --- a/print.go +++ b/print.go @@ -33,10 +33,11 @@ type Printer struct { pbNumStr string pbDurStr string noClean bool + summary bool } -func NewPrinter(maxNum int64, maxDuration time.Duration, noCleanBar bool) *Printer { - return &Printer{maxNum: maxNum, maxDuration: maxDuration, noClean: noCleanBar} +func NewPrinter(maxNum int64, maxDuration time.Duration, noCleanBar, summary bool) *Printer { + return &Printer{maxNum: maxNum, maxDuration: maxDuration, noClean: noCleanBar, summary: summary} } func (p *Printer) updateProgressValue(rs *SnapshotReport) { @@ -66,6 +67,10 @@ func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Durat var buf bytes.Buffer var backCursor string + cl := clearLine + if p.summary { + cl = nil + } echo := func(isFinal bool) { report := snapshot() p.updateProgressValue(report) @@ -77,12 +82,12 @@ func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Durat for { i := bytes.IndexByte(result, '\n') if i == -1 { - os.Stdout.Write(clearLine) + os.Stdout.Write(cl) os.Stdout.Write(result) break } n++ - os.Stdout.Write(clearLine) + os.Stdout.Write(cl) os.Stdout.Write(result[:i]) os.Stdout.Write([]byte("\n")) result = result[i+1:] @@ -97,7 +102,9 @@ func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Durat for { select { case <-ticker.C: - echo(false) + if !p.summary { + echo(false) + } case <-doneChan: ticker.Stop() break loop