def run(self): self._logger.debug("VoIPClient.run()") self._running = True prev_call_logs = set( [d for d in os.listdir(".") if d.startswith("call_")]) self._itgrecv = ITG("ITGRecv") self._itgrecv.start() while self._running: time.sleep(1) call_logs = set([ d for d in os.listdir(".") if d.startswith("call_") and d.endswith(".bin") ]) new_logs = [l for l in call_logs if l not in prev_call_logs] prev_call_logs = call_logs for logf in new_logs: while not self._is_finished_writing(logf): time.sleep(1) self._logger.debug("CALL: %s" % logf) metric = self._parse_log(logf) self._create_metric(metric=metric) self._report_metric()
def run(self): self._logger.debug("VoIPServer.run()") self._running = True if not self._config: self._logger.info("No config applied yet - using default config") self.set_config() self._random_wait_start() args = {'duration': self._config['call_duration'] * 1000, 'client_ip': self._config['client_ip'], 'codec': self._config['codec']} call_cnt = 0 while self._running: if not self._do_request(): time.sleep(0.1) else: self._logger.debug("VoIPServer running") call_cnt += 1 args['rcv_file'] = "call_%d.bin" % int((time.time() - 1523017559) * 10) cmd = "ITGSend -a {client_ip} -t {duration} -m rttm -x {rcv_file} VoIP -x {codec}".format(**args) self._itgsend = ITG(cmd) self._itgsend.start() self._itgsend.wait() self._request_finished()
class VoIPServer(PrototypeServer.PrototypeServer): def __init__(self, **kwargs): super(VoIPServer, self).__init__(**kwargs) self._logger.debug("VoIPServer.__init__()") self._default_config.update({ "client_ip": "127.0.0.1", "call_duration": 30, "codec": "G.729.2" }) self._itgsend = None def prepare(self): self._logger.debug("VoIPServer.prepare()") def run(self): self._logger.debug("VoIPServer.run()") self._running = True if not self._config: self._logger.info("No config applied yet - using default config") self.set_config() self._random_wait_start() args = {'duration': self._config['call_duration'] * 1000, 'client_ip': self._config['client_ip'], 'codec': self._config['codec']} call_cnt = 0 while self._running: if not self._do_request(): time.sleep(0.1) else: self._logger.debug("VoIPServer running") call_cnt += 1 args['rcv_file'] = "call_%d.bin" % int((time.time() - 1523017559) * 10) cmd = "ITGSend -a {client_ip} -t {duration} -m rttm -x {rcv_file} VoIP -x {codec}".format(**args) self._itgsend = ITG(cmd) self._itgsend.start() self._itgsend.wait() self._request_finished() def clean_up(self): self._logger.debug("VoIPServer.clean_up()") self._itgsend.stop() def _apply_config(self): self._logger.debug("VoIPServer._apply_config()")
class VoIPClient(PrototypeClient.PrototypeClient): def __init__(self, **kwargs): super(VoIPClient, self).__init__(**kwargs) self._logger.debug("VoIPClient.__init__()") self._default_config.update({}) self._itgrecv = None def prepare(self): self._logger.debug("VoIPClient.prepare()") self.register_application() def run(self): self._logger.debug("VoIPClient.run()") self._running = True prev_call_logs = set( [d for d in os.listdir(".") if d.startswith("call_")]) self._itgrecv = ITG("ITGRecv") self._itgrecv.start() while self._running: time.sleep(1) call_logs = set([ d for d in os.listdir(".") if d.startswith("call_") and d.endswith(".bin") ]) new_logs = [l for l in call_logs if l not in prev_call_logs] prev_call_logs = call_logs for logf in new_logs: while not self._is_finished_writing(logf): time.sleep(1) self._logger.debug("CALL: %s" % logf) metric = self._parse_log(logf) self._create_metric(metric=metric) self._report_metric() def _parse_log(self, logf): # Summary Statistics cmd = "ITGDec %s -c 1000000 %s.txt" % (logf, logf) self._logger.debug("CMD: %s " % cmd) subprocess.check_call(cmd, shell=True) with open("%s.txt" % logf) as f: stats = f.readlines() # It should be only one line.. assert (len(stats) == 1) m = ["time", "bitrate", "delay", "jitter", "packets_lost"] sp = stats[0].split() stats = {m: float(sp[i]) for i, m in enumerate(m)} # Print metrics summary for debugging cmd = "ITGDec %s > %s.summary.txt" % (logf, logf) self._logger.debug("CMD: %s " % cmd) subprocess.check_call(cmd, shell=True) # Packet count and sizes cmd = "ITGDec %s -P > %s.sizes.txt" % (logf, logf) self._logger.debug("CMD: %s " % cmd) subprocess.check_call(cmd, shell=True) with open("%s.sizes.txt" % logf) as f: sizes = f.readlines() pkt_count = len(sizes) - 2 avg_pkt_size = int(sum([int(p) for p in sizes[2:]]) / pkt_count) stats.update({ 'call': logf, 'rcvd_pkt_count': pkt_count, 'rcvd_avg_pkt_size': avg_pkt_size, 'packet_loss': stats['packets_lost'] / (pkt_count + stats['packets_lost']) }) return stats def _is_finished_writing(self, logf): """ Checks if ITG finished writing the logfile. :param logf: :return: """ cmd = "lsof -t %s" % logf #self._logger.debug("CMD: %s" % cmd) try: out = subprocess.check_output(cmd, shell=True) except subprocess.CalledProcessError: # Exception means no process has the file open right now return True return False def clean_up(self): self._logger.debug("VoIPClient.clean_up()") self._itgrecv.stop() self.unregister_application() def _apply_config(self): self._logger.debug("VoIPClient._apply_config()")