@@ -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.
2121func (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 ()
0 commit comments