From ffa724fae768a061b2d01ef1bdf01e92732cdfb8 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sun, 17 Aug 2025 19:30:26 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A8=20Refactor=20'Responses'=20typ?= =?UTF-8?q?e=20and=20its=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requests/response.go | 58 +++++++++++++++++--------------------------- requests/run.go | 14 +++++------ types/durations.go | 5 ++-- utils/slice.go | 4 +-- 4 files changed, 33 insertions(+), 48 deletions(-) diff --git a/requests/response.go b/requests/response.go index 16155ce..be3ec76 100644 --- a/requests/response.go +++ b/requests/response.go @@ -14,47 +14,30 @@ type Response struct { Time time.Duration } -type Responses []*Response +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 (responses Responses) Print() { - total := struct { - Count int - Min time.Duration - Max time.Duration - Sum time.Duration - P90 time.Duration - P95 time.Duration - P99 time.Duration - }{ - Count: len(responses), - Min: responses[0].Time, - Max: responses[0].Time, + if len(responses) == 0 { + return } - mergedResponses := make(map[string]types.Durations) - var allDurations types.Durations - for _, response := range responses { - if response.Time < total.Min { - total.Min = response.Time - } - if response.Time > total.Max { - total.Max = response.Time - } - total.Sum += response.Time + mergedResponses := make(map[string]types.Durations) + + totalDurations := make(types.Durations, len(responses)) + var totalSum time.Duration + totalCount := len(responses) + + for i, response := range responses { + totalSum += response.Time + totalDurations[i] = response.Time mergedResponses[response.Response] = append( mergedResponses[response.Response], 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.SetOutputMirror(os.Stdout) @@ -93,15 +76,18 @@ func (responses Responses) Print() { } if len(mergedResponses) > 1 { + totalDurations.Sort() + allDurationsLenAsFloat := float64(len(totalDurations) - 1) + t.AppendRow(table.Row{ "Total", - total.Count, - 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), + totalCount, + utils.DurationRoundBy(totalDurations[0], roundPrecision), + utils.DurationRoundBy(totalDurations[len(totalDurations)-1], roundPrecision), + utils.DurationRoundBy(totalSum/time.Duration(totalCount), roundPrecision), // Average + utils.DurationRoundBy(totalDurations[int(0.90*allDurationsLenAsFloat)], roundPrecision), + utils.DurationRoundBy(totalDurations[int(0.95*allDurationsLenAsFloat)], roundPrecision), + utils.DurationRoundBy(totalDurations[int(0.99*allDurationsLenAsFloat)], roundPrecision), }) } t.Render() diff --git a/requests/run.go b/requests/run.go index 2095a2b..edd59d6 100644 --- a/requests/run.go +++ b/requests/run.go @@ -66,7 +66,7 @@ func releaseDodos( streamWG sync.WaitGroup requestCountPerDodo uint dodosCount = requestConfig.GetValidDodosCountForRequests() - responses = make([][]*Response, dodosCount) + responses = make([][]Response, dodosCount) increase = make(chan int64, requestConfig.RequestCount) ) @@ -123,7 +123,7 @@ func sendRequestByCount( request *Request, timeout time.Duration, requestCount uint, - responseData *[]*Response, + responseData *[]Response, increase chan<- int64, wg *sync.WaitGroup, ) { @@ -146,7 +146,7 @@ func sendRequestByCount( if err == types.ErrInterrupt { return } - *responseData = append(*responseData, &Response{ + *responseData = append(*responseData, Response{ Response: err.Error(), Time: completedTime, }) @@ -154,7 +154,7 @@ func sendRequestByCount( return } - *responseData = append(*responseData, &Response{ + *responseData = append(*responseData, Response{ Response: strconv.Itoa(response.StatusCode()), Time: completedTime, }) @@ -170,7 +170,7 @@ func sendRequest( ctx context.Context, request *Request, timeout time.Duration, - responseData *[]*Response, + responseData *[]Response, increase chan<- int64, wg *sync.WaitGroup, ) { @@ -193,7 +193,7 @@ func sendRequest( if err == types.ErrInterrupt { return } - *responseData = append(*responseData, &Response{ + *responseData = append(*responseData, Response{ Response: err.Error(), Time: completedTime, }) @@ -201,7 +201,7 @@ func sendRequest( return } - *responseData = append(*responseData, &Response{ + *responseData = append(*responseData, Response{ Response: strconv.Itoa(response.StatusCode()), Time: completedTime, }) diff --git a/types/durations.go b/types/durations.go index 18fcf9c..ee4970d 100644 --- a/types/durations.go +++ b/types/durations.go @@ -1,6 +1,7 @@ package types import ( + "slices" "sort" "time" ) @@ -14,9 +15,7 @@ func (d Durations) Sort(ascending ...bool) { return d[i] > d[j] }) } else { // Otherwise, sort in ascending order - sort.Slice(d, func(i, j int) bool { - return d[i] < d[j] - }) + slices.Sort(d) } } diff --git a/utils/slice.go b/utils/slice.go index 1f56656..3c6d636 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -2,8 +2,8 @@ package utils import "math/rand" -func Flatten[T any](nested [][]*T) []*T { - flattened := make([]*T, 0) +func Flatten[T any](nested [][]T) []T { + flattened := make([]T, 0) for _, n := range nested { flattened = append(flattened, n...) } From 361d423651aa9a4677710407eba56bf49b277463 Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Sun, 17 Aug 2025 19:31:36 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20bump=20version=20to=20?= =?UTF-8?q?0.7.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 6b6ef24..f333966 100644 --- a/config/config.go +++ b/config/config.go @@ -18,7 +18,7 @@ import ( ) const ( - VERSION string = "0.7.2" + VERSION string = "0.7.3" DefaultUserAgent string = "Dodo/" + VERSION DefaultMethod string = "GET" DefaultTimeout time.Duration = time.Second * 10