🔨 Round off durations before printing

This commit is contained in:
Aykhan Shahsuvarov 2024-12-19 21:40:53 +04:00
parent 77ddcf722a
commit 18b1c7dae3
4 changed files with 59 additions and 22 deletions

View File

@ -209,8 +209,8 @@ func (config *JSONConfig) MergeConfigs(newConfig *JSONConfig) {
type CLIConfig struct { type CLIConfig struct {
*Config *Config
Yes Option[bool] `json:"yes" validate:"omitempty"` Yes Option[bool] `json:"yes" validate:"omitempty"`
ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"` ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"`
} }
func NewCLIConfig( func NewCLIConfig(

View File

@ -5,6 +5,7 @@ import (
"time" "time"
. "github.com/aykhans/dodo/types" . "github.com/aykhans/dodo/types"
"github.com/aykhans/dodo/utils"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
) )
@ -17,7 +18,7 @@ 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, average time, and latency percentiles. // response count, minimum time, maximum time, average time, and latency percentiles.
func (respones Responses) Print() { func (responses Responses) Print() {
total := struct { total := struct {
Count int Count int
Min time.Duration Min time.Duration
@ -27,14 +28,14 @@ func (respones Responses) Print() {
P95 time.Duration P95 time.Duration
P99 time.Duration P99 time.Duration
}{ }{
Count: len(respones), Count: len(responses),
Min: respones[0].Time, Min: responses[0].Time,
Max: respones[0].Time, Max: responses[0].Time,
} }
mergedResponses := make(map[string]Durations) mergedResponses := make(map[string]Durations)
var allDurations Durations var allDurations Durations
for _, response := range respones { for _, response := range responses {
if response.Time < total.Min { if response.Time < total.Min {
total.Min = response.Time total.Min = response.Time
} }
@ -64,14 +65,15 @@ func (respones Responses) Print() {
t.AppendHeader(table.Row{ t.AppendHeader(table.Row{
"Response", "Response",
"Count", "Count",
"Min Time", "Min",
"Max Time", "Max",
"Average Time", "Average",
"P90", "P90",
"P95", "P95",
"P99", "P99",
}) })
var roundPrecision int64 = 4
for key, durations := range mergedResponses { for key, durations := range mergedResponses {
durations.Sort() durations.Sort()
durationsLen := len(durations) durationsLen := len(durations)
@ -80,12 +82,12 @@ func (respones Responses) Print() {
t.AppendRow(table.Row{ t.AppendRow(table.Row{
key, key,
durationsLen, durationsLen,
durations.First(), utils.DurationRoundBy(*durations.First(), roundPrecision),
durations.Last(), utils.DurationRoundBy(*durations.Last(), roundPrecision),
durations.Avg(), utils.DurationRoundBy(durations.Avg(), roundPrecision),
durations[int(0.90*durationsLenAsFloat)], utils.DurationRoundBy(durations[int(0.90*durationsLenAsFloat)], roundPrecision),
durations[int(0.95*durationsLenAsFloat)], utils.DurationRoundBy(durations[int(0.95*durationsLenAsFloat)], roundPrecision),
durations[int(0.99*durationsLenAsFloat)], utils.DurationRoundBy(durations[int(0.99*durationsLenAsFloat)], roundPrecision),
}) })
t.AppendSeparator() t.AppendSeparator()
} }
@ -94,12 +96,12 @@ func (respones Responses) Print() {
t.AppendRow(table.Row{ t.AppendRow(table.Row{
"Total", "Total",
total.Count, total.Count,
total.Min, utils.DurationRoundBy(total.Min, roundPrecision),
total.Max, utils.DurationRoundBy(total.Max, roundPrecision),
total.Sum / time.Duration(total.Count), // Average utils.DurationRoundBy(total.Sum/time.Duration(total.Count), roundPrecision), // Average
total.P90, utils.DurationRoundBy(total.P90, roundPrecision),
total.P95, utils.DurationRoundBy(total.P95, roundPrecision),
total.P99, utils.DurationRoundBy(total.P99, roundPrecision),
}) })
} }
t.Render() t.Render()

21
utils/int.go Normal file
View File

@ -0,0 +1,21 @@
package utils
type Number interface {
int | int8 | int16 | int32 | int64
}
func NumLen[T Number](n T) T {
if n < 0 {
n = -n
}
if n == 0 {
return 1
}
var count T = 0
for n > 0 {
n /= 10
count++
}
return count
}

14
utils/time.go Normal file
View File

@ -0,0 +1,14 @@
package utils
import "time"
func DurationRoundBy(duration time.Duration, n int64) time.Duration {
if durationLen := NumLen(duration.Nanoseconds()); durationLen > n {
roundNum := 1
for range durationLen - n {
roundNum *= 10
}
return duration.Round(time.Duration(roundNum))
}
return duration
}