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

16
main.go
View File

@ -36,7 +36,7 @@ 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()
summary = kingpin.Flag("summary", "Only print the summary without realtime reports").Default("false").NegatableBool()
url = kingpin.Arg("url", "request url").Required().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())
}

View File

@ -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:
if !p.summary {
echo(false)
}
case <-doneChan:
ticker.Stop()
break loop