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 ##############################################################################
13 import vstf.common.decorator as deco
14 from vstf.common import perfmark as mark
15 from vstf.common.utils import kill_by_name, my_popen
17 LOG = logging.getLogger(__name__)
22 self._send_processes = []
23 self._receive_processes = []
25 @deco.check("protocol", choices=['tcp_lat', 'udp_lat'])
26 @deco.check("namespace", defaults=None)
28 @deco.check("time", defaults=10)
29 @deco.check("size", defaults=64)
30 def send_start(self, **kwargs):
31 cmd = self.format_send_start(**kwargs)
32 LOG.info("cmd:%s", cmd)
33 process = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
38 error_str = "start qperf send success"
39 self._send_processes.append(process)
42 error_str = "start qperf send failed, %s" % (str(kwargs))
48 def format_send_start(self, **kwargs):
49 cmd = "qperf %(dst_ip)s -t %(time)s -m %(pkt_size)s -vu %(type)s "
51 'dst_ip': kwargs['dst'][0]['ip'],
52 'type': kwargs['protocol'],
53 'time': kwargs['time'],
54 'pkt_size': kwargs['size'],
59 def send_stop(self, **kwargs):
61 for process in self._send_processes:
63 read = process.stdout.read()
64 read = self._parse_data(read)
66 results.append((ret, read))
67 self._send_processes = []
71 def format_receive_start(self, **kwargs):
75 def receive_start(self, **kwargs):
76 cmd = self.format_receive_start(**kwargs)
77 LOG.info("cmd:%s", cmd)
79 process = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
84 error_str = "start qperf receive success"
85 self._receive_processes.append(process)
88 error_str = "start qperf receive failed, %s" % (str(kwargs))
90 raise Exception(error_str)
93 def receive_stop(self, **kwargs):
95 for process in self._receive_processes:
98 self._receive_processes = []
99 error_str = "stop qperf receive success"
100 return ret, error_str
102 def receive_kill(self):
103 kill_by_name('qperf')
104 self._receive_processes = []
108 for process in self._receive_processes:
111 LOG.info("process.kill(qperf daemon:%s)", process.pid)
112 for process in self._send_processes:
113 LOG.info("process.wait(qperf client:%s)", process.pid)
115 self._receive_processes = []
116 self._send_processes = []
119 def force_clean(self):
120 LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
121 kill_by_name('qperf')
122 self._send_processes = []
123 self._receive_processes = []
126 def _parse_data(self, data):
130 buf = data.splitlines()
131 if "latency" in buf[1]:
132 data = buf[1].strip().split()
134 latency = float(data[2]) / 1000
136 latency = float(data[2])
138 mark.minLatency: latency,
139 mark.avgLatency: latency,
140 mark.maxLatency: latency
147 perf.receive_start(namespace='receive')
151 "protocol": "udp_lat",
153 {"ip": "192.168.1.102"}
157 print perf.send_start(**send)
159 print perf.send_stop()
160 print perf.receive_stop()
163 if __name__ == "__main__":
164 from vstf.common.log import setup_logging
166 setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-qperf.log", clevel=logging.DEBUG)