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__)
26 self._send_processes = []
27 self._receive_processes = []
33 @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
34 @deco.check("namespace", defaults=None)
36 @deco.check("time", defaults=600)
37 @deco.check("size", defaults=64)
38 @deco.check("threads", defaults=1)
39 def send_start(self, **kwargs):
41 cmd = self.format_send_start(**kwargs)
42 LOG.debug("cmd:%s", cmd)
44 process = subprocess.Popen(
46 stdout=subprocess.PIPE,
47 stderr=subprocess.PIPE)
52 error_str = "start iperf send success"
53 self._send_processes.append(process)
56 error_str = "start iperf send failed, %s", (str(kwargs))
61 def format_send_start(self, **kwargs):
62 cmd = "iperf %(type)s -c %(dst_ip)s -i 1 -l %(pkt_size)s -t %(time)s -P %(threads)s "
64 'type': self._typemap[kwargs['protocol']],
65 'dst_ip': kwargs['dst'][0]['ip'],
66 'time': kwargs['time'],
67 'pkt_size': kwargs['size'],
68 'threads': kwargs['threads'],
75 for process in self._send_processes:
80 read = "process is stopped by killed"
81 results.append((ret, read))
83 self._send_processes = []
87 def format_receive_start(self, **kwargs):
88 cmd = 'iperf %s -s ' % (self._typemap[kwargs['protocol']])
91 @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
92 @deco.check("namespace", defaults=None)
93 def receive_start(self, **kwargs):
94 cmd = self.format_receive_start(**kwargs)
95 LOG.debug("cmd:%s", cmd)
97 process = subprocess.Popen(
99 stdout=subprocess.PIPE,
100 stderr=subprocess.PIPE)
105 error_str = "start iperf receive success"
106 self._receive_processes.append(process)
109 error_str = "start iperf receive failed, %s" % (str(kwargs))
110 return ret, error_str
112 def receive_stop(self):
114 for process in self._receive_processes:
117 self._receive_processes = []
120 def receive_kill(self):
122 receive_pids = utils.get_pid_by_name('iperf')
123 for pid in receive_pids:
124 os.kill(pid, signal.SIGKILL)
126 error_str = "stop iperf receive success"
128 return ret, error_str
130 def force_clean(self):
131 LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
132 kill_by_name('iperf')
133 self._send_processes = []
134 self._receive_processes = []
141 print perf.receive_start(namespace='receive', protocol=pro)
145 "protocol": "udp_bw",
147 {"ip": "192.168.1.102"}
152 print perf.send_start(**send)
154 print perf.send_stop()
155 print perf.receive_stop()
158 if __name__ == "__main__":
159 from vstf.common.log import setup_logging
163 log_file="/var/log/vstf-iperf.log",
164 clevel=logging.DEBUG)