-
Notifications
You must be signed in to change notification settings - Fork 11
/
httpstat.go
170 lines (140 loc) · 3.36 KB
/
httpstat.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
package httpstat
import (
"context"
"crypto/tls"
"fmt"
"net/http/httptrace"
"time"
)
// TODO: TimeResponse etc should have the end time stored,
// currently only relevant to the final request
// TODO: store more request information for redirects,
// such as the url :)
// Trace results.
type Trace interface {
Address() string
TLS() bool
Start() time.Time
TimeDNS() time.Duration
TimeConnect() time.Duration
TimeTLS() time.Duration
TimeWait() time.Duration
TimeResponse(time.Time) time.Duration
TimeDownload(time.Time) time.Duration
TimeTotal(time.Time) time.Duration
Stats() *Stats
}
type trace struct {
addr string
tls bool
start time.Time
dnsStart time.Time
dnsEnd time.Time
tcpStart time.Time
tcpEnd time.Time
tlsStart time.Time
tlsEnd time.Time
waitStart time.Time
waitEnd time.Time
}
// TLS implementation.
func (t *trace) TLS() bool {
return t.tls
}
// Address implementation.
func (t *trace) Address() string {
return t.addr
}
// Start implementation.
func (t *trace) Start() time.Time {
return t.start
}
// TimeDNS implementation.
func (t *trace) TimeDNS() time.Duration {
return t.dnsEnd.Sub(t.dnsStart)
}
// TimeConnect implementation.
func (t *trace) TimeConnect() time.Duration {
return t.tcpEnd.Sub(t.tcpStart)
}
// TimeTLS implementation.
func (t *trace) TimeTLS() time.Duration {
return t.tlsEnd.Sub(t.tlsStart)
}
// TimeWait implementation.
func (t *trace) TimeWait() time.Duration {
return t.waitEnd.Sub(t.waitStart)
}
// TimeDownload implementation.
func (t *trace) TimeDownload(now time.Time) time.Duration {
return now.Sub(t.waitEnd)
}
// TimeResponse implementation.
func (t *trace) TimeResponse(now time.Time) time.Duration {
return now.Sub(t.waitStart)
}
// TimeTotal implementation.
func (t *trace) TimeTotal(now time.Time) time.Duration {
return now.Sub(t.start)
}
// WithTraces traces request timings.
func WithTraces(ctx context.Context, traces *[]Trace) context.Context {
var t *trace
return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
GetConn: func(addr string) {
t = &trace{}
t.start = time.Now()
t.addr = addr
},
GotConn: func(info httptrace.GotConnInfo) {
if info.Reused {
t = &trace{}
t.start = time.Now()
}
*traces = append(*traces, t)
},
DNSStart: func(info httptrace.DNSStartInfo) {
t.dnsStart = time.Now()
},
DNSDone: func(info httptrace.DNSDoneInfo) {
t.dnsEnd = time.Now()
},
ConnectStart: func(network, addr string) {
t.tcpStart = time.Now()
},
ConnectDone: func(network, addr string, err error) {
t.tcpEnd = time.Now()
},
TLSHandshakeStart: func() {
t.tls = true
t.tlsStart = time.Now()
},
TLSHandshakeDone: func(_ tls.ConnectionState, _ error) {
t.tlsEnd = time.Now()
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
t.waitStart = time.Now()
},
GotFirstResponseByte: func() {
t.waitEnd = time.Now()
},
})
}
// Stats returns a struct of stats.
func (t trace) Stats() *Stats {
now := time.Now()
return &Stats{
TLS: t.TLS(),
TimeDNS: t.TimeDNS(),
TimeConnect: t.TimeConnect(),
TimeTLS: t.TimeTLS(),
TimeWait: t.TimeWait(),
TimeResponse: t.TimeResponse(now),
TimeDownload: t.TimeDownload(now),
TimeTotal: t.TimeTotal(now),
}
}
// Millisecond formatter.
func ms(d time.Duration) string {
return fmt.Sprintf("%.0fms", float64(d)/float64(time.Millisecond))
}