-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathresult.go
273 lines (244 loc) · 8.08 KB
/
result.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
package irtt
import (
"encoding/json"
"math"
"sort"
"time"
)
// Result is returned from Run.
type Result struct {
VersionInfo *VersionInfo `json:"version"`
SystemInfo *SystemInfo `json:"system_info"`
Config *ClientConfig `json:"config"`
SendErr error `json:"send_err,omitempty"`
ReceiveErr error `json:"receive_err,omitempty"`
*Stats `json:"stats"`
RoundTrips []RoundTrip `json:"round_trips"`
}
func newResult(rec *Recorder, cfg *ClientConfig, serr error, rerr error) *Result {
stats := &Stats{Recorder: rec}
r := &Result{
VersionInfo: NewVersionInfo(),
SystemInfo: NewSystemInfo(),
Config: cfg,
SendErr: serr,
ReceiveErr: rerr,
Stats: stats,
}
// calculate total duration (monotonic time since start)
r.Duration = cfg.TimeSource.Now(Monotonic).Sub(r.Start)
// create RoundTrips array
r.RoundTrips = make([]RoundTrip, len(rec.RoundTripData))
for i := 0; i < len(r.RoundTrips); i++ {
rt := &r.RoundTrips[i]
rt.Seqno = Seqno(i)
rt.RoundTripData = &r.RoundTripData[i]
// use received window to update lost status of previous round trips
if rt.ReplyReceived() {
rt.Lost = LostFalse
rwin := rt.RoundTripData.receivedWindow
if cfg.Params.ReceivedStats&ReceivedStatsWindow != 0 && (rwin&0x1 != 0) {
rwin >>= 1
wend := i - 63
if wend < 0 {
wend = 0
}
for j := i - 1; j >= wend; j-- {
rcvd := (rwin&0x1 != 0)
prt := &r.RoundTrips[j]
if rcvd {
if prt.Lost != LostFalse {
prt.Lost = LostDown
}
} else if prt.Lost == LostTrue || prt.Lost == LostUp {
prt.Lost = LostUp
} // else don't allow a transition from not lost to lost
rwin >>= 1
}
}
}
// calculate IPDV
rt.IPDV = InvalidDuration
rt.SendIPDV = InvalidDuration
rt.ReceiveIPDV = InvalidDuration
if i > 0 {
rtp := &r.RoundTrips[i-1]
if rt.ReplyReceived() && rtp.ReplyReceived() {
rt.IPDV = rt.IPDVSince(rtp.RoundTripData)
rt.SendIPDV = rt.SendIPDVSince(rtp.RoundTripData)
rt.ReceiveIPDV = rt.ReceiveIPDVSince(rtp.RoundTripData)
}
}
}
// do median calculations (could figure out a rolling median one day)
r.visitStats(&r.RTTStats, false, func(rt *RoundTrip) time.Duration {
return rt.RTT()
})
r.visitStats(&r.SendDelayStats, false, func(rt *RoundTrip) time.Duration {
return rt.SendDelay()
})
r.visitStats(&r.ReceiveDelayStats, false, func(rt *RoundTrip) time.Duration {
return rt.ReceiveDelay()
})
// IPDV
r.visitStats(&r.RoundTripIPDVStats, true, func(rt *RoundTrip) time.Duration {
return AbsDuration(rt.IPDV)
})
// send IPDV
r.visitStats(&r.SendIPDVStats, true, func(rt *RoundTrip) time.Duration {
return AbsDuration(rt.SendIPDV)
})
// receive IPDV
r.visitStats(&r.ReceiveIPDVStats, true, func(rt *RoundTrip) time.Duration {
return AbsDuration(rt.ReceiveIPDV)
})
// calculate server processing time, if available
for _, rt := range rec.RoundTripData {
spt := rt.ServerProcessingTime()
if spt != InvalidDuration {
r.ServerProcessingTimeStats.push(spt)
}
}
// set packets sent and received
r.PacketsSent = r.SendCallStats.N
r.PacketsReceived = r.RTTStats.N + r.Duplicates
// calculate expected packets sent based on the time between the first and
// last send
r.ExpectedPacketsSent = pcount(r.LastSent.Sub(r.FirstSend), r.Config.Interval)
// calculate timer stats
r.TimerErrPercent = 100 * float64(r.TimerErrorStats.Mean()) / float64(r.Config.Interval)
// for some reason, occasionally one more packet is sent than expected, which
// wraps around the uint, so just punt and hard prevent this for now
if r.ExpectedPacketsSent < r.PacketsSent {
r.TimerMisses = 0
r.ExpectedPacketsSent = r.PacketsSent
} else {
r.TimerMisses = r.ExpectedPacketsSent - r.PacketsSent
}
r.TimerMissPercent = 100 * float64(r.TimerMisses) / float64(r.ExpectedPacketsSent)
// calculate send rate
r.SendRate = calculateBitrate(r.BytesSent, r.LastSent.Sub(r.FirstSend))
// calculate receive rate (start from time of first receipt)
r.ReceiveRate = calculateBitrate(r.BytesReceived, r.LastReceived.Sub(r.FirstReceived))
// calculate packet loss percent
if r.RTTStats.N > 0 {
r.PacketLossPercent = 100 * float64(r.SendCallStats.N-r.RTTStats.N) /
float64(r.SendCallStats.N)
} else {
r.PacketLossPercent = float64(100)
}
// calculate upstream and downstream loss percent
if r.ServerPacketsReceived > 0 {
r.UpstreamLossPercent = 100 *
float64(r.SendCallStats.N-uint(r.ServerPacketsReceived)) /
float64(r.SendCallStats.N)
r.DownstreamLossPercent = 100.0 *
float64(uint(r.ServerPacketsReceived)-r.PacketsReceived) /
float64(r.ServerPacketsReceived)
}
// calculate duplicate percent
if r.PacketsReceived > 0 {
r.DuplicatePercent = 100 * float64(r.Duplicates) / float64(r.PacketsReceived)
}
// calculate late packets percent
if r.PacketsReceived > 0 {
r.LatePacketsPercent = 100 * float64(r.LatePackets) / float64(r.PacketsReceived)
}
return r
}
// visitStats visits each RoundTrip, optionally pushes to a DurationStats, and
// at the end, sets the median value on the DurationStats.
func (r *Result) visitStats(ds *DurationStats, push bool,
fn func(*RoundTrip) time.Duration) {
fs := make([]float64, 0, len(r.RoundTrips))
for i := 0; i < len(r.RoundTrips); i++ {
d := fn(&r.RoundTrips[i])
if d != InvalidDuration {
if push {
ds.push(d)
}
fs = append(fs, float64(d))
}
}
if len(fs) > 0 {
ds.setMedian(median(fs))
}
}
// RoundTrip stores the Timestamps and statistics for a single round trip.
type RoundTrip struct {
Seqno Seqno `json:"seqno"`
Lost Lost `json:"lost"`
*RoundTripData `json:"timestamps"`
IPDV time.Duration `json:"-"`
SendIPDV time.Duration `json:"-"`
ReceiveIPDV time.Duration `json:"-"`
}
// MarshalJSON implements the json.Marshaler interface.
func (rt *RoundTrip) MarshalJSON() ([]byte, error) {
type Alias RoundTrip
delay := make(map[string]interface{})
if rt.RTT() != InvalidDuration {
delay["rtt"] = rt.RTT()
}
if rt.SendDelay() != InvalidDuration {
delay["send"] = rt.SendDelay()
}
if rt.ReceiveDelay() != InvalidDuration {
delay["receive"] = rt.ReceiveDelay()
}
ipdv := make(map[string]interface{})
if rt.IPDV != InvalidDuration {
ipdv["rtt"] = rt.IPDV
}
if rt.SendIPDV != InvalidDuration {
ipdv["send"] = rt.SendIPDV
}
if rt.ReceiveIPDV != InvalidDuration {
ipdv["receive"] = rt.ReceiveIPDV
}
j := &struct {
*Alias
Delay map[string]interface{} `json:"delay"`
IPDV map[string]interface{} `json:"ipdv"`
}{
Alias: (*Alias)(rt),
Delay: delay,
IPDV: ipdv,
}
return json.Marshal(j)
}
// Stats are the statistics in the Result.
type Stats struct {
*Recorder
Duration time.Duration `json:"duration"`
ExpectedPacketsSent uint `json:"-"`
PacketsSent uint `json:"packets_sent"`
PacketsReceived uint `json:"packets_received"`
PacketLossPercent float64 `json:"packet_loss_percent"`
UpstreamLossPercent float64 `json:"upstream_loss_percent"`
DownstreamLossPercent float64 `json:"downstream_loss_percent"`
DuplicatePercent float64 `json:"duplicate_percent"`
LatePacketsPercent float64 `json:"late_packets_percent"`
SendIPDVStats DurationStats `json:"ipdv_send"`
ReceiveIPDVStats DurationStats `json:"ipdv_receive"`
RoundTripIPDVStats DurationStats `json:"ipdv_round_trip"`
ServerProcessingTimeStats DurationStats `json:"server_processing_time"`
TimerErrPercent float64 `json:"timer_err_percent"`
TimerMisses uint `json:"timer_misses"`
TimerMissPercent float64 `json:"timer_miss_percent"`
SendRate Bitrate `json:"send_rate"`
ReceiveRate Bitrate `json:"receive_rate"`
}
// median calculates the median value of the supplied float64 slice. The array
// is sorted in place, so its original order is modified.
func median(f []float64) float64 {
sort.Float64s(f)
l := len(f)
if l == 0 {
return math.NaN()
}
if l%2 == 0 {
return (float64(f[l/2-1]) + float64(f[l/2])) / 2.0
}
return float64(f[l/2])
}