5 # see license for license details
13 import vstf.common.decorator as deco
14 import vstf.agent.perf.utils as utils
15 from vstf.common.utils import kill_by_name
17 LOG = logging.getLogger(__name__)
22 self._send_processes = []
23 self._receive_processes = []
29 @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
30 @deco.check("namespace", defaults=None)
32 @deco.check("time", defaults=600)
33 @deco.check("size", defaults=64)
34 @deco.check("threads", defaults=1)
35 def send_start(self, **kwargs):
37 cmd = self.format_send_start(**kwargs)
38 LOG.debug("cmd:%s", cmd)
40 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45 error_str = "start iperf send success"
46 self._send_processes.append(process)
49 error_str = "start iperf send failed, %s", (str(kwargs))
54 def format_send_start(self, **kwargs):
55 cmd = "iperf %(type)s -c %(dst_ip)s -i 1 -l %(pkt_size)s -t %(time)s -P %(threads)s "
57 'type': self._typemap[kwargs['protocol']],
58 'dst_ip': kwargs['dst'][0]['ip'],
59 'time': kwargs['time'],
60 'pkt_size': kwargs['size'],
61 'threads': kwargs['threads'],
68 for process in self._send_processes:
73 read = "process is stopped by killed"
74 results.append((ret, read))
76 self._send_processes = []
80 def format_receive_start(self, **kwargs):
81 cmd = 'iperf %s -s ' % (self._typemap[kwargs['protocol']])
84 @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
85 @deco.check("namespace", defaults=None)
86 def receive_start(self, **kwargs):
87 cmd = self.format_receive_start(**kwargs)
88 LOG.debug("cmd:%s", cmd)
90 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
95 error_str = "start iperf receive success"
96 self._receive_processes.append(process)
99 error_str = "start iperf receive failed, %s" % (str(kwargs))
100 return ret, error_str
102 def receive_stop(self):
104 for process in self._receive_processes:
107 self._receive_processes = []
110 def receive_kill(self):
112 receive_pids = utils.get_pid_by_name('iperf')
113 for pid in receive_pids:
114 os.kill(pid, signal.SIGKILL)
116 error_str = "stop iperf receive success"
118 return ret, error_str
120 def force_clean(self):
121 LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
122 kill_by_name('iperf')
123 self._send_processes = []
124 self._receive_processes = []
131 print perf.receive_start(namespace='receive', protocol=pro)
135 "protocol": "udp_bw",
137 {"ip": "192.168.1.102"}
142 print perf.send_start(**send)
144 print perf.send_stop()
145 print perf.receive_stop()
148 if __name__ == "__main__":
149 from vstf.common.log import setup_logging
151 setup_logging(level=logging.DEBUG, log_file="/var/log/vstf-iperf.log", clevel=logging.DEBUG)