Example #1
0
    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)
Example #2
0
    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)
Example #3
0
 def _play_videos(self):
     """ Automatically consumes videos when the buffer has been filled initially
         Supposed to be run only in an independant thread.
         Runs once every simulated second.
     """
     while True:
         # Infinite loop, not really efficient
         for id_media in self.media_asked_for:
             # if the state of the video is "buffering"
             if self.play_wait_buffer and self.media_asked_for[id_media]['state'] is 'buffer':
                 # if the buffer is filled enough, we update the state to "playing"
                 if self.media_asked_for[id_media]['buffer'] > self.buffer_size:
                     self.media_asked_for[id_media]['state'] = 'play'
             # if the state of the video is "playing"
             if self.media_asked_for[id_media]['state'] is 'play':
                 if self.media_asked_for[id_media]['buffer'] >= self.media_asked_for[id_media]['bitrate']:
                     self.media_asked_for[id_media]['buffer'] -= self.media_asked_for[id_media]['bitrate']
                 else:
                     self.media_asked_for[id_media]['buffer'] = 0
                 if self.media_asked_for[id_media]['buffer'] is 0:
                     # change the state if we want to wait for the buffer to be filled
                     if self.play_wait_buffer:
                         # only when we want to wait for a buffer refill each time it stops
                         self.media_asked_for[id_media]['state'] = 'buffer'
                     #print("Buffer empty for video "+str(id_media))
                     self._video_stopped(id_media)
                 percentage = self.media_asked_for[id_media]['buffer']/self.buffer_size*100
                 #print("Buffer for media "+str(id_media)+" filled at "+str(percentage)+"%")
         simu.sleep(1)
Example #4
0
    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)
Example #5
0
    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')
Example #6
0
    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 run_simulation(self):
        """ Runs the simulation, either with a scheduler or by waiting to trigger
            each event. 

            With the event_lock method, we wait of an event to happend with a 
            timeout set to the delay until the next event. The event should be 
            triggered when all current downloads are over, which means that nothing
            is happening any more in the simulation. This happens only when the 
            skip_inactivity is set to True.

            With the scheduler method, when not skiping inactivity, we just run 
            the already filled and configured scheduler object. The option to skip
            inactivity should not be used as it is using a lot of CPU for nothing. 
        """
        try:
            if self.method == 'event_lock':

                if self.skip_inactivity:
                    simu.action_when_zero = self.signal_sys_inact

                while not self._events_queue.empty():
                    event = self._events_queue.get()
                    print("New event: "+str(event))
                    self._req_event.clear()

                    if not self.skip_inactivity or not simu.no_active_download():
                        self._req_event.wait(event['delay'])

                    self._clients[event['id_client']].request_media(event['id_video'], event['id_server'])

            elif self.method == 'scheduler':
                if self.skip_inactivity:
                    while True:
                        """ Inefficient way to skip the inactivity """
                        next = self._scheduler.run(False)
                        # have a threshold to avoid testing the inavtivity all the time
                        if next != None:
                            if next/config.speed >= 1:
                                # if it takes more than 1 second in real time
                                if simu.no_active_download(self._clients.values()):
                                    simu.add_time(next-1)
                                    print("Skiping inactivity!")
                        elif self._scheduler.empty():
                            return
                        else:
                            simu.sleep(next/2)
                else:
                    self._scheduler.run()
            else:
                print("run_simulation error: no method specified!")
        except (KeyboardInterrupt, SystemExit):
            print(' ')
            print("Simulation interupted. To exit, press ctrl+c again.")
            print(' ')
Example #8
0
    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)
Example #9
0
    def _worker(self):
        """ infinite loop running in a thread to simulate the time needed to send the data.
            the thread gets the data to send from the Queue q, where the items have two fields:

            - delay, how long the task is supposed to take
            - data, the data to send, after the delay

            private
        """
        while True:
            item = self.q.get()
            data = item['data']
            mode = item['mode']
            if mode is 'normal':
                # we set the chunkId before it is updated in the item (in the if)
                data['chunkId'] = item['chunkId']

                # if the packet is too big, we split it
                if item['size'] > self.max_chunk:
                    data['chunkSize'] = self.max_chunk
                    item['chunkId'] += 1
                    item['size'] -= self.max_chunk
                    # and put the rest on the top of the queue, to have a round robin
                    self.q.put(item)
                # if not, we set the chunkSize to remaining size and don't split it
                else:
                    data['chunkSize'] = item['size']
                    data['lastChunk'] = True

            elif mode is 'forwardchunk':
                if 'chunkSize' not in data:
                    print("We got a problem with this chunk forwarding!")
                    data['chunkSize'] = item['size']

            elif mode is 'donotchunk':
                data['chunkId'] = 0
                data['chunkSize'] = item['size']
                data['lastChunk'] = True

            delay = data['chunkSize']/self.bandwidth

            if data['chunkId'] is 0:
                """ only add the latency on the first chunk as the latency
                    is only noticable one time, then all chunks are sent
                    consecutively  """
                delay += self.latency

            #print("Delay: "+str(delay)+", ChunkSize: "+str(data['chunkSize']))

            simu.sleep(delay)
            self.peer.received_callback(data)
            self.q.task_done()
Example #10
0
    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)
Example #11
0
    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])
Example #12
0
    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)
Example #13
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)
Example #14
0
    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)
Example #15
0
    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)
Example #16
0
    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)))
Example #17
0
    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])
Example #18
0
 def test_sleep(self):
     start = time.time()
     speed = config.speed
     simu.sleep(5)
     end = (time.time() - start) * speed
     self.assertAlmostEqual(end, 5, 1)