Skip to content

Commit

Permalink
fix: response time histogram padding
Browse files Browse the repository at this point in the history
  • Loading branch information
elmanelman committed Aug 18, 2021
1 parent 3358b20 commit b731502
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,21 +335,45 @@ func formatPercent(num int, total uint64) string {
}

func histogram(buckets []runner.Bucket) string {
max := 0
maxMark := 0.0
maxCount := 0
for _, b := range buckets {
if v := b.Count; v > max {
max = v
if v := b.Mark; v > maxMark {
maxMark = v
}
if v := b.Count; v > maxCount {
maxCount = v
}
}

formatMark := func(mark float64) string {
return fmt.Sprintf("%.3f", mark*1000)
}
formatCount := func(count int) string {
return fmt.Sprintf("%v", count)
}

maxMarkLen := len(formatMark(maxMark))
maxCountLen := len(formatCount(maxCount))
res := new(bytes.Buffer)
for i := 0; i < len(buckets); i++ {
// Normalize bar lengths.
var barLen int
if max > 0 {
barLen = (buckets[i].Count*40 + max/2) / max
if maxCount > 0 {
barLen = (buckets[i].Count*40 + maxCount/2) / maxCount
}
res.WriteString(fmt.Sprintf(" %4.3f [%v]\t|%v\n", buckets[i].Mark*1000, buckets[i].Count, strings.Repeat(barChar, barLen)))
markStr := formatMark(buckets[i].Mark)
countStr := formatCount(buckets[i].Count)
res.WriteString(fmt.Sprintf(
" %s%s [%v]%s |%v\n",
markStr,
strings.Repeat(" ", maxMarkLen-len(markStr)),
buckets[i].Count,
strings.Repeat(" ", maxCountLen-len(countStr)),
strings.Repeat(barChar, barLen),
))
}

return res.String()
}

Expand Down

0 comments on commit b731502

Please sign in to comment.