Skip to content

Commit ffd5481

Browse files
committed
Avoid a zerodiv case in UDP loss formatting related to the stream-failure scenario
1 parent 8663b66 commit ffd5481

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/protocol/results.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl IntervalResult for TcpReceiveResult {
213213

214214
fn to_string(&self, bit:bool) -> String {
215215
let duration_divisor;
216-
if self.duration == 0.0 { //avoid zerodiv, which should be impossible, but safety
216+
if self.duration == 0.0 { //avoid zerodiv, which can happen if the stream fails
217217
duration_divisor = 1.0;
218218
} else {
219219
duration_divisor = self.duration;
@@ -275,7 +275,7 @@ impl IntervalResult for TcpSendResult {
275275

276276
fn to_string(&self, bit:bool) -> String {
277277
let duration_divisor;
278-
if self.duration == 0.0 { //avoid zerodiv, which should be impossible, but safety
278+
if self.duration == 0.0 { //avoid zerodiv, which can happen if the stream fails
279279
duration_divisor = 1.0;
280280
} else {
281281
duration_divisor = self.duration;
@@ -345,7 +345,7 @@ impl IntervalResult for UdpReceiveResult {
345345

346346
fn to_string(&self, bit:bool) -> String {
347347
let duration_divisor;
348-
if self.duration == 0.0 { //avoid zerodiv, which should be impossible, but safety
348+
if self.duration == 0.0 { //avoid zerodiv, which can happen if the stream fails
349349
duration_divisor = 1.0;
350350
} else {
351351
duration_divisor = self.duration;
@@ -414,7 +414,7 @@ impl IntervalResult for UdpSendResult {
414414

415415
fn to_string(&self, bit:bool) -> String {
416416
let duration_divisor;
417-
if self.duration == 0.0 { //avoid zerodiv, which should be impossible, but safety
417+
if self.duration == 0.0 { //avoid zerodiv, which can happen if the stream fails
418418
duration_divisor = 1.0;
419419
} else {
420420
duration_divisor = self.duration;
@@ -822,7 +822,7 @@ impl TestResults for TcpTestResults {
822822
}
823823

824824
let send_duration_divisor;
825-
if duration_send == 0.0 { //avoid zerodiv, which should be impossible, but safety
825+
if duration_send == 0.0 { //avoid zerodiv, which can happen if all streams fail
826826
send_duration_divisor = 1.0;
827827
} else {
828828
send_duration_divisor = duration_send;
@@ -834,7 +834,7 @@ impl TestResults for TcpTestResults {
834834
};
835835

836836
let receive_duration_divisor;
837-
if duration_receive == 0.0 { //avoid zerodiv, which should be impossible, but safety
837+
if duration_receive == 0.0 { //avoid zerodiv, which can happen if all streams fail
838838
receive_duration_divisor = 1.0;
839839
} else {
840840
receive_duration_divisor = duration_receive;
@@ -1086,7 +1086,7 @@ impl TestResults for UdpTestResults {
10861086
}
10871087

10881088
let send_duration_divisor;
1089-
if duration_send == 0.0 { //avoid zerodiv, which should be impossible, but safety
1089+
if duration_send == 0.0 { //avoid zerodiv, which can happen if all streams fail
10901090
send_duration_divisor = 1.0;
10911091
} else {
10921092
send_duration_divisor = duration_send;
@@ -1098,7 +1098,7 @@ impl TestResults for UdpTestResults {
10981098
};
10991099

11001100
let receive_duration_divisor;
1101-
if duration_receive == 0.0 { //avoid zerodiv, which should be impossible, but safety
1101+
if duration_receive == 0.0 { //avoid zerodiv, which can happen if all streams fail
11021102
receive_duration_divisor = 1.0;
11031103
} else {
11041104
receive_duration_divisor = duration_receive;
@@ -1110,6 +1110,12 @@ impl TestResults for UdpTestResults {
11101110
};
11111111

11121112
let packets_lost = packets_sent - packets_received;
1113+
let packets_sent_divisor;
1114+
if packets_sent == 0 { //avoid zerodiv, which can happen if all streams fail
1115+
packets_sent_divisor = 1.0
1116+
} else {
1117+
packets_sent_divisor = packets_sent as f64;
1118+
}
11131119
let mut output = format!("==========\n\
11141120
UDP send result over {:.2}s | streams: {}\n\
11151121
bytes: {} | per second: {:.3} | {}\n\
@@ -1124,7 +1130,7 @@ impl TestResults for UdpTestResults {
11241130

11251131
duration_receive, self.stream_results.len(),
11261132
bytes_received, receive_bytes_per_second, receive_throughput,
1127-
packets_received, packets_lost, (packets_lost as f64 / packets_sent as f64) * 100.0, packets_out_of_order, packets_duplicated, packets_received as f64 / receive_duration_divisor,
1133+
packets_received, packets_lost, (packets_lost as f64 / packets_sent_divisor) * 100.0, packets_out_of_order, packets_duplicated, packets_received as f64 / receive_duration_divisor,
11281134
);
11291135
if jitter_calculated {
11301136
output.push_str(&format!("\njitter: {:.6}s over {} consecutive packets", jitter_weight / (unbroken_sequence_count as f64), unbroken_sequence_count));

0 commit comments

Comments
 (0)