plow/print.go

505 lines
14 KiB
Go
Raw Normal View History

2021-06-15 12:21:02 +00:00
package main
import (
"bytes"
2022-07-01 15:19:55 +00:00
"encoding/json"
2021-06-15 12:21:02 +00:00
"fmt"
"github.com/mattn/go-isatty"
"github.com/mattn/go-runewidth"
"math"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
var (
maxBarLen = 40
barStart = "|"
barBody = "■"
barEnd = "|"
barSpinner = []string{"|", "/", "-", "\\"}
clearLine = []byte("\r\033[K")
2021-06-22 14:39:02 +00:00
isTerminal = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
2021-06-15 12:21:02 +00:00
)
type Printer struct {
maxNum int64
maxDuration time.Duration
curNum int64
curDuration time.Duration
pbInc int64
pbNumStr string
pbDurStr string
2021-11-23 02:13:07 +00:00
noClean bool
summary bool
2021-06-15 12:21:02 +00:00
}
func NewPrinter(maxNum int64, maxDuration time.Duration, noCleanBar, summary bool) *Printer {
return &Printer{maxNum: maxNum, maxDuration: maxDuration, noClean: noCleanBar, summary: summary}
2021-06-15 12:21:02 +00:00
}
func (p *Printer) updateProgressValue(rs *SnapshotReport) {
2021-06-22 14:39:02 +00:00
p.pbInc++
2021-06-15 12:21:02 +00:00
if p.maxDuration > 0 {
n := rs.Elapsed
if n > p.maxDuration {
n = p.maxDuration
}
p.curDuration = n
barLen := int((p.curDuration*time.Duration(maxBarLen-2) + p.maxDuration/2) / p.maxDuration)
p.pbDurStr = barStart + strings.Repeat(barBody, barLen) + strings.Repeat(" ", maxBarLen-2-barLen) + barEnd
}
if p.maxNum > 0 {
p.curNum = rs.Count
if p.maxNum > 0 {
barLen := int((p.curNum*int64(maxBarLen-2) + p.maxNum/2) / p.maxNum)
p.pbNumStr = barStart + strings.Repeat(barBody, barLen) + strings.Repeat(" ", maxBarLen-2-barLen) + barEnd
} else {
idx := p.pbInc % int64(len(barSpinner))
p.pbNumStr = barSpinner[int(idx)]
}
}
}
2022-07-01 15:19:55 +00:00
func (p *Printer) PrintLoop(snapshot func() *SnapshotReport, interval time.Duration, useSeconds bool, json bool, doneChan <-chan struct{}) {
2021-06-15 12:21:02 +00:00
var buf bytes.Buffer
var backCursor string
cl := clearLine
2022-07-01 15:19:55 +00:00
if p.summary || interval == 0 || !isTerminal {
cl = nil
}
2021-06-15 12:21:02 +00:00
echo := func(isFinal bool) {
report := snapshot()
p.updateProgressValue(report)
os.Stdout.WriteString(backCursor)
buf.Reset()
2022-07-01 15:19:55 +00:00
if json {
p.formatJSONReports(&buf, report, isFinal, useSeconds)
} else {
p.formatTableReports(&buf, report, isFinal, useSeconds)
}
2021-06-15 12:21:02 +00:00
result := buf.Bytes()
n := 0
for {
i := bytes.IndexByte(result, '\n')
if i == -1 {
os.Stdout.Write(cl)
2021-06-15 12:21:02 +00:00
os.Stdout.Write(result)
break
}
n++
os.Stdout.Write(cl)
2021-06-15 12:21:02 +00:00
os.Stdout.Write(result[:i])
os.Stdout.Write([]byte("\n"))
result = result[i+1:]
}
os.Stdout.Sync()
2022-07-01 15:19:55 +00:00
if isTerminal {
backCursor = fmt.Sprintf("\033[%dA", n)
}
2021-06-15 12:21:02 +00:00
}
if interval > 0 {
ticker := time.NewTicker(interval)
loop:
for {
select {
case <-ticker.C:
if !p.summary {
echo(false)
}
2021-06-15 12:21:02 +00:00
case <-doneChan:
ticker.Stop()
break loop
}
}
} else {
<-doneChan
}
echo(true)
}
2021-06-22 14:39:02 +00:00
//nolint
2021-06-15 12:21:02 +00:00
const (
FgBlackColor int = iota + 30
FgRedColor
FgGreenColor
FgYellowColor
FgBlueColor
FgMagentaColor
FgCyanColor
FgWhiteColor
)
func colorize(s string, seq int) string {
if !isTerminal {
return s
}
return fmt.Sprintf("\033[%dm%s\033[0m", seq, s)
}
func durationToString(d time.Duration, useSeconds bool) string {
d = d.Truncate(time.Microsecond)
if useSeconds {
return formatFloat64(d.Seconds())
}
return d.String()
}
func alignBulk(bulk [][]string, aligns ...int) {
maxLen := map[int]int{}
for _, b := range bulk {
for i, bb := range b {
lbb := displayWidth(bb)
if maxLen[i] < lbb {
maxLen[i] = lbb
}
}
}
for _, b := range bulk {
for i, ali := range aligns {
if len(b) >= i+1 {
2021-06-22 14:39:02 +00:00
if i == len(aligns)-1 && ali == AlignLeft {
2021-06-15 12:21:02 +00:00
continue
}
b[i] = padString(b[i], " ", maxLen[i], ali)
}
}
}
}
func writeBulkWith(writer *bytes.Buffer, bulk [][]string, lineStart, sep, lineEnd string) {
for _, b := range bulk {
writer.WriteString(lineStart)
writer.WriteString(b[0])
for _, bb := range b[1:] {
writer.WriteString(sep)
writer.WriteString(bb)
}
writer.WriteString(lineEnd)
}
}
func writeBulk(writer *bytes.Buffer, bulk [][]string) {
writeBulkWith(writer, bulk, " ", " ", "\n")
}
func formatFloat64(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
2022-07-01 15:19:55 +00:00
func (p *Printer) formatJSONReports(writer *bytes.Buffer, snapshot *SnapshotReport, isFinal bool, useSeconds bool) {
indent := 0
writer.WriteString("{\n")
indent++
p.buildJSONSummary(writer, snapshot, indent)
if len(snapshot.Errors) != 0 {
writer.WriteString(",\n")
p.buildJSONErrors(writer, snapshot, indent)
}
writer.WriteString(",\n")
p.buildJSONStats(writer, snapshot, useSeconds, indent)
writer.WriteString(",\n")
p.buildJSONPercentile(writer, snapshot, useSeconds, indent)
writer.WriteString(",\n")
p.buildJSONHistogram(writer, snapshot, useSeconds, indent)
writer.WriteString("\n}\n")
}
2021-06-15 12:21:02 +00:00
func (p *Printer) formatTableReports(writer *bytes.Buffer, snapshot *SnapshotReport, isFinal bool, useSeconds bool) {
summaryBulk := p.buildSummary(snapshot, isFinal)
errorsBulks := p.buildErrors(snapshot)
statsBulk := p.buildStats(snapshot, useSeconds)
percBulk := p.buildPercentile(snapshot, useSeconds)
hisBulk := p.buildHistogram(snapshot, useSeconds, isFinal)
writer.WriteString("Summary:\n")
writeBulk(writer, summaryBulk)
writer.WriteString("\n")
if errorsBulks != nil {
writer.WriteString("Error:\n")
writeBulk(writer, errorsBulks)
writer.WriteString("\n")
}
writeBulkWith(writer, statsBulk, "", " ", "\n")
writer.WriteString("\n")
writer.WriteString("Latency Percentile:\n")
writeBulk(writer, percBulk)
writer.WriteString("\n")
writer.WriteString("Latency Histogram:\n")
writeBulk(writer, hisBulk)
}
2022-07-01 15:19:55 +00:00
func (p *Printer) buildJSONHistogram(writer *bytes.Buffer, snapshot *SnapshotReport, useSeconds bool, indent int) {
tab0 := strings.Repeat(" ", indent)
writer.WriteString(tab0 + "\"Histograms\": [\n")
tab1 := strings.Repeat(" ", indent+1)
maxCount := 0
hisSum := 0
for _, bin := range snapshot.Histograms {
if maxCount < bin.Count {
maxCount = bin.Count
}
hisSum += bin.Count
}
for i, bin := range snapshot.Histograms {
writer.WriteString(fmt.Sprintf(`%s[ "%s", %d ]`, tab1,
durationToString(bin.Mean, useSeconds), bin.Count))
if i != len(snapshot.Histograms)-1 {
writer.WriteString(",")
}
writer.WriteString("\n")
}
writer.WriteString(tab0 + "]")
}
2021-06-15 12:21:02 +00:00
func (p *Printer) buildHistogram(snapshot *SnapshotReport, useSeconds bool, isFinal bool) [][]string {
hisBulk := make([][]string, 0, 8)
maxCount := 0
hisSum := 0
for _, bin := range snapshot.Histograms {
if maxCount < bin.Count {
maxCount = bin.Count
}
hisSum += bin.Count
}
for _, bin := range snapshot.Histograms {
row := []string{durationToString(bin.Mean, useSeconds), strconv.Itoa(bin.Count)}
if isFinal {
row = append(row, fmt.Sprintf("%.2f%%", math.Floor(float64(bin.Count)*1e4/float64(hisSum)+0.5)/100.0))
2021-11-22 08:01:46 +00:00
}
2021-11-23 02:13:07 +00:00
if !isFinal || p.noClean {
2021-06-15 12:21:02 +00:00
barLen := 0
if maxCount > 0 {
barLen = (bin.Count*maxBarLen + maxCount/2) / maxCount
}
row = append(row, strings.Repeat(barBody, barLen))
}
hisBulk = append(hisBulk, row)
}
if isFinal {
2021-06-22 14:39:02 +00:00
alignBulk(hisBulk, AlignLeft, AlignRight, AlignRight)
2021-06-15 12:21:02 +00:00
} else {
2021-06-22 14:39:02 +00:00
alignBulk(hisBulk, AlignLeft, AlignRight, AlignLeft)
2021-06-15 12:21:02 +00:00
}
return hisBulk
}
2022-07-01 15:19:55 +00:00
func (p *Printer) buildJSONPercentile(writer *bytes.Buffer, snapshot *SnapshotReport, useSeconds bool, indent int) {
tab0 := strings.Repeat(" ", indent)
writer.WriteString(tab0 + "\"Percentiles\": {\n")
tab1 := strings.Repeat(" ", indent+1)
for i, percentile := range snapshot.Percentiles {
perc := formatFloat64(percentile.Percentile * 100)
writer.WriteString(fmt.Sprintf(`%s"%s": "%s"`, tab1, "P"+perc,
durationToString(percentile.Latency, useSeconds)))
if i != len(snapshot.Percentiles)-1 {
writer.WriteString(",")
}
writer.WriteString("\n")
}
writer.WriteString(tab0 + "}")
}
2021-06-15 12:21:02 +00:00
func (p *Printer) buildPercentile(snapshot *SnapshotReport, useSeconds bool) [][]string {
percBulk := make([][]string, 2)
percAligns := make([]int, 0, len(snapshot.Percentiles))
for _, percentile := range snapshot.Percentiles {
perc := formatFloat64(percentile.Percentile * 100)
percBulk[0] = append(percBulk[0], "P"+perc)
percBulk[1] = append(percBulk[1], durationToString(percentile.Latency, useSeconds))
2021-06-22 14:39:02 +00:00
percAligns = append(percAligns, AlignCenter)
2021-06-15 12:21:02 +00:00
}
2021-06-22 14:39:02 +00:00
percAligns[0] = AlignLeft
2021-06-15 12:21:02 +00:00
alignBulk(percBulk, percAligns...)
return percBulk
}
2022-07-01 15:19:55 +00:00
func (p *Printer) buildJSONStats(writer *bytes.Buffer, snapshot *SnapshotReport, useSeconds bool, indent int) {
tab0 := strings.Repeat(" ", indent)
writer.WriteString(tab0 + "\"Statistics\": {\n")
tab1 := strings.Repeat(" ", indent+1)
writer.WriteString(fmt.Sprintf(`%s"Latency": { "Min": "%s", "Mean": "%s", "StdDev": "%s", "Max": "%s" }`,
tab1,
durationToString(snapshot.Stats.Min, useSeconds),
durationToString(snapshot.Stats.Mean, useSeconds),
durationToString(snapshot.Stats.StdDev, useSeconds),
durationToString(snapshot.Stats.Max, useSeconds),
))
if snapshot.RpsStats != nil {
writer.WriteString(",\n")
writer.WriteString(fmt.Sprintf(`%s"RPS": { "Min": %s, "Mean": %s, "StdDev": %s, "Max": %s }`,
tab1,
formatFloat64(math.Trunc(snapshot.RpsStats.Min*100)/100.0),
formatFloat64(math.Trunc(snapshot.RpsStats.Mean*100)/100.0),
formatFloat64(math.Trunc(snapshot.RpsStats.StdDev*100)/100.0),
formatFloat64(math.Trunc(snapshot.RpsStats.Max*100)/100.0),
))
}
writer.WriteString("\n" + tab0 + "}")
}
2021-06-15 12:21:02 +00:00
func (p *Printer) buildStats(snapshot *SnapshotReport, useSeconds bool) [][]string {
var statsBulk [][]string
statsBulk = append(statsBulk,
[]string{"Statistics", "Min", "Mean", "StdDev", "Max"},
[]string{
" Latency",
durationToString(snapshot.Stats.Min, useSeconds),
durationToString(snapshot.Stats.Mean, useSeconds),
durationToString(snapshot.Stats.StdDev, useSeconds),
durationToString(snapshot.Stats.Max, useSeconds),
},
)
if snapshot.RpsStats != nil {
statsBulk = append(statsBulk,
[]string{
" RPS",
formatFloat64(math.Trunc(snapshot.RpsStats.Min*100) / 100.0),
formatFloat64(math.Trunc(snapshot.RpsStats.Mean*100) / 100.0),
formatFloat64(math.Trunc(snapshot.RpsStats.StdDev*100) / 100.0),
formatFloat64(math.Trunc(snapshot.RpsStats.Max*100) / 100.0),
},
)
}
2021-06-22 14:39:02 +00:00
alignBulk(statsBulk, AlignLeft, AlignCenter, AlignCenter, AlignCenter, AlignCenter)
2021-06-15 12:21:02 +00:00
return statsBulk
}
2022-07-01 15:19:55 +00:00
func (p *Printer) buildJSONErrors(writer *bytes.Buffer, snapshot *SnapshotReport, indent int) {
tab0 := strings.Repeat(" ", indent)
writer.WriteString(tab0 + "\"Error\": {\n")
tab1 := strings.Repeat(" ", indent+1)
errors := sortMapStrInt(snapshot.Errors)
for i, v := range errors {
v[1] = colorize(v[1], FgRedColor)
vb, _ := json.Marshal(v[0])
writer.WriteString(fmt.Sprintf(`%s%s: %s`, tab1, vb, v[1]))
if i != len(errors)-1 {
writer.WriteString(",")
}
writer.WriteString("\n")
}
writer.WriteString(tab0 + "}")
}
2021-06-15 12:21:02 +00:00
func (p *Printer) buildErrors(snapshot *SnapshotReport) [][]string {
var errorsBulks [][]string
for k, v := range snapshot.Errors {
vs := colorize(strconv.FormatInt(v, 10), FgRedColor)
errorsBulks = append(errorsBulks, []string{vs, "\"" + k + "\""})
}
if errorsBulks != nil {
sort.Slice(errorsBulks, func(i, j int) bool { return errorsBulks[i][1] < errorsBulks[j][1] })
}
2021-06-22 14:39:02 +00:00
alignBulk(errorsBulks, AlignLeft, AlignLeft)
2021-06-15 12:21:02 +00:00
return errorsBulks
}
2022-07-01 15:19:55 +00:00
func sortMapStrInt(m map[string]int64) (ret [][]string) {
for k, v := range m {
ret = append(ret, []string{k, strconv.FormatInt(v, 10)})
}
sort.Slice(ret, func(i, j int) bool { return ret[i][0] < ret[j][0] })
return
}
func (p *Printer) buildJSONSummary(writer *bytes.Buffer, snapshot *SnapshotReport, indent int) {
tab0 := strings.Repeat(" ", indent)
writer.WriteString(tab0 + "\"Summary\": {\n")
{
tab1 := strings.Repeat(" ", indent+1)
writer.WriteString(fmt.Sprintf("%s\"Elapsed\": \"%s\",\n", tab1, snapshot.Elapsed.Truncate(100*time.Millisecond).String()))
writer.WriteString(fmt.Sprintf("%s\"Count\": %d,\n", tab1, snapshot.Count))
writer.WriteString(fmt.Sprintf("%s\"Counts\": {\n", tab1))
i := 0
tab2 := strings.Repeat(" ", indent+2)
codes := sortMapStrInt(snapshot.Codes)
for _, v := range codes {
i++
if v[0] != "2xx" {
v[1] = colorize(v[1], FgMagentaColor)
}
writer.WriteString(fmt.Sprintf(`%s"%s": %s`, tab2, v[0], v[1]))
if i != len(snapshot.Codes) {
writer.WriteString(",")
}
writer.WriteString("\n")
}
writer.WriteString(tab1 + "},\n")
writer.WriteString(fmt.Sprintf("%s\"RPS\": %.3f,\n", tab1, snapshot.RPS))
writer.WriteString(fmt.Sprintf("%s\"Reads\": \"%.3fMB/s\",\n", tab1, snapshot.ReadThroughput))
writer.WriteString(fmt.Sprintf("%s\"Writes\": \"%.3fMB/s\"\n", tab1, snapshot.WriteThroughput))
}
writer.WriteString(tab0 + "}")
}
2021-06-15 12:21:02 +00:00
func (p *Printer) buildSummary(snapshot *SnapshotReport, isFinal bool) [][]string {
summarybulk := make([][]string, 0, 8)
2022-07-01 15:19:55 +00:00
elapsedLine := []string{"Elapsed", snapshot.Elapsed.Truncate(100 * time.Millisecond).String()}
2021-06-15 12:21:02 +00:00
if p.maxDuration > 0 && !isFinal {
elapsedLine = append(elapsedLine, p.pbDurStr)
}
countLine := []string{"Count", strconv.FormatInt(snapshot.Count, 10)}
if p.maxNum > 0 && !isFinal {
countLine = append(countLine, p.pbNumStr)
}
summarybulk = append(
summarybulk,
elapsedLine,
countLine,
)
2022-07-01 15:19:55 +00:00
codes := sortMapStrInt(snapshot.Codes)
for _, v := range codes {
if v[0] != "2xx" {
v[1] = colorize(v[1], FgMagentaColor)
2021-06-15 12:21:02 +00:00
}
2022-07-01 15:19:55 +00:00
summarybulk = append(summarybulk, []string{" " + v[0], v[1]})
2021-06-15 12:21:02 +00:00
}
summarybulk = append(summarybulk,
[]string{"RPS", fmt.Sprintf("%.3f", snapshot.RPS)},
[]string{"Reads", fmt.Sprintf("%.3fMB/s", snapshot.ReadThroughput)},
[]string{"Writes", fmt.Sprintf("%.3fMB/s", snapshot.WriteThroughput)},
)
2021-06-22 14:39:02 +00:00
alignBulk(summarybulk, AlignLeft, AlignRight)
2021-06-15 12:21:02 +00:00
return summarybulk
}
var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]")
func displayWidth(str string) int {
return runewidth.StringWidth(ansi.ReplaceAllLiteralString(str, ""))
}
const (
2021-06-22 14:39:02 +00:00
AlignLeft = iota
AlignRight
AlignCenter
2021-06-15 12:21:02 +00:00
)
func padString(s, pad string, width int, align int) string {
gap := width - displayWidth(s)
if gap > 0 {
2021-06-22 14:39:02 +00:00
if align == AlignLeft {
2021-06-15 12:21:02 +00:00
return s + strings.Repeat(pad, gap)
2021-06-22 14:39:02 +00:00
} else if align == AlignRight {
2021-06-15 12:21:02 +00:00
return strings.Repeat(pad, gap) + s
2021-06-22 14:39:02 +00:00
} else if align == AlignCenter {
gapLeft := gap / 2
2021-06-15 12:21:02 +00:00
gapRight := gap - gapLeft
return strings.Repeat(pad, gapLeft) + s + strings.Repeat(pad, gapRight)
}
}
return s
}