From 18b1c7dae3b28790449d7812b52adbeef3eff74a Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Thu, 19 Dec 2024 21:40:53 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Round=20off=20durations=20before?= =?UTF-8?q?=20printing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 4 ++-- requests/response.go | 42 ++++++++++++++++++++++-------------------- utils/int.go | 21 +++++++++++++++++++++ utils/time.go | 14 ++++++++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 utils/int.go create mode 100644 utils/time.go diff --git a/config/config.go b/config/config.go index 09689b7..9015f8a 100644 --- a/config/config.go +++ b/config/config.go @@ -209,8 +209,8 @@ func (config *JSONConfig) MergeConfigs(newConfig *JSONConfig) { type CLIConfig struct { *Config - Yes Option[bool] `json:"yes" validate:"omitempty"` - ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"` + Yes Option[bool] `json:"yes" validate:"omitempty"` + ConfigFile string `validation_name:"config-file" validate:"omitempty,filepath"` } func NewCLIConfig( diff --git a/requests/response.go b/requests/response.go index 00d1f08..9bf671d 100644 --- a/requests/response.go +++ b/requests/response.go @@ -5,6 +5,7 @@ import ( "time" . "github.com/aykhans/dodo/types" + "github.com/aykhans/dodo/utils" "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 // response count, minimum time, maximum time, average time, and latency percentiles. -func (respones Responses) Print() { +func (responses Responses) Print() { total := struct { Count int Min time.Duration @@ -27,14 +28,14 @@ func (respones Responses) Print() { P95 time.Duration P99 time.Duration }{ - Count: len(respones), - Min: respones[0].Time, - Max: respones[0].Time, + Count: len(responses), + Min: responses[0].Time, + Max: responses[0].Time, } mergedResponses := make(map[string]Durations) var allDurations Durations - for _, response := range respones { + for _, response := range responses { if response.Time < total.Min { total.Min = response.Time } @@ -64,14 +65,15 @@ func (respones Responses) Print() { t.AppendHeader(table.Row{ "Response", "Count", - "Min Time", - "Max Time", - "Average Time", + "Min", + "Max", + "Average", "P90", "P95", "P99", }) + var roundPrecision int64 = 4 for key, durations := range mergedResponses { durations.Sort() durationsLen := len(durations) @@ -80,12 +82,12 @@ func (respones Responses) Print() { t.AppendRow(table.Row{ key, durationsLen, - durations.First(), - durations.Last(), - durations.Avg(), - durations[int(0.90*durationsLenAsFloat)], - durations[int(0.95*durationsLenAsFloat)], - durations[int(0.99*durationsLenAsFloat)], + utils.DurationRoundBy(*durations.First(), roundPrecision), + utils.DurationRoundBy(*durations.Last(), roundPrecision), + utils.DurationRoundBy(durations.Avg(), roundPrecision), + utils.DurationRoundBy(durations[int(0.90*durationsLenAsFloat)], roundPrecision), + utils.DurationRoundBy(durations[int(0.95*durationsLenAsFloat)], roundPrecision), + utils.DurationRoundBy(durations[int(0.99*durationsLenAsFloat)], roundPrecision), }) t.AppendSeparator() } @@ -94,12 +96,12 @@ func (respones Responses) Print() { t.AppendRow(table.Row{ "Total", total.Count, - total.Min, - total.Max, - total.Sum / time.Duration(total.Count), // Average - total.P90, - total.P95, - total.P99, + utils.DurationRoundBy(total.Min, roundPrecision), + utils.DurationRoundBy(total.Max, roundPrecision), + utils.DurationRoundBy(total.Sum/time.Duration(total.Count), roundPrecision), // Average + utils.DurationRoundBy(total.P90, roundPrecision), + utils.DurationRoundBy(total.P95, roundPrecision), + utils.DurationRoundBy(total.P99, roundPrecision), }) } t.Render() diff --git a/utils/int.go b/utils/int.go new file mode 100644 index 0000000..6faa348 --- /dev/null +++ b/utils/int.go @@ -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 +} diff --git a/utils/time.go b/utils/time.go new file mode 100644 index 0000000..9aff2fa --- /dev/null +++ b/utils/time.go @@ -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 +}