forked from cesar-ilharco/rmcat_evaluation
/
bwe_utils_unittest.py
158 lines (134 loc) · 6.63 KB
/
bwe_utils_unittest.py
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
import unittest
import random
from packet import Packet
from packet_source import PacketSource
from bwe_utils import loss_ratio, global_loss_ratio, receiving_rate_kbps, average_bitrate_kbps, average_delay_ms
"""
Unittests for bwe_utils.
"""
class TestBweUtils(unittest.TestCase):
def assertNear(self, x, y, precision):
self.assertTrue(abs(x-y) < precision)
def test_loss_ratio_no_packets(self):
self.assertEqual(loss_ratio(None, 0.0), 0.0)
self.assertEqual(loss_ratio(None, 50.0), 0.0)
self.assertEqual(loss_ratio([], 0.0), 0.0)
self.assertEqual(loss_ratio([], 50.0), 0.0)
def test_loss_ratio_single_packet(self):
# Only id and arrival time matter here
packet = Packet(1, None, None)
packet.arrival_time_ms = 10.0
self.assertEqual(loss_ratio([packet], 0.0), 0.0)
self.assertEqual(loss_ratio([packet], 50.0), 0.0)
def test_loss_ratio_gap_packets(self):
packets = []
for i in range(10):
# Only id and arrival time matter here
packet = Packet(i, None, None)
packet.arrival_time_ms = 10.0 * i
packets.append(packet)
for i in range(20, 30):
# Only id and arrival time matter here
packet = Packet(i, None, None)
packet.arrival_time_ms = 10.0 * i
packets.append(packet)
# Short time windows won't trigger loss computation.
for t in range(0, 200, 10):
self.assertEqual(loss_ratio(packets, t), 0.0)
for i in range(10):
time_window_ms = 200 + 10*i
loss = 1.0 - float(11 + i)/(21 + i)
self.assertEqual(loss_ratio(packets, time_window_ms), loss)
def test_global_loss_ratio_no_packets(self):
self.assertEqual(global_loss_ratio(None), 0.0)
def test_global_loss_ratio_contiguous_packets(self):
for i in range(10):
first_id = random.randint(0,100)
number_packets = random.randint(0,1000)
packets = [Packet(first_id+i, None, None) for i in range(number_packets)]
self.assertEqual(global_loss_ratio(packets), 0.0)
def test_global_loss_ratio_shuffled_packets(self):
for i in range(10):
first_id = random.randint(0,100)
number_packets = random.randint(0,1000)
packets = [Packet(first_id+i, None, None) for i in range(number_packets)]
random.shuffle(packets)
self.assertEqual(global_loss_ratio(packets), 0.0)
def test_global_loss_ratio_sparsed_packets(self):
p1 = Packet(1, None, None)
p2 = Packet(1000, None, None)
self.assertEqual(global_loss_ratio([p1, p2]), 1.0 - float(2)/1000)
def test_receiving_rate_kbps_no_packets(self):
self.assertEqual(receiving_rate_kbps(None, 0.0), 0.0)
self.assertEqual(receiving_rate_kbps(None, 100.0), 0.0)
self.assertEqual(receiving_rate_kbps([], 0.0), 0.0)
self.assertEqual(receiving_rate_kbps([], 100.0), 0.0)
def test_receiving_rate_kbps_single_packet(self):
payload_sizes_bytes = [0.0, 500.0, 1200.0] # Empty or non-empty packet.
for payload_size_bytes in payload_sizes_bytes:
# id and send_time don't matter here.
packet = Packet(None, None, payload_size_bytes)
for i in range(10):
packet.arrival_time_ms = i
time_window_ms = random.uniform(100.0, 1000.0)
self.assertEqual(receiving_rate_kbps([packet], time_window_ms),
8.0 * packet.payload_size_bytes / time_window_ms)
def test_receiving_rate_regular_packets(self):
for i in range(10):
bitrate_kbps = random.uniform(150.0, 2500.0)
packet_size_bytes = random.uniform(1.0, 8000.0)
delay_ms = random.uniform(10.0, 300.0)
packet_source = PacketSource(packet_size_bytes)
packets = []
for j in range(1000):
packet = packet_source.create_packet(bitrate_kbps)
packet.arrival_time_ms = packet.send_time_ms + delay_ms
packets.append(packet)
time_window_ms = random.uniform(0.0, 1.0) * packets[-1].arrival_time_ms
self.assertNear(receiving_rate_kbps(packets, time_window_ms), bitrate_kbps, 0.001)
def test_average_bitrate_no_packets(self):
self.assertEqual(average_bitrate_kbps(None), 0.0)
self.assertEqual(average_bitrate_kbps([]), 0.0)
def test_average_bitrate_single_packet(self):
for i in range(10):
packet_size_bytes = random.uniform(1.0, 8000.0)
delay_ms = random.uniform(10.0, 300.0)
packet = Packet(1, 0.0, packet_size_bytes)
packet.arrival_time_ms = delay_ms
self.assertEqual(average_bitrate_kbps([packet]), 8.0 * packet_size_bytes / delay_ms)
def test_average_bitrate_two_packets(self):
for i in range(10):
packet_size_bytes = random.uniform(1.0, 8000.0)
delay_ms = random.uniform(10.0, 300.0)
gap_ms = random.uniform(10.0, 50.0)
packet1 = Packet(1, 0.0, packet_size_bytes)
packet2 = Packet(2, gap_ms, packet_size_bytes)
packet1.arrival_time_ms = delay_ms
packet2.arrival_time_ms = gap_ms + delay_ms
self.assertNear(average_bitrate_kbps([packet1, packet2]), 8.0 * packet_size_bytes / gap_ms, 0.001)
def test_average_bitrate_multiple_packets(self):
for i in range(10):
packet_size_bytes = random.uniform(1.0, 8000.0)
delay_ms = random.uniform(10.0, 300.0)
gap_ms = random.uniform(10.0, 50.0)
packets = []
for j in range(1000):
packets.append(Packet(j+1, j*gap_ms, packet_size_bytes))
packets[j].arrival_time_ms = packets[j].send_time_ms + delay_ms
self.assertNear(average_bitrate_kbps(packets), 8.0 * packet_size_bytes / gap_ms, 0.001)
def test_average_delay_no_packets(self):
self.assertEqual(average_delay_ms(None), 0.0)
self.assertEqual(average_delay_ms([]), 0.0)
def test_average_delay_multiple_packets(self):
self.assertEqual(average_delay_ms(None), 0.0)
self.assertEqual(average_delay_ms([]), 0.0)
for i in range(10):
delay_ms = random.uniform(10.0, 300.0)
gap_ms = random.uniform(10.0, 50.0)
packets = []
for j in range(1000):
packets.append(Packet(j+1, j*gap_ms, 1200.0))
packets[j].arrival_time_ms = packets[j].send_time_ms + delay_ms
self.assertNear(average_delay_ms(packets), delay_ms, 0.001)
if __name__ == '__main__':
unittest.main()