Merge pull request #130 from aykhans/refactor/response

Refactor 'Responses' type and its methods
This commit is contained in:
2025-08-17 19:51:50 +04:00
committed by GitHub
5 changed files with 34 additions and 49 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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,
})

View File

@@ -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)
}
}

View File

@@ -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...)
}