1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
16 import vstf.common.decorator as deco
17 import vstf.agent.perf.utils as utils
18 from vstf.common.utils import kill_by_name
20 LOG = logging.getLogger(__name__)
25 self._send_processes = []
26 self._receive_processes = []
32 @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
33 @deco.check("namespace", defaults=None)
35 @deco.check("time", defaults=600)
36 @deco.check("size", defaults=64)
37 @deco.check("threads", defaults=1)
38 def send_start(self, **kwargs):
40 cmd = self.format_send_start(**kwargs)
41 LOG.debug("cmd:%s", cmd)
43 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
48 error_str = "start iperf send success"
49 self._send_processes.append(process)
52 error_str = "start iperf send failed, %s", (str(kwargs))
57 def format_send_start(self, **kwargs):
58 cmd = "iperf %(type)s -c %(dst_ip)s -i 1 -l %(pkt_size)s -t %(time)s -P %(threads)s "
60 'type': self._typemap[kwargs['protocol']],
61 'dst_ip': kwargs['dst'][0]['ip'],
62 'time': kwargs['time'],
63 'pkt_size': kwargs['size'],
64 'threads': kwargs['threads'],
71 for process in self._send_processes:
76 read = "process is stopped by killed"
77 results.append((ret, read))
79 self._send_processes = []
83 def format_receive_start(self, **kwargs):
84 cmd = 'iperf %s -s ' % (self._typemap[kwargs['protocol']])
87 @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
88 @deco.check("namespace", defaults=None)
89 def receive_start(self, **kwargs):
90 cmd = self.format_receive_start(**kwargs)
91 LOG.debug("cmd:%s", cmd)
93 process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98 error_str = "start iperf receive success"
99 self._receive_processes.append(process)
102 error_str = "start iperf receive failed, %s" % (str(kwargs))
103 return ret, error_str
105 def receive_stop(self):
107 for process in self._receive_processes:
110 self._receive_processes = []
113 def receive_kill(self):
115 receive_pids = utils.get_pid_by_name('iperf')
116 for pid in receive_pids:
117 os.kill(pid, signal.SIGKILL)
119 error_str = "stop iperf receive success"
121 return ret, error_str
123 def force_clean(self):
124 LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
125 kill_by_name('iperf')
126 self._send_processes = []
127 self._receive_processes = []
134 print perf.receive_start(namespace='receive', protocol=pro)
138 "protocol": "udp_bw",
140 {"ip": "192.168.1.102"}
145 print perf.send_start(**send)
147 print perf.send_stop()
148 print perf.receive_stop()
151 if __name__ == "__main__":
152 from vstf.common.log import setup_logging
154 setup_logging(level=logging.DEBUG, log_file="/var/log/vstf-iperf.log", clevel=logging.DEBUG)