-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
485 lines (332 loc) · 15.6 KB
/
tests.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#coding=utf-8
"""
Presentation
============
This module contains unit and functional tests of the model module.
To run it:
~$ python -m unittest tests
Listing of the tests
==================
"""
from model import *
from metrics import *
import simu
import unittest
import time
@PacketTimer('request', 'received_callback')
class TimedPeer(Peer):
def __method(self):
print("method")
def method(self):
self.__method()
@TwoMethodsTimerAndCounter("request_media", "start_playback", "_video_stopped")
class StopClient(Client):
pass
@TwoMethodsTimer("request_media", "start_playback")
class LatenciesClient(Client):
pass
class TestSimuHelpers(unittest.TestCase):
def test_sleep(self):
start = time.time()
speed = config.speed
simu.sleep(5)
end = (time.time() - start) * speed
self.assertAlmostEqual(end, 5, 1)
def test_no_active_download(self):
self.s1 = VideoServer(1, "s1")
self.c1 = LatenciesClient(1001, "c1")
self.c1.set_func_new_dl(simu.inc_nb_dl)
self.c1.set_func_end_dl(simu.dec_nb_dl)
self.c2 = LatenciesClient(1002, "c2")
self.c2.set_func_new_dl(simu.inc_nb_dl)
self.c2.set_func_end_dl(simu.dec_nb_dl)
# we set the chunk to a big size so that we can determine accurately the download time
self.c1.connect_to(self.s1).set_lag(0.1).set_bandwidth(1024).set_max_chunk(32)
self.s1.connect_to(self.c1).set_lag(0.1).set_bandwidth(1024).set_max_chunk(32)
self.video = {'idVideo': 1337, 'duration': 60, 'size': 2048, 'bitrate': 2048/60, 'title': 'Video', 'description': 'A video'}
self.s1.add_video(video=self.video)
self.c1.request_media(1337, 2)
self.assertFalse(simu.no_active_download((self.c1, self.c2)))
simu.sleep(3)
self.assertTrue(simu.no_active_download((self.c1, self.c2)))
class TestRequestResponse(unittest.TestCase):
def setUp(self):
self.c1 = Peer(1001, "c1")
self.c2 = Peer(1002, "c2")
self.c3 = Peer(1003, "c3")
self.p = ForwardProxy(0, "Proxy")
self.c1.connect_to(self.p).set_lag(0.1)
self.p.connect_to(self.c1).set_lag(0.1)
self.c3.connect_to(self.p).set_lag(0.1)
self.p.connect_to(self.c3).set_lag(0.1)
def test_requests(self):
self.c1.request("lol")
self.c3.request("pouet")
simu.sleep(1)
self.assertEqual(self.c1.received_data['payload'], 'There you go: lol')
self.assertEqual(self.c3.received_data['payload'], 'There you go: pouet')
class TestClient(unittest.TestCase):
def setUp(self):
# testing direct access to a video server
self.s2 = VideoServer(2, "s2")
self.c4 = StopClient(1004, "c4")
# we set the chunk to a big size so that we can determine accurately the download time
self.c4.connect_to(self.s2).set_lag(0.1).set_bandwidth(2048)
self.s2.connect_to(self.c4).set_lag(0.1).set_bandwidth(2048)
self.c1 = StopClient(1001, "c1")
self.c2 = StopClient(1002, "c2")
self.p = ForwardProxy(0, "Proxy")
self.s1 = VideoServer(1, "s1")
self.c1.connect_to(self.p).set_lag(0.1).set_bandwidth(2048)
self.p.connect_to(self.c1).set_lag(0.1).set_bandwidth(2048)
self.c2.connect_to(self.p).set_lag(0.1).set_bandwidth(2048)
self.p.connect_to(self.c2).set_lag(0.1).set_bandwidth(2048)
self.s1.connect_to(self.p).set_lag(0.01).set_bandwidth(1024)
self.p.connect_to(self.s1).set_lag(0.01).set_bandwidth(1024)
def test_buffer_big_bitrate(self):
bigvideo = {'idVideo': 1, 'duration': 100, 'size': 409600, 'bitrate': 4096, 'title': 'Big Video', 'description': 'Big bitrate'}
self.s2.add_video(video=bigvideo)
self.c4.request_media(1, 2)
self.c4.start_video_consumer()
simu.sleep(2)
#self.assertEqual(self.c4.received_data['payload'], self.video)
simu.sleep(2)
simu.sleep(2)
simu.sleep(2)
#print(str(self.c4.counter))
self.assertGreater(self.c4.counter, 0)
def test_wait_vs_nowait(self):
bigvideo = {'idVideo': 1, 'duration': 10, 'size': 40960, 'bitrate': 4096, 'title': 'Big Video', 'description': 'Big bitrate'}
self.s2.add_video(video=bigvideo)
self.c4.set_buffer_size(6144)
self.c4.start_video_consumer()
self.c4.set_play_wait_buffer(False)
self.c4.request_media(1, 2)
simu.sleep(10)
print(str(self.c4.counter))
counter1 = self.c4.counter
self.c4.set_play_wait_buffer(True)
self.c4.request_media(1, 2)
simu.sleep(10)
print(str(self.c4.counter))
counter2 = self.c4.counter - counter1
self.assertGreater(counter1, counter2)
def test_two_videos_buffer_nowait(self):
video1 = {'idVideo': 1, 'duration': 10, 'size': 7680, 'bitrate': 768, 'title': 'Video', 'description': 'A video'}
video2 = {'idVideo': 2, 'duration': 5, 'size': 7500, 'bitrate': 1500, 'title': 'Video', 'description': 'A video'}
self.s1.add_video(video=video1)
self.s1.add_video(video=video2)
self.c2.set_play_wait_buffer(False)
self.c1.set_play_wait_buffer(False)
self.c2.start_video_consumer()
self.c1.start_video_consumer()
self.c1.request_media(1, 1)
self.assertEqual(self.c1.counter, 0)
simu.sleep(2)
self.c2.request_media(2, 1)
simu.sleep(10)
#print(str(self.c2.counter))
#print(str(self.c1.counter))
self.assertGreater(self.c2.counter, 0)
self.assertGreater(self.c1.counter, 0)
def test_two_videos_buffer(self):
video1 = {'idVideo': 1, 'duration': 10, 'size': 7680, 'bitrate': 768, 'title': 'Video', 'description': 'A video'}
video2 = {'idVideo': 2, 'duration': 5, 'size': 7500, 'bitrate': 1500, 'title': 'Video', 'description': 'A video'}
self.s1.add_video(video=video1)
self.s1.add_video(video=video2)
self.c2.set_play_wait_buffer(True)
self.c1.set_play_wait_buffer(True)
self.c2.start_video_consumer()
self.c1.start_video_consumer()
self.c1.request_media(1, 1)
self.assertEqual(self.c1.counter, 0)
simu.sleep(2)
self.c2.request_media(2, 1)
simu.sleep(10)
#print(str(self.c2.counter))
#print(str(self.c1.counter))
self.assertGreater(self.c2.counter, 0)
pass
class TestTiming(unittest.TestCase):
def setUp(self):
self.c1 = TimedPeer(1001, "c1")
self.c2 = TimedPeer(1002, "c2")
self.c3 = TimedPeer(1003, "c3")
self.p = ForwardProxy(0, "Proxy")
self.c1.connect_to(self.p).set_lag(1)
self.p.connect_to(self.c1).set_lag(1)
self.c3.connect_to(self.p).set_lag(0.5)
self.p.connect_to(self.c3).set_lag(0.5)
def test_basic_timing(self):
self.c1.request("lol")
self.c3.request("pouet")
simu.sleep(4)
self.assertTrue(self.c1.latencies[0] > 2 and self.c1.latencies[0] < 3)
def test_queue(self):
# by sending two requests one right after the other and having a latency of 1/1
# the first request should take 1+1 sec and the second 1+(1+1) as it has to wait for
# the first to be sent
self.c1.request("lol")
self.c1.request("pouet")
simu.sleep(5)
self.assertTrue(self.c1.latencies[0] > 2 and self.c1.latencies[0] < 3)
self.assertTrue(self.c1.latencies[1] > 3 and self.c1.latencies[0] < 4)
def test_wrapper(self):
self.c1.method()
class TestVideoServer(unittest.TestCase):
def setUp(self):
# testing direct access to a video server
self.s2 = VideoServer(2, "s2")
self.c4 = LatenciesClient(1004, "c4")
# we set the chunk to a big size so that we can determine accurately the download time
self.c4.connect_to(self.s2).set_lag(0.1).set_bandwidth(2048).set_max_chunk(32000)
self.s2.connect_to(self.c4).set_lag(0.1).set_bandwidth(2048).set_max_chunk(32000)
self.video = {'idVideo': 1337, 'duration': 60, 'size': 2048, 'bitrate': 2048/60, 'title': 'Video', 'description': 'A video'}
def test_video_access(self):
self.s2.add_video(video=self.video)
self.c4.request_media(1337, 2)
simu.sleep(2)
self.assertEqual(self.c4.received_data['payload'], self.video)
simu.sleep(10)
def test_add_video_access(self):
self.s2.add_video(60, 2048, 2048/60, 'Video', 'A video', 1337)
self.c4.request_media(1337, 2)
simu.sleep(2)
self.assertEqual(self.c4.received_data['payload'], self.video)
def test_transfer_speed(self):
bigvideo = {'idVideo': 1, 'duration': 60, 'size': 8192, 'bitrate': 8192/60, 'title': 'Big Video', 'description': 'Big bitrate'}
self.s2.add_video(video=bigvideo)
self.c4.set_buffer_size(8192)
self.c4.request_media(1, 2)
simu.sleep(5)
print("Latency "+str(self.c4.latencies[0]))
# should take 4.2 seconds, latency is 0.1, so 2*0.1, the size 8192 divided by the speed 2048: 2*0.1+8192/2048 = 4.2
self.assertTrue(self.c4.latencies[0] > 4 and self.c4.latencies[0] < 5)
bigvideo = {'idVideo': 2, 'duration': 60, 'size': 16384, 'bitrate': 8192/60, 'title': 'Big big Video', 'description': 'Biiiig bitrate'}
self.s2.add_video(video=bigvideo)
self.c4.request_media(2, 2)
simu.sleep(9)
# should take two times as much as it is two times as big
self.assertTrue(self.c4.latencies[1] > 8 and self.c4.latencies[1] < 9)
class TestForwardProxy(unittest.TestCase):
def setUp(self):
self.c1 = Client(1001, "c1")
self.c2 = Client(1002, "c2")
self.c3 = Client(1003, "c3")
self.p = ForwardProxy(0, "Proxy")
self.c1.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c1).set_lag(0.1).set_bandwidth(12000)
self.c2.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c2).set_lag(0.1).set_bandwidth(12000)
self.c3.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c3).set_lag(0.1).set_bandwidth(12000)
self.s1 = VideoServer(1, "s1")
self.s1.connect_to(self.p).set_lag(0.01).set_bandwidth(12000)
self.p.connect_to(self.s1).set_lag(0.01).set_bandwidth(12000)
self.video1 = {'idVideo': 1337, 'duration': 60, 'size': 2048, 'bitrate': 2048/60, 'title': 'Video', 'description': 'A video'}
self.video2 = {'idVideo': 9001, 'duration': 55, 'size': 2000, 'bitrate': 2000/55, 'title': 'Video 2', 'description': 'Another video'}
def test_one_video_access(self):
self.s1.add_video(video=self.video2)
self.c1.request_media(9001, 1)
simu.sleep(2)
self.assertEqual(self.c1.received_data['payload'], self.video2)
def test_multiple_video_multiple_access(self):
self.s1.add_video(video=self.video1)
self.s1.add_video(video=self.video2)
self.c1.request_media(9001, 1)
simu.sleep(2)
self.assertEqual(self.c1.received_data['payload'], self.video2)
self.c2.request_media(9001, 1)
self.c3.request_media(1337, 1)
simu.sleep(2)
self.assertEqual(self.c2.received_data['payload'], self.video2)
self.assertEqual(self.c3.received_data['payload'], self.video1)
class TestUnlimitedProxy(unittest.TestCase):
def setUp(self):
self.c1 = LatenciesClient(1001, "c1")
self.c2 = LatenciesClient(1002, "c2")
self.p = UnlimitedProxy(0, "Proxy")
#good bandwidth between the clients and proxy
self.c1.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c1).set_lag(0.1).set_bandwidth(12000)
self.c2.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c2).set_lag(0.1).set_bandwidth(12000)
self.s1 = VideoServer(1, "s1")
#but congested link between proxy and video server
self.s1.connect_to(self.p).set_lag(0.01).set_bandwidth(1024)
self.p.connect_to(self.s1).set_lag(0.01).set_bandwidth(1024)
self.video1 = {'idVideo': 1337, 'duration': 60, 'size': 2048, 'bitrate': 2048/60, 'title': 'Video', 'description': 'A video'}
self.bigvideo = {'idVideo': 1, 'duration': 60, 'size': 8192, 'bitrate': 8192/60, 'title': 'Big Video', 'description': 'Big bitrate'}
def test_caching_benefits(self):
self.s1.add_video(video=self.video1)
self.c1.set_two_in_a_row_protection(False)
self.c1.request_media(1337, 1)
simu.sleep(3)
self.c1.request_media(1337, 1)
simu.sleep(2)
self.c1.request_media(1337, 1)
simu.sleep(2)
print(self.c1.latencies)
self.assertTrue(self.c1.latencies[0] > self.c1.latencies[1])
class TestFIFOProxy(unittest.TestCase):
def setUp(self):
self.c1 = LatenciesClient(1001, "c1")
self.c2 = LatenciesClient(1002, "c2")
self.p = FIFOProxy(0, "Proxy")
#good bandwidth between the clients and proxy
self.c1.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c1).set_lag(0.1).set_bandwidth(12000)
self.c2.connect_to(self.p).set_lag(0.1).set_bandwidth(12000)
self.p.connect_to(self.c2).set_lag(0.1).set_bandwidth(12000)
self.s1 = VideoServer(1, "s1")
#but congested link between proxy and video server
self.s1.connect_to(self.p).set_lag(0.01).set_bandwidth(1024)
self.p.connect_to(self.s1).set_lag(0.01).set_bandwidth(1024)
self.video1 = {'idVideo': 1337, 'duration': 60, 'size': 2048, 'bitrate': 2048/60, 'title': 'Video', 'description': 'A video'}
self.bigvideo = {'idVideo': 1, 'duration': 60, 'size': 8192, 'bitrate': 8192/60, 'title': 'Big Video', 'description': 'Big bitrate'}
def test_caching_benefits(self):
self.s1.add_video(video=self.video1)
self.c1.set_buffer_size(512)
self.c2.set_buffer_size(512)
self.c2.request_media(1337, 1)
simu.sleep(3)
self.c1.request_media(1337, 1)
simu.sleep(2)
self.assertTrue(self.c2.latencies[0] > self.c1.latencies[0])
# def test_plot(self):
# video2 = {'idVideo': 9001, 'duration': 55, 'size': 2000, 'bitrate': 2000/55, 'title': 'Video 2', 'description': 'Another video'}
# self.s1.add_video(video=self.video1)
# self.s1.add_video(video=video2)
# self.s1.add_video(video=self.bigvideo)
# self.c1.request_media(1337, 1)
# simu.sleep(3)
# self.c1.request_media(1337, 1)
# self.c2.request_media(9001, 1)
# simu.sleep(3)
# self.c1.request_media(9001, 1)
# self.c2.request_media(1337, 1)
# simu.sleep(3)
# self.c1.request_media(1337, 1)
# simu.sleep(3)
# self.c1.request_media(1337, 1)
# simu.sleep(3)
# self.c1.request_media(1, 1)
# simu.sleep(5)
# self.c2.request_media(1, 1)
# simu.sleep(5)
# print(self.c1.latencies)
# ps = PlotStats()
# ps.plot_latencies(self.c1.latencies)
# ps.hist_latencies(self.c1.latencies+self.c2.latencies)
def test_cache_stats(self):
self.s1.add_video(video=self.video1)
self.c2.request_media(1337, 1)
# don't know why but when requesting with c1, it doesn't work.
simu.sleep(3)
self.c1.request_media(1337, 1)
simu.sleep(1)
stats = self.p.get_hit_stats()
self.assertEqual(stats['byte_hit_ratio'], 0.5)
self.assertEqual(stats['byte_cache'], self.video1['size']/8)
if __name__ == '__main__':
unittest.main()