Add latency percentiles to the results table

This commit is contained in:
Aykhan Shahsuvarov 2024-12-17 03:42:52 +04:00
parent 3068e0acae
commit bd33b2c6a2
2 changed files with 57 additions and 58 deletions

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"time" "time"
"github.com/aykhans/dodo/utils" . "github.com/aykhans/dodo/types"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
) )
@ -18,24 +18,32 @@ type Response struct {
type Responses []*Response type Responses []*Response
// Print prints the responses in a tabular format, including information such as // Print prints the responses in a tabular format, including information such as
// response count, minimum time, maximum time, and average time. // response count, minimum time, maximum time, average time, and latency percentiles.
func (respones Responses) Print() { func (respones Responses) Print() {
var ( total := struct {
totalMinDuration time.Duration = respones[0].Time Count int
totalMaxDuration time.Duration = respones[0].Time Min time.Duration
totalDuration time.Duration Max time.Duration
totalCount int = len(respones) Sum time.Duration
) P90 time.Duration
mergedResponses := make(map[string][]time.Duration) P95 time.Duration
P99 time.Duration
}{
Count: len(respones),
Min: respones[0].Time,
Max: respones[0].Time,
}
mergedResponses := make(map[string]Durations)
var allDurations Durations
for _, response := range respones { for _, response := range respones {
if response.Time < totalMinDuration { if response.Time < total.Min {
totalMinDuration = response.Time total.Min = response.Time
} }
if response.Time > totalMaxDuration { if response.Time > total.Max {
totalMaxDuration = response.Time total.Max = response.Time
} }
totalDuration += response.Time total.Sum += response.Time
if response.Error != nil { if response.Error != nil {
mergedResponses[response.Error.Error()] = append( mergedResponses[response.Error.Error()] = append(
@ -48,38 +56,60 @@ func (respones Responses) Print() {
response.Time, response.Time,
) )
} }
allDurations = append(allDurations, response.Time)
} }
allDurations.Sort()
allDurationsLenAsFloat := float64(len(allDurations) - 1)
total.P90 = allDurations[int(0.90*allDurationsLenAsFloat)]
total.P95 = allDurations[int(0.95*allDurationsLenAsFloat)]
total.P99 = allDurations[int(0.99*allDurationsLenAsFloat)]
t := table.NewWriter() t := table.NewWriter()
t.SetOutputMirror(os.Stdout) t.SetOutputMirror(os.Stdout)
t.SetStyle(table.StyleLight) t.SetStyle(table.StyleLight)
t.SetColumnConfigs([]table.ColumnConfig{ t.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, WidthMax: 80}, {Number: 1, WidthMax: 40},
}) })
t.AppendHeader(table.Row{ t.AppendHeader(table.Row{
"Response", "Response",
"Count", "Count",
"Min Time", "Min Time",
"Max Time", "Max Time",
"Average Time", "Average Time",
"P90",
"P95",
"P99",
}) })
for key, durations := range mergedResponses { for key, durations := range mergedResponses {
durations.Sort()
durationsLen := len(durations)
durationsLenAsFloat := float64(durationsLen - 1)
t.AppendRow(table.Row{ t.AppendRow(table.Row{
key, key,
len(durations), durationsLen,
utils.MinDuration(durations...), durations.First(),
utils.MaxDuration(durations...), durations.Last(),
utils.AvgDuration(durations...), durations.Avg(),
durations[int(0.90*durationsLenAsFloat)],
durations[int(0.95*durationsLenAsFloat)],
durations[int(0.99*durationsLenAsFloat)],
}) })
t.AppendSeparator() t.AppendSeparator()
} }
if len(mergedResponses) > 1 {
t.AppendRow(table.Row{ t.AppendRow(table.Row{
"Total", "Total",
totalCount, total.Count,
totalMinDuration, total.Min,
totalMaxDuration, total.Max,
totalDuration / time.Duration(totalCount), total.Sum / time.Duration(total.Count), // Average
total.P90,
total.P95,
total.P99,
}) })
}
t.Render() t.Render()
} }

View File

@ -1,31 +0,0 @@
package utils
import "time"
func MinDuration(durations ...time.Duration) time.Duration {
min := durations[0]
for _, d := range durations {
if d < min {
min = d
}
}
return min
}
func MaxDuration(durations ...time.Duration) time.Duration {
max := durations[0]
for _, d := range durations {
if d > max {
max = d
}
}
return max
}
func AvgDuration(durations ...time.Duration) time.Duration {
total := time.Duration(0)
for _, d := range durations {
total += d
}
return total / time.Duration(len(durations))
}