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
|
package rpm
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/http/httptrace"
"time"
"github.com/network-quality/goresponsiveness/debug"
"github.com/network-quality/goresponsiveness/stats"
"github.com/network-quality/goresponsiveness/traceable"
"github.com/network-quality/goresponsiveness/utilities"
)
type Probe struct {
client *http.Client
stats *stats.TraceStats
trace *httptrace.ClientTrace
debug debug.DebugLevel
probeid uint64
}
func (p *Probe) String() string {
return fmt.Sprintf("(Probe %v): stats: %v\n", p.probeid, p.stats)
}
func (p *Probe) ProbeId() uint64 {
return p.probeid
}
func (p *Probe) GetTrace() *httptrace.ClientTrace {
return p.trace
}
func (p *Probe) GetDnsDelta() time.Duration {
delta := p.stats.DnsDoneTime.Sub(p.stats.DnsStartTime)
if debug.IsDebug(p.debug) {
fmt.Printf("(Probe %v): DNS Time: %v\n", p.probeid, delta)
}
return delta
}
func (p *Probe) GetTCPDelta() time.Duration {
delta := p.stats.ConnectDoneTime.Sub(p.stats.ConnectStartTime)
if debug.IsDebug(p.debug) {
fmt.Printf("(Probe %v): TCP Connection Time: %v\n", p.probeid, delta)
}
return delta
}
func (p *Probe) GetTLSDelta() time.Duration {
if utilities.IsSome(p.stats.TLSDoneTime) {
panic("There should not be TLS information, but there is.")
}
delta := time.Duration(0)
if debug.IsDebug(p.debug) {
fmt.Printf("(Probe %v): TLS Time: %v\n", p.probeid, delta)
}
return delta
}
func (p *Probe) GetTLSAndHttpHeaderDelta() time.Duration {
// Because the TLS handshake occurs *after* the TCP connection (ConnectDoneTime)
// and *before* the HTTP transaction, we know that the delta between the time
// that the first HTTP response byte is available and the time that the TCP
// connection was established includes both the time for the HTTP header RTT
// *and* the TLS handshake RTT, whether we can specifically measure the latter
// or not. Eventually when TLS handshake tracing is fixed, we can break these
// into separate buckets, but for now this workaround is reasonable.
delta := p.stats.HttpResponseReadyTime.Sub(p.stats.ConnectDoneTime)
if debug.IsDebug(p.debug) {
fmt.Printf("(Probe %v): Http TLS and Header Time: %v\n", p.probeid, delta)
}
return delta
}
func (p *Probe) GetHttpHeaderDelta() time.Duration {
panic("Unusable until TLS tracing support is enabled! Use GetTLSAndHttpHeaderDelta() instead.\n")
delta := p.stats.HttpResponseReadyTime.Sub(utilities.GetSome(p.stats.TLSDoneTime))
if debug.IsDebug(p.debug) {
fmt.Printf("(Probe %v): Http Header Time: %v\n", p.probeid, delta)
}
return delta
}
func (p *Probe) GetHttpDownloadDelta(httpDoneTime time.Time) time.Duration {
delta := httpDoneTime.Sub(p.stats.HttpResponseReadyTime)
if debug.IsDebug(p.debug) {
fmt.Printf("(Probe %v): Http Download Time: %v\n", p.probeid, delta)
}
return delta
}
func NewProbe(client *http.Client, debugLevel debug.DebugLevel) *Probe {
probe := &Probe{
client: client,
stats: &stats.TraceStats{},
trace: nil,
debug: debugLevel,
probeid: utilities.GenerateConnectionId(),
}
trace := traceable.GenerateHttpTimingTracer(probe, debugLevel)
probe.trace = trace
return probe
}
func (probe *Probe) SetDnsStartTimeInfo(
now time.Time,
dnsStartInfo httptrace.DNSStartInfo,
) {
probe.stats.DnsStartTime = now
probe.stats.DnsStart = dnsStartInfo
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) DNS Start for %v: %v\n",
probe.ProbeId(),
dnsStartInfo,
)
}
}
func (probe *Probe) SetDnsDoneTimeInfo(
now time.Time,
dnsDoneInfo httptrace.DNSDoneInfo,
) {
probe.stats.DnsDoneTime = now
probe.stats.DnsDone = dnsDoneInfo
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) DNS Done for %v: %v\n",
probe.ProbeId(),
probe.stats.DnsDone,
)
}
}
func (probe *Probe) SetConnectStartTime(
now time.Time,
) {
probe.stats.ConnectStartTime = now
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) TCP Start for %v at %v\n",
probe.ProbeId(),
probe.stats.ConnectStartTime,
)
}
}
func (probe *Probe) SetConnectDoneTimeError(
now time.Time,
err error,
) {
probe.stats.ConnectDoneTime = now
probe.stats.ConnectDoneError = err
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) TCP Done for %v (with error %v) @ %v\n",
probe.ProbeId(),
probe.stats.ConnectDoneError,
probe.stats.ConnectDoneTime,
)
}
}
func (probe *Probe) SetGetConnTime(now time.Time) {
probe.stats.GetConnectionStartTime = now
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) Started getting connection for %v @ %v\n",
probe.ProbeId(),
probe.stats.GetConnectionStartTime,
)
}
}
func (probe *Probe) SetGotConnTimeInfo(
now time.Time,
gotConnInfo httptrace.GotConnInfo,
) {
probe.stats.GetConnectionDoneTime = now
probe.stats.ConnInfo = gotConnInfo
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) Got connection for %v at %v with info %v\n",
probe.ProbeId(),
probe.stats.GetConnectionDoneTime,
probe.stats.ConnInfo,
)
}
}
func (probe *Probe) SetTLSHandshakeStartTime(
now time.Time,
) {
probe.stats.TLSStartTime = utilities.Some(now)
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) Started TLS Handshake for %v @ %v\n",
probe.ProbeId(),
probe.stats.TLSStartTime,
)
}
}
func (probe *Probe) SetTLSHandshakeDoneTimeState(
now time.Time,
connectionState tls.ConnectionState,
) {
probe.stats.TLSDoneTime = utilities.Some(now)
probe.stats.TLSConnInfo = connectionState
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) Completed TLS handshake for %v at %v with info %v\n",
probe.ProbeId(),
probe.stats.TLSDoneTime,
probe.stats.TLSConnInfo,
)
}
}
func (probe *Probe) SetHttpWroteRequestTimeInfo(
now time.Time,
info httptrace.WroteRequestInfo,
) {
probe.stats.HttpWroteRequestTime = now
probe.stats.HttpInfo = info
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) Http finished writing request for %v at %v with info %v\n",
probe.ProbeId(),
probe.stats.HttpWroteRequestTime,
probe.stats.HttpInfo,
)
}
}
func (probe *Probe) SetHttpResponseReadyTime(
now time.Time,
) {
probe.stats.HttpResponseReadyTime = now
if debug.IsDebug(probe.debug) {
fmt.Printf(
"(Probe) Http response is ready for %v at %v\n",
probe.ProbeId(),
probe.stats.HttpResponseReadyTime,
)
}
}
func CalculateSequentialRTTsTime(
ctx context.Context,
saturated_rtt_probe *Probe,
new_rtt_probe *Probe,
url string,
debugLevel debug.DebugLevel,
) chan utilities.GetLatency {
responseChannel := make(chan utilities.GetLatency)
go func() {
before := time.Now()
roundTripCount := uint16(0)
/*
TODO: We are not going to measure round-trip times on the load-generating connection
right now because we are dealing with a massive amount of buffer bloat on the
Apple CDN.
TODO: When this functionality is enabled, we may need to change the assertion in
the GotConn callback in the Traceable interface in traceable.go because a connection
will be reused in that case. If such a situation does come to pass, we will want to
move that assertion in to the various Traceable interface implementations that continue
to rely on this assertion.
c_a, err := saturated_client.Get(url)
if err != nil {
responseChannel <- GetLatency{Delay: 0, RTTs: 0, Err: err}
return
}
// TODO: Make this interruptable somehow
// by using _ctx_.
_, err = io.ReadAll(c_a.Body)
if err != nil {
responseChannel <- GetLatency{Delay: 0, RTTs: 0, Err: err}
return
}
roundTripCount += 5
c_a.Body.Close()
*/
c_b_req, err := http.NewRequestWithContext(
httptrace.WithClientTrace(ctx, new_rtt_probe.GetTrace()),
"GET",
url,
nil,
)
if err != nil {
responseChannel <- utilities.GetLatency{Delay: 0, RoundTripCount: 0, Err: err}
return
}
c_b, err := new_rtt_probe.client.Do(c_b_req)
if err != nil {
responseChannel <- utilities.GetLatency{Delay: 0, RoundTripCount: 0, Err: err}
return
}
// TODO: Make this interruptable somehow by using _ctx_.
_, err = io.ReadAll(c_b.Body)
if err != nil {
responseChannel <- utilities.GetLatency{Delay: 0, Err: err}
return
}
after := time.Now()
// Depending on whether we think that Close() requires another RTT (via TCP), we
// may need to move this before/after capturing the after time.
c_b.Body.Close()
sanity := after.Sub(before)
tlsAndHttpHeaderDelta := new_rtt_probe.GetTLSAndHttpHeaderDelta() // Constitutes 2 RTT, per the Spec.
httpDownloadDelta := new_rtt_probe.GetHttpDownloadDelta(after) // Constitutes 1 RTT, per the Spec.
dnsDelta := new_rtt_probe.GetDnsDelta() // Constitutes 1 RTT, per the Spec.
tcpDelta := new_rtt_probe.GetTCPDelta() // Constitutes 1 RTT, per the Spec.
totalDelay := tlsAndHttpHeaderDelta + httpDownloadDelta + dnsDelta + tcpDelta
if debug.IsDebug(debugLevel) {
fmt.Printf(
"(Probe %v) sanity vs total: %v vs %v\n",
new_rtt_probe.ProbeId(),
sanity,
totalDelay,
)
}
roundTripCount += 5 // According to addition, there are 5 RTTs that we measured.
responseChannel <- utilities.GetLatency{Delay: totalDelay, RoundTripCount: roundTripCount, Err: nil}
}()
return responseChannel
}
|