Skip to content

Commit 25d4762

Browse files
authored
Merge pull request #130 from aykhans/refactor/response
Refactor 'Responses' type and its methods
2 parents 7930be4 + 361d423 commit 25d4762

File tree

5 files changed

+33
-48
lines changed

5 files changed

+33
-48
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
VERSION string = "0.7.2"
21+
VERSION string = "0.7.3"
2222
DefaultUserAgent string = "Dodo/" + VERSION
2323
DefaultMethod string = "GET"
2424
DefaultTimeout time.Duration = time.Second * 10

requests/response.go

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,30 @@ type Response struct {
1414
Time time.Duration
1515
}
1616

17-
type Responses []*Response
17+
type Responses []Response
1818

1919
// Print prints the responses in a tabular format, including information such as
2020
// response count, minimum time, maximum time, average time, and latency percentiles.
2121
func (responses Responses) Print() {
22-
total := struct {
23-
Count int
24-
Min time.Duration
25-
Max time.Duration
26-
Sum time.Duration
27-
P90 time.Duration
28-
P95 time.Duration
29-
P99 time.Duration
30-
}{
31-
Count: len(responses),
32-
Min: responses[0].Time,
33-
Max: responses[0].Time,
22+
if len(responses) == 0 {
23+
return
3424
}
25+
3526
mergedResponses := make(map[string]types.Durations)
36-
var allDurations types.Durations
3727

38-
for _, response := range responses {
39-
if response.Time < total.Min {
40-
total.Min = response.Time
41-
}
42-
if response.Time > total.Max {
43-
total.Max = response.Time
44-
}
45-
total.Sum += response.Time
28+
totalDurations := make(types.Durations, len(responses))
29+
var totalSum time.Duration
30+
totalCount := len(responses)
31+
32+
for i, response := range responses {
33+
totalSum += response.Time
34+
totalDurations[i] = response.Time
4635

4736
mergedResponses[response.Response] = append(
4837
mergedResponses[response.Response],
4938
response.Time,
5039
)
51-
allDurations = append(allDurations, response.Time)
5240
}
53-
allDurations.Sort()
54-
allDurationsLenAsFloat := float64(len(allDurations) - 1)
55-
total.P90 = allDurations[int(0.90*allDurationsLenAsFloat)]
56-
total.P95 = allDurations[int(0.95*allDurationsLenAsFloat)]
57-
total.P99 = allDurations[int(0.99*allDurationsLenAsFloat)]
5841

5942
t := table.NewWriter()
6043
t.SetOutputMirror(os.Stdout)
@@ -93,15 +76,18 @@ func (responses Responses) Print() {
9376
}
9477

9578
if len(mergedResponses) > 1 {
79+
totalDurations.Sort()
80+
allDurationsLenAsFloat := float64(len(totalDurations) - 1)
81+
9682
t.AppendRow(table.Row{
9783
"Total",
98-
total.Count,
99-
utils.DurationRoundBy(total.Min, roundPrecision),
100-
utils.DurationRoundBy(total.Max, roundPrecision),
101-
utils.DurationRoundBy(total.Sum/time.Duration(total.Count), roundPrecision), // Average
102-
utils.DurationRoundBy(total.P90, roundPrecision),
103-
utils.DurationRoundBy(total.P95, roundPrecision),
104-
utils.DurationRoundBy(total.P99, roundPrecision),
84+
totalCount,
85+
utils.DurationRoundBy(totalDurations[0], roundPrecision),
86+
utils.DurationRoundBy(totalDurations[len(totalDurations)-1], roundPrecision),
87+
utils.DurationRoundBy(totalSum/time.Duration(totalCount), roundPrecision), // Average
88+
utils.DurationRoundBy(totalDurations[int(0.90*allDurationsLenAsFloat)], roundPrecision),
89+
utils.DurationRoundBy(totalDurations[int(0.95*allDurationsLenAsFloat)], roundPrecision),
90+
utils.DurationRoundBy(totalDurations[int(0.99*allDurationsLenAsFloat)], roundPrecision),
10591
})
10692
}
10793
t.Render()

requests/run.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func releaseDodos(
6666
streamWG sync.WaitGroup
6767
requestCountPerDodo uint
6868
dodosCount = requestConfig.GetValidDodosCountForRequests()
69-
responses = make([][]*Response, dodosCount)
69+
responses = make([][]Response, dodosCount)
7070
increase = make(chan int64, requestConfig.RequestCount)
7171
)
7272

@@ -123,7 +123,7 @@ func sendRequestByCount(
123123
request *Request,
124124
timeout time.Duration,
125125
requestCount uint,
126-
responseData *[]*Response,
126+
responseData *[]Response,
127127
increase chan<- int64,
128128
wg *sync.WaitGroup,
129129
) {
@@ -146,15 +146,15 @@ func sendRequestByCount(
146146
if err == types.ErrInterrupt {
147147
return
148148
}
149-
*responseData = append(*responseData, &Response{
149+
*responseData = append(*responseData, Response{
150150
Response: err.Error(),
151151
Time: completedTime,
152152
})
153153
increase <- 1
154154
return
155155
}
156156

157-
*responseData = append(*responseData, &Response{
157+
*responseData = append(*responseData, Response{
158158
Response: strconv.Itoa(response.StatusCode()),
159159
Time: completedTime,
160160
})
@@ -170,7 +170,7 @@ func sendRequest(
170170
ctx context.Context,
171171
request *Request,
172172
timeout time.Duration,
173-
responseData *[]*Response,
173+
responseData *[]Response,
174174
increase chan<- int64,
175175
wg *sync.WaitGroup,
176176
) {
@@ -193,15 +193,15 @@ func sendRequest(
193193
if err == types.ErrInterrupt {
194194
return
195195
}
196-
*responseData = append(*responseData, &Response{
196+
*responseData = append(*responseData, Response{
197197
Response: err.Error(),
198198
Time: completedTime,
199199
})
200200
increase <- 1
201201
return
202202
}
203203

204-
*responseData = append(*responseData, &Response{
204+
*responseData = append(*responseData, Response{
205205
Response: strconv.Itoa(response.StatusCode()),
206206
Time: completedTime,
207207
})

types/durations.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types
22

33
import (
4+
"slices"
45
"sort"
56
"time"
67
)
@@ -14,9 +15,7 @@ func (d Durations) Sort(ascending ...bool) {
1415
return d[i] > d[j]
1516
})
1617
} else { // Otherwise, sort in ascending order
17-
sort.Slice(d, func(i, j int) bool {
18-
return d[i] < d[j]
19-
})
18+
slices.Sort(d)
2019
}
2120
}
2221

utils/slice.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package utils
22

33
import "math/rand"
44

5-
func Flatten[T any](nested [][]*T) []*T {
6-
flattened := make([]*T, 0)
5+
func Flatten[T any](nested [][]T) []T {
6+
flattened := make([]T, 0)
77
for _, n := range nested {
88
flattened = append(flattened, n...)
99
}

0 commit comments

Comments
 (0)