-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpstat.go
103 lines (87 loc) · 2.51 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
package httpstat
import (
"crypto/tls"
"net/http/httptrace"
"time"
)
type Tracer struct {
getConn time.Time
dnsStart time.Time
dnsDone time.Time
connectStart time.Time
connectDone time.Time
tlsHandshakeStart time.Time
tlsHandshakeDone time.Time
gotConn time.Time
wroteRequest time.Time
gotFirstResponseByte time.Time
Stat
}
type Stat struct {
DNSLookup time.Duration
TCPConnection time.Duration
TLSHandshake time.Duration
// Waiting to get a network connection.
WaitingConnection time.Duration
Sending time.Duration
// Waiting for server first byte.
WaitingServer time.Duration
Receiving time.Duration
}
func (t *Tracer) DNSStart(_ httptrace.DNSStartInfo) {
t.dnsStart = time.Now()
}
func (t *Tracer) DNSDone(_ httptrace.DNSDoneInfo) {
t.dnsDone = time.Now()
}
func (t *Tracer) ConnectStart(_, _ string) {
t.connectStart = time.Now()
}
func (t *Tracer) ConnectDone(_, _ string, _ error) {
t.connectDone = time.Now()
}
func (t *Tracer) GetConn(_ string) {
t.getConn = time.Now()
}
func (t *Tracer) GotConn(_ httptrace.GotConnInfo) {
t.gotConn = time.Now()
}
func (t *Tracer) GotFirstResponseByte() {
t.gotFirstResponseByte = time.Now()
}
func (t *Tracer) TLSHandshakeStart() {
t.tlsHandshakeStart = time.Now()
}
func (t *Tracer) TLSHandshakeDone(_ tls.ConnectionState, _ error) {
t.tlsHandshakeDone = time.Now()
}
func (t *Tracer) WroteRequest(_ httptrace.WroteRequestInfo) {
t.wroteRequest = time.Now()
}
func (t *Tracer) ClientTrace() *httptrace.ClientTrace {
return &httptrace.ClientTrace{
GetConn: t.GetConn,
GotConn: t.GotConn,
GotFirstResponseByte: t.GotFirstResponseByte,
DNSStart: t.DNSStart,
DNSDone: t.DNSDone,
ConnectStart: t.ConnectStart,
ConnectDone: t.ConnectDone,
TLSHandshakeStart: t.TLSHandshakeStart,
TLSHandshakeDone: t.TLSHandshakeDone,
WroteRequest: t.WroteRequest,
}
}
func (t *Tracer) Done(now time.Time) {
t.DNSLookup = t.dnsDone.Sub(t.dnsStart)
t.TCPConnection = t.connectDone.Sub(t.connectStart)
// TODO: Investigate why connectStart is called but connectDone is not called.
if t.TCPConnection < 0 {
t.TCPConnection = 0
}
t.TLSHandshake = t.tlsHandshakeDone.Sub(t.tlsHandshakeStart)
t.WaitingConnection = t.gotConn.Sub(t.getConn)
t.Sending = t.wroteRequest.Sub(t.gotConn)
t.WaitingServer = t.gotFirstResponseByte.Sub(t.wroteRequest)
t.Receiving = now.Sub(t.gotFirstResponseByte)
}