-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
226 lines (188 loc) · 6.59 KB
/
stat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package httpfile
import (
"fmt"
"io"
"sort"
"strconv"
"time"
)
// Stat is the file execute time
type Stat struct {
Requests int
TimeConsuming float64
BytesSend int
BytesReceived int
Successed int
Failed int
}
// Report is the statatics of results
type Report struct {
TotalRequests int
Currency int
Successed int
RateLimit int
Failed int
TotalSend int
TotalRecv int
RequestTotalTimeUsed float64
ResponseTotalTimeUsed float64
RequestPerSecond int
ResponsePerSecond int
SendSpeed float64
RecvSpeed float64
AvgTimeUsed float64
MaxTimeUsed float64
MinTimeUsed float64
P50TimeUsed float64
P75TimeUsed float64
P90TimeUsed float64
P95TimeUsed float64
P99TimeUsed float64
Stats []Stat
}
func pos(n int, p float64) int {
pn := int(float64(n) * p)
if pn >= n {
pn = n - 1
}
return pn
}
func removeFailed(stats []Stat) []Stat {
i := 0
for _, s := range stats {
if s.Successed > 0 {
stats[i] = s
i++
}
}
return stats[0:i]
}
// ReportStat generate report for stats
func ReportStat(stats []Stat, totalTimeUsed float64) Report {
var report Report
if len(stats) == 0 {
return report
}
sort.Slice(stats, func(i, j int) bool {
return stats[i].TimeConsuming < stats[j].TimeConsuming
})
sumTimeUsed := 0.0
report.TotalRequests = len(stats)
for _, s := range stats {
report.TotalRecv = report.TotalRecv + s.BytesReceived
report.TotalSend = report.TotalSend + s.BytesSend
report.Successed += s.Successed
report.Failed += s.Failed
sumTimeUsed = sumTimeUsed + s.TimeConsuming
}
report.RequestTotalTimeUsed = totalTimeUsed
report.RecvSpeed = float64(report.TotalRecv) / float64(report.RequestTotalTimeUsed)
report.SendSpeed = float64(report.TotalSend) / float64(report.RequestTotalTimeUsed)
report.ResponseTotalTimeUsed = sumTimeUsed
report.AvgTimeUsed = sumTimeUsed / float64(report.Successed)
report.RequestPerSecond = int((1.0 / report.RequestTotalTimeUsed) * float64(report.Successed))
report.ResponsePerSecond = int((1.0 / report.ResponseTotalTimeUsed) * float64(report.Successed))
ss := removeFailed(stats)
report.MinTimeUsed = ss[0].TimeConsuming
report.MaxTimeUsed = ss[len(ss)-1].TimeConsuming
report.P50TimeUsed = ss[pos(len(ss), 0.50)].TimeConsuming
report.P75TimeUsed = ss[pos(len(ss), 0.75)].TimeConsuming
report.P90TimeUsed = ss[pos(len(ss), 0.90)].TimeConsuming
report.P95TimeUsed = ss[pos(len(ss), 0.95)].TimeConsuming
report.P99TimeUsed = ss[pos(len(ss), 0.99)].TimeConsuming
report.Stats = stats
return report
}
// PlainOutput is plain output of report
func PlainOutput(report *Report, w io.Writer) {
format := "%-20v: %v\n"
fmt.Fprintf(w, format, "Total Requests", report.TotalRequests)
fmt.Fprintf(w, format, "Currency", report.Currency)
fmt.Fprintf(w, format, "Successed", report.Successed)
fmt.Fprintf(w, format, "RateLimit", report.Successed)
fmt.Fprintf(w, format, "Failed", report.Failed)
fmt.Fprintf(w, format, "Request Time Used", report.RequestTotalTimeUsed)
fmt.Fprintf(w, format, "Reqeust Per Second", report.RequestPerSecond)
fmt.Fprintf(w, format, "Send Speed", report.SendSpeed)
fmt.Fprintf(w, format, "Recv Speed", report.RecvSpeed)
fmt.Fprintln(w)
fmt.Fprintf(w, format, "Response Time Used", report.ResponseTotalTimeUsed)
fmt.Fprintf(w, format, "Response Per Second", report.ResponsePerSecond)
fmt.Fprintln(w)
fmt.Fprintf(w, format, "Total Time Used", report.ResponseTotalTimeUsed)
fmt.Fprintf(w, format, "Avg Time Used", report.AvgTimeUsed)
fmt.Fprintf(w, format, "Min Time Used", report.MinTimeUsed)
fmt.Fprintf(w, format, "Max Time Used", report.MaxTimeUsed)
fmt.Fprintln(w)
fmt.Fprintf(w, format, "P50 Time Used", report.P50TimeUsed)
fmt.Fprintf(w, format, "P75 Time Used", report.P75TimeUsed)
fmt.Fprintf(w, format, "P90 Time Used", report.P90TimeUsed)
fmt.Fprintf(w, format, "P95 Time Used", report.P95TimeUsed)
fmt.Fprintf(w, format, "P99 Time Used", report.P99TimeUsed)
}
func thoundsNumber(n int) string {
in := strconv.Itoa(n)
numOfDigits := len(in)
if n < 0 {
numOfDigits-- // First character is the - sign (not a digit)
}
numOfCommas := (numOfDigits - 1) / 3
out := make([]byte, len(in)+numOfCommas)
if n < 0 {
in, out[0] = in[1:], '-'
}
for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 {
out[j] = in[i]
if i == 0 {
return string(out)
}
if k++; k == 3 {
j, k = j-1, 0
out[j] = ','
}
}
}
func humanDuration(val float64) string {
d := time.Duration(val * float64(time.Second))
return d.String()
}
func bytesNumber(val float64) string {
unit := "B"
if val < 1024*1024 {
unit = "K"
val = val / (1024)
} else if val < 1024*1024*1024 {
unit = "M"
val = val / (1024 * 1024)
} else {
unit = "G"
val = val / (1024 * 1024 * 1024)
}
return fmt.Sprintf("%.3f%s", val, unit)
}
// HumanOutput is plain output of report
func HumanOutput(report *Report, w io.Writer) {
format := "%-20v: %v%v\n"
fmt.Fprintf(w, format, "Total Requests", thoundsNumber(report.TotalRequests), "")
fmt.Fprintf(w, format, "Currency", thoundsNumber(report.Currency), "")
fmt.Fprintf(w, format, "Successed", thoundsNumber(report.Successed), "")
fmt.Fprintf(w, format, "Failed", thoundsNumber(report.Failed), "")
fmt.Fprintln(w)
fmt.Fprintf(w, format, "Request Time Used", humanDuration(report.RequestTotalTimeUsed), " (with request rate limit wait)")
fmt.Fprintf(w, format, "Reqeust Per Second", thoundsNumber(report.RequestPerSecond), "/S")
fmt.Fprintf(w, format, "Send Speed", bytesNumber(report.SendSpeed), "/S")
fmt.Fprintf(w, format, "Recv Speed", bytesNumber(report.RecvSpeed), "/S")
fmt.Fprintln(w)
fmt.Fprintf(w, format, "Response Time Used", humanDuration(report.ResponseTotalTimeUsed), " (without request rate limit wait)")
fmt.Fprintf(w, format, "Response Per Second", thoundsNumber(report.ResponsePerSecond), "/S")
fmt.Fprintln(w)
fmt.Fprintf(w, format, "Avg Time Used", humanDuration(report.AvgTimeUsed), "")
fmt.Fprintf(w, format, "Min Time Used", humanDuration(report.MinTimeUsed), "")
fmt.Fprintf(w, format, "Max Time Used", humanDuration(report.MaxTimeUsed), "")
fmt.Fprintln(w)
fmt.Fprintf(w, format, "P50 Time Used", humanDuration(report.P50TimeUsed), "")
fmt.Fprintf(w, format, "P75 Time Used", humanDuration(report.P75TimeUsed), "")
fmt.Fprintf(w, format, "P90 Time Used", humanDuration(report.P90TimeUsed), "")
fmt.Fprintf(w, format, "P95 Time Used", humanDuration(report.P95TimeUsed), "")
fmt.Fprintf(w, format, "P99 Time Used", humanDuration(report.P99TimeUsed), "")
}