-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpstats.go
354 lines (293 loc) · 7.97 KB
/
httpstats.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
352
353
354
package stress
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
urlModule "net/url"
"os"
"reflect"
"sort"
"text/tabwriter"
"time"
"github.com/muravjov/slog/base"
)
type Statuses map[string]int64
type StatsType struct {
Statuses Statuses
// contains the list of all done requests
// 10 seconds of heavy stress doesn't eat much memory:
// 10 seconds * 6000 prs = 60000 float64 = 60000 * 8 = 480000 < 0.5 Megabyte
Times []float64
}
type RequestContext struct {
Client *http.Client
Stats *StatsType
IncrStat func(msg string, dur float64)
WaitStatsReady func()
}
type RequestContextOptions struct {
KeepAlive bool
StressTimeout float64
TLSConfigFile string
}
func NewRequestContext() *RequestContext {
rco := &RequestContextOptions{
KeepAlive: false,
}
return NewRequestContextEx(rco)
}
func NewRequestContextEx(rco *RequestContextOptions) *RequestContext {
rc := &RequestContext{}
var rt http.RoundTripper
var customTransport *http.Transport
makeCustomTransport := func() {
if customTransport != nil {
return
}
transport := &http.Transport{}
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
base.Assert(ok)
// :TRICKY: make altered variant of http.DefaultTransport - no way to make simpler :(
// "assigment copies lock value (sync.Mutex)"
//*transport = *defaultTransport
toElem := func(i interface{}) reflect.Value {
return reflect.ValueOf(i).Elem()
}
tValue, dtValue := toElem(transport), toElem(defaultTransport)
// list of attrs see at http.DefaultTransport initialization
for _, name := range []string{
"Proxy",
"DialContext",
"MaxIdleConns",
"IdleConnTimeout",
"TLSHandshakeTimeout",
"ExpectContinueTimeout",
} {
tValue.FieldByName(name).Set(dtValue.FieldByName(name))
}
customTransport = transport
rt = transport
}
if !rco.KeepAlive {
makeCustomTransport()
customTransport.DisableKeepAlives = true
}
if rco.TLSConfigFile != "" {
tlsConfig := NewTLSConfig(rco.TLSConfigFile)
if tlsConfig != nil {
makeCustomTransport()
customTransport.TLSClientConfig = tlsConfig
}
}
client := &http.Client{Transport: rt}
rc.Client = client
client.Timeout = time.Duration(float64(time.Second) * rco.StressTimeout)
// for the sake of simplicity, let's choose sync.Map
// https://medium.com/@deckarep/the-new-kid-in-town-gos-sync-map-de24a6bf7c2c
//var stats sync.Map
//
// No, unnecessarily difficult for now,- go for channels
statuses := make(map[string]int64)
stats := &StatsType{
statuses,
nil,
}
rc.Stats = stats
type requestStat struct {
msg string
duration float64 // seconds
}
statChan := make(chan requestStat, 1000)
statDone := NewEvent()
go func() {
for rs := range statChan {
msg := rs.msg
counter, ok := statuses[msg]
if ok {
counter = counter + 1
} else {
counter = 1
}
statuses[msg] = counter
stats.Times = append(stats.Times, rs.duration)
}
statDone.Done()
}()
rc.IncrStat = func(msg string, dur float64) {
statChan <- requestStat{msg, dur}
}
rc.WaitStatsReady = func() {
close(statChan)
statDone.Wait()
}
return rc
}
// :TRICKY: see http.errServerClosedIdle - upexported
var errServerClosedIdle error
func IsServerClosedIdle(err error) bool {
if errServerClosedIdle != nil {
return err == errServerClosedIdle
}
if err.Error() == "http: server closed idle connection" {
errServerClosedIdle = err
return true
}
return false
}
func ExecuteRequest(req *http.Request, rc *RequestContext) {
// https://blog.golang.org/http-tracing
gotConn, gotFirstResponseByte := false, false
trace := &httptrace.ClientTrace{
GotConn: func(connInfo httptrace.GotConnInfo) {
gotConn = true
},
GotFirstResponseByte: func() {
gotFirstResponseByte = true
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
now := time.Now()
appendStat := func(msg string) {
dur := measureTime(now)
rc.IncrStat(msg, dur)
}
// :TRICKY: main error checking occurs in transport.go:persistConn.readLoop()
resp, err := rc.Client.Do(req)
if err != nil {
var hint string
// client.Do() wraps real error with "method: url: <real error>",
// it is inconveniently
switch errTyped := err.(type) {
case *urlModule.Error:
err = errTyped.Err
if err == io.EOF {
// so weird looking "EOF"
hint = fmt.Sprintf("GotConn=%t, GotFirstResponseByte=%t", gotConn, gotFirstResponseByte)
} else if IsServerClosedIdle(err) {
hint = "client stays keepalive after request, but server closes the connection"
} else if errTyped.Timeout() {
// really that is err.(net.Error) and unexported httpError
hint = "non-zero timeout hit"
}
}
msg := err.Error()
if hint != "" {
msg = fmt.Sprintf("%s (%s)", msg, hint)
}
appendStat(msg)
return
}
defer resp.Body.Close()
//fmt.Println(resp.StatusCode)
//bdat, err := ioutil.ReadAll(resp.Body)
//base.CheckError(err)
//fmt.Println(string(bdat))
io.Copy(ioutil.Discard, resp.Body)
switch dig := resp.StatusCode / 100; dig {
case 2, 4, 5:
appendStat(fmt.Sprintf("%dxx", dig))
default:
appendStat(resp.Status)
}
}
func ExecuteGet(url string, rc *RequestContext) {
var body io.Reader = nil
req, err := http.NewRequest("GET", url, body)
base.CheckError(err)
//req.Header.Add("If-None-Match", `W/"wyzzy"`)
ExecuteRequest(req, rc)
}
type StatElement struct {
Key string
Counter int64
}
type StatList []StatElement
func (p StatList) Len() int { return len(p) }
// reverse order
func (p StatList) Less(i, j int) bool { return p[j].Counter < p[i].Counter }
func (p StatList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func PrintReport(stats *StatsType, stressTimes StressTimes, aggregationCount int) {
statuses := stats.Statuses
var lst StatList
var totals int64 = 0
successTotals := statuses["2xx"]
for k, v := range statuses {
lst = append(lst, StatElement{k, v})
totals += v
}
sort.Sort(lst)
if len(lst) > aggregationCount {
tail := lst[aggregationCount-1:]
lst = lst[:aggregationCount-1]
var tailCounter int64 = 0
for _, se := range tail {
tailCounter += se.Counter
}
lst = append(lst, StatElement{
"Other Stats",
tailCounter,
})
// again, to make "Other Stats" in the right order
sort.Sort(lst)
}
getRatio := func(counter int64, totals float64) string {
percent := "n/a"
if totals > 0 {
percent = fmt.Sprintf("%.2f", float64(counter)/float64(totals))
}
return percent
}
getPercent := func(counter int64, totals float64) string {
return getRatio(counter*100, totals)
}
// Print
fmt.Print(`
=======
Report
`)
fmt.Println("Counter Table:")
fmt.Printf("%%\t%s\t%s\n", "Counter", "Name")
for _, se := range lst {
fmt.Printf("%s\t%d\t\"%s\"\n", getPercent(se.Counter, float64(totals)), se.Counter, se.Key)
}
fmt.Println("\nTotals:")
const padding = 3
var twFlags uint = 0 // tabwriter.Debug
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', twFlags)
writeColumn := func(format string, a ...interface{}) {
fmt.Fprintln(w, fmt.Sprintf(format, a...))
}
timeElapsed := stressTimes.ElapsedTime
writeColumn("Number of Requests:\t%d", totals)
writeColumn("Time Elapsed:\t%.2f seconds", timeElapsed)
writeColumn("Requests per Second:\t%s", getRatio(totals, timeElapsed))
writeColumn("Jobs Spawning per Second:\t%s", getRatio(totals, stressTimes.SpawningJobsTime))
writeColumn("Success percent (HTTP 2xx):\t%s", getPercent(successTotals, float64(totals)))
w.Write([]byte("\n"))
times := stats.Times
sort.Float64s(times)
writeTime := func(title string, fn func() float64) {
value := "n/a"
if ln := len(times); ln > 0 {
value = fmt.Sprintf("%f", fn())
}
writeColumn("%s, seconds:\t%s", title, value)
}
writeTime("Worst request duration", func() float64 {
return times[len(times)-1]
})
writeTime("Mean request duration", func() float64 {
var sum float64
for _, d := range times {
sum += d
}
return sum / float64(len(times))
})
writeTime("95ptl request duration", func() float64 {
idx := int(float64(len(times)) * 0.95)
return times[idx]
})
w.Flush()
}