Added a summary flag to skip the live printing to the terminal

This commit is contained in:
Shmulik Regev 2022-02-09 18:29:21 +02:00 committed by ddc
parent 4956057d04
commit 8b16c126a3
2 changed files with 24 additions and 11 deletions

18
main.go
View File

@ -36,8 +36,8 @@ var (
autoOpenBrowser = kingpin.Flag("auto-open-browser", "Specify whether auto open browser to show Web charts").Bool() 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() clean = kingpin.Flag("clean", "Clean the histogram bar once its finished. Default is true").Default("true").NegatableBool()
summary = kingpin.Flag("summary", "Only print the summary without realtime reports").Default("false").NegatableBool()
url = kingpin.Arg("url", "request url").Required().String() url = kingpin.Arg("url", "request url").Required().String()
) )
func errAndExit(msg string) { func errAndExit(msg string) {
@ -166,6 +166,11 @@ func main() {
return return
} }
outStream := os.Stdout
if *summary {
outStream = os.Stderr
isTerminal = false
}
// description // description
var desc string var desc string
desc = fmt.Sprintf("Benchmarking %s", *url) desc = fmt.Sprintf("Benchmarking %s", *url)
@ -176,7 +181,7 @@ func main() {
desc += fmt.Sprintf(" for %s", duration.String()) desc += fmt.Sprintf(" for %s", duration.String())
} }
desc += fmt.Sprintf(" using %d connection(s).", *concurrency) desc += fmt.Sprintf(" using %d connection(s).", *concurrency)
fmt.Println(desc) fmt.Fprintln(outStream,desc)
// charts listener // charts listener
var ln net.Listener var ln net.Listener
@ -186,9 +191,9 @@ func main() {
errAndExit(err.Error()) errAndExit(err.Error())
return 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 // do request
go requester.Run() go requester.Run()
@ -208,6 +213,7 @@ func main() {
} }
// terminal printer // terminal printer
printer := NewPrinter(*requests, *duration, !*clean) printer := NewPrinter(*requests, *duration, !*clean, *summary)
printer.PrintLoop(report.Snapshot, *interval, *seconds, report.Done()) printer.PrintLoop(report.Snapshot, *interval, *seconds, report.Done())
} }

View File

@ -33,10 +33,11 @@ type Printer struct {
pbNumStr string pbNumStr string
pbDurStr string pbDurStr string
noClean bool noClean bool
summary bool
} }
func NewPrinter(maxNum int64, maxDuration time.Duration, noCleanBar bool) *Printer { func NewPrinter(maxNum int64, maxDuration time.Duration, noCleanBar, summary bool) *Printer {
return &Printer{maxNum: maxNum, maxDuration: maxDuration, noClean: noCleanBar} return &Printer{maxNum: maxNum, maxDuration: maxDuration, noClean: noCleanBar, summary: summary}
} }
func (p *Printer) updateProgressValue(rs *SnapshotReport) { 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 buf bytes.Buffer
var backCursor string var backCursor string
cl := clearLine
if p.summary {
cl = nil
}
echo := func(isFinal bool) { echo := func(isFinal bool) {
report := snapshot() report := snapshot()
p.updateProgressValue(report) p.updateProgressValue(report)
@ -77,12 +82,12 @@ func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Durat
for { for {
i := bytes.IndexByte(result, '\n') i := bytes.IndexByte(result, '\n')
if i == -1 { if i == -1 {
os.Stdout.Write(clearLine) os.Stdout.Write(cl)
os.Stdout.Write(result) os.Stdout.Write(result)
break break
} }
n++ n++
os.Stdout.Write(clearLine) os.Stdout.Write(cl)
os.Stdout.Write(result[:i]) os.Stdout.Write(result[:i])
os.Stdout.Write([]byte("\n")) os.Stdout.Write([]byte("\n"))
result = result[i+1:] result = result[i+1:]
@ -97,7 +102,9 @@ func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Durat
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
echo(false) if !p.summary {
echo(false)
}
case <-doneChan: case <-doneChan:
ticker.Stop() ticker.Stop()
break loop break loop