forked from rakyll/hey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
351 lines (315 loc) · 8.53 KB
/
report.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package requester
import (
"bytes"
"fmt"
"io"
"log"
"sort"
"time"
)
const (
barChar = "■"
)
// We report for max 1M results.
const maxRes = 1000000
type report struct {
avgTotal float64
fastest float64
slowest float64
average float64
rps float64
avgConn float64
avgDNS float64
avgTLS float64
avgReq float64
avgRes float64
avgDelay float64
connLats []float64
dnsLats []float64
tlsLats []float64
reqLats []float64
resLats []float64
delayLats []float64
offsets []float64
statusCodes []int
results chan *result
done chan bool
total time.Duration
errorDist map[string]int
lats []float64
sizeTotal int64
numRes int64
output string
w io.Writer
}
func newReport(w io.Writer, results chan *result, output string, n int) *report {
cap := min(n, maxRes)
return &report{
output: output,
results: results,
done: make(chan bool, 1),
errorDist: make(map[string]int),
w: w,
connLats: make([]float64, 0, cap),
dnsLats: make([]float64, 0, cap),
tlsLats: make([]float64, 0, cap),
reqLats: make([]float64, 0, cap),
resLats: make([]float64, 0, cap),
delayLats: make([]float64, 0, cap),
lats: make([]float64, 0, cap),
statusCodes: make([]int, 0, cap),
}
}
func runReporter(r *report) {
// Loop will continue until channel is closed
for res := range r.results {
r.numRes++
if res.err != nil {
r.errorDist[res.err.Error()]++
} else {
r.avgTotal += res.duration.Seconds()
r.avgConn += res.connDuration.Seconds()
r.avgDelay += res.delayDuration.Seconds()
r.avgDNS += res.dnsDuration.Seconds()
r.avgTLS += res.tlsDuration.Seconds()
r.avgReq += res.reqDuration.Seconds()
r.avgRes += res.resDuration.Seconds()
if len(r.resLats) < maxRes {
r.lats = append(r.lats, res.duration.Seconds())
r.connLats = append(r.connLats, res.connDuration.Seconds())
r.dnsLats = append(r.dnsLats, res.dnsDuration.Seconds())
r.tlsLats = append(r.tlsLats, res.tlsDuration.Seconds())
r.reqLats = append(r.reqLats, res.reqDuration.Seconds())
r.delayLats = append(r.delayLats, res.delayDuration.Seconds())
r.resLats = append(r.resLats, res.resDuration.Seconds())
r.statusCodes = append(r.statusCodes, res.statusCode)
r.offsets = append(r.offsets, res.offset.Seconds())
}
if res.contentLength > 0 {
r.sizeTotal += res.contentLength
}
}
}
// Signal reporter is done.
r.done <- true
}
func (r *report) finalize(total time.Duration) {
r.total = total
r.rps = float64(r.numRes) / r.total.Seconds()
r.average = r.avgTotal / float64(len(r.lats))
r.avgConn = r.avgConn / float64(len(r.connLats))
r.avgDelay = r.avgDelay / float64(len(r.delayLats))
r.avgDNS = r.avgDNS / float64(len(r.dnsLats))
r.avgTLS = r.avgTLS / float64(len(r.tlsLats))
r.avgReq = r.avgReq / float64(len(r.reqLats))
r.avgRes = r.avgRes / float64(len(r.resLats))
r.print()
}
func (r *report) print() {
buf := &bytes.Buffer{}
if err := newTemplate(r.output).Execute(buf, r.snapshot()); err != nil {
log.Println("error:", err.Error())
return
}
r.printf(buf.String())
r.printf("\n")
}
func (r *report) printf(s string, v ...interface{}) {
fmt.Fprintf(r.w, s, v...)
}
func (r *report) snapshot() Report {
snapshot := Report{
AvgTotal: r.avgTotal,
Average: r.average,
Rps: r.rps,
SizeTotal: r.sizeTotal,
AvgConn: r.avgConn,
AvgDNS: r.avgDNS,
AvgTLS: r.avgTLS,
AvgReq: r.avgReq,
AvgRes: r.avgRes,
AvgDelay: r.avgDelay,
Total: r.total,
ErrorDist: r.errorDist,
NumRes: r.numRes,
Lats: make([]float64, len(r.lats)),
ConnLats: make([]float64, len(r.lats)),
DnsLats: make([]float64, len(r.lats)),
TlsLats: make([]float64, len(r.lats)),
ReqLats: make([]float64, len(r.lats)),
ResLats: make([]float64, len(r.lats)),
DelayLats: make([]float64, len(r.lats)),
Offsets: make([]float64, len(r.lats)),
StatusCodes: make([]int, len(r.lats)),
}
if len(r.lats) == 0 {
return snapshot
}
snapshot.SizeReq = r.sizeTotal / int64(len(r.lats))
copy(snapshot.Lats, r.lats)
copy(snapshot.ConnLats, r.connLats)
copy(snapshot.DnsLats, r.dnsLats)
copy(snapshot.TlsLats, r.tlsLats)
copy(snapshot.ReqLats, r.reqLats)
copy(snapshot.ResLats, r.resLats)
copy(snapshot.DelayLats, r.delayLats)
copy(snapshot.StatusCodes, r.statusCodes)
copy(snapshot.Offsets, r.offsets)
sort.Float64s(r.lats)
r.fastest = r.lats[0]
r.slowest = r.lats[len(r.lats)-1]
sort.Float64s(r.connLats)
sort.Float64s(r.dnsLats)
sort.Float64s(r.tlsLats)
sort.Float64s(r.reqLats)
sort.Float64s(r.resLats)
sort.Float64s(r.delayLats)
// TODO: consider other histograms?
snapshot.Histogram = r.histogram(r.lats)
snapshot.LatencyDistribution = r.latencies()
snapshot.Fastest = r.fastest
snapshot.Slowest = r.slowest
snapshot.ConnMax = r.connLats[0]
snapshot.ConnMin = r.connLats[len(r.connLats)-1]
snapshot.DnsMax = r.dnsLats[0]
snapshot.DnsMin = r.dnsLats[len(r.dnsLats)-1]
if len(r.tlsLats) > 0 {
snapshot.TlsMax = r.tlsLats[0]
snapshot.TlsMin = r.tlsLats[len(r.tlsLats)-1]
}
snapshot.ReqMax = r.reqLats[0]
snapshot.ReqMin = r.reqLats[len(r.reqLats)-1]
snapshot.DelayMax = r.delayLats[0]
snapshot.DelayMin = r.delayLats[len(r.delayLats)-1]
snapshot.ResMax = r.resLats[0]
snapshot.ResMin = r.resLats[len(r.resLats)-1]
statusCodeDist := make(map[int]int, len(snapshot.StatusCodes))
for _, statusCode := range snapshot.StatusCodes {
statusCodeDist[statusCode]++
}
snapshot.StatusCodeDist = statusCodeDist
return snapshot
}
func (r *report) latencies() []LatencyDistribution {
pctls := []int{10, 25, 50, 75, 90, 95, 99}
data := make([]float64, len(pctls))
j := 0
// TODO: loop over percentiles
// For each percentile, create the latency distribution directly
for i := 0; i < len(r.lats) && j < len(pctls); i++ {
current := i * 100 / len(r.lats)
if current >= pctls[j] {
data[j] = r.lats[i]
j++
}
}
res := make([]LatencyDistribution, len(pctls))
for i := 0; i < len(pctls); i++ {
if data[i] > 0 {
res[i] = LatencyDistribution{Percentage: pctls[i], Latency: data[i]}
}
}
return res
}
func (r *report) histogram(data []float64) []Bucket {
bc := 10
buckets := make([]float64, bc+1)
counts := make([]int, bc+1)
bs := (r.slowest - r.fastest) / float64(bc)
for i := 0; i < bc; i++ {
buckets[i] = r.fastest + bs*float64(i)
}
buckets[bc] = r.slowest
var bi int
var maximum int
for i := 0; i < len(data); {
if data[i] <= buckets[bi] {
i++
counts[bi]++
if maximum < counts[bi] {
maximum = counts[bi]
}
} else if bi < len(buckets)-1 {
bi++
}
}
res := make([]Bucket, len(buckets))
for i := 0; i < len(buckets); i++ {
res[i] = Bucket{
Mark: buckets[i],
Count: counts[i],
Frequency: float64(counts[i]) / float64(len(r.lats)),
}
}
return res
}
type Report struct {
AvgTotal float64
Fastest float64
Slowest float64
Average float64
Rps float64
AvgConn float64
AvgDNS float64
AvgTLS float64
AvgReq float64
AvgRes float64
AvgDelay float64
ConnMax float64
ConnMin float64
DnsMax float64
DnsMin float64
TlsMax float64
TlsMin float64
ReqMax float64
ReqMin float64
ResMax float64
ResMin float64
DelayMax float64
DelayMin float64
Lats []float64
ConnLats []float64
DnsLats []float64
TlsLats []float64
ReqLats []float64
ResLats []float64
DelayLats []float64
Offsets []float64
StatusCodes []int
Total time.Duration
ErrorDist map[string]int
StatusCodeDist map[int]int
SizeTotal int64
SizeReq int64
NumRes int64
LatencyDistribution []LatencyDistribution
Histogram []Bucket
}
type LatencyDistribution struct {
Percentage int
Latency float64
DnsLatency float64
ConnLatency float64
TlsLatency float64
DelayLatency float64
ReqLatency float64
RespLatency float64
}
type Bucket struct {
Mark float64
Count int
Frequency float64
}