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__)
23 self._send_processes = []
24 self._receive_processes = []
26 @deco.check("protocol", choices=['tcp_lat', 'udp_lat'])
27 @deco.check("namespace", defaults=None)
29 @deco.check("time", defaults=10)
30 @deco.check("size", defaults=64)
31 def send_start(self, **kwargs):
32 cmd = self.format_send_start(**kwargs)
33 LOG.info("cmd:%s", cmd)
36 stdout=subprocess.PIPE,
37 stderr=subprocess.PIPE)
42 error_str = "start qperf send success"
43 self._send_processes.append(process)
46 error_str = "start qperf send failed, %s" % (str(kwargs))
52 def format_send_start(self, **kwargs):
53 cmd = "qperf %(dst_ip)s -t %(time)s -m %(pkt_size)s -vu %(type)s "
55 'dst_ip': kwargs['dst'][0]['ip'],
56 'type': kwargs['protocol'],
57 'time': kwargs['time'],
58 'pkt_size': kwargs['size'],
63 def send_stop(self, **kwargs):
65 for process in self._send_processes:
67 read = process.stdout.read()
68 read = self._parse_data(read)
70 results.append((ret, read))
71 self._send_processes = []
75 def format_receive_start(self, **kwargs):
79 def receive_start(self, **kwargs):
80 cmd = self.format_receive_start(**kwargs)
81 LOG.info("cmd:%s", cmd)
85 stdout=subprocess.PIPE,
86 stderr=subprocess.PIPE)
91 error_str = "start qperf receive success"
92 self._receive_processes.append(process)
95 error_str = "start qperf receive failed, %s" % (str(kwargs))
97 raise Exception(error_str)
100 def receive_stop(self, **kwargs):
102 for process in self._receive_processes:
105 self._receive_processes = []
106 error_str = "stop qperf receive success"
107 return ret, error_str
109 def receive_kill(self):
110 kill_by_name('qperf')
111 self._receive_processes = []
115 for process in self._receive_processes:
118 LOG.info("process.kill(qperf daemon:%s)", process.pid)
119 for process in self._send_processes:
120 LOG.info("process.wait(qperf client:%s)", process.pid)
122 self._receive_processes = []
123 self._send_processes = []
126 def force_clean(self):
127 LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
128 kill_by_name('qperf')
129 self._send_processes = []
130 self._receive_processes = []
133 def _parse_data(self, data):
137 buf = data.splitlines()
138 if "latency" in buf[1]:
139 data = buf[1].strip().split()
141 latency = float(data[2]) / 1000
143 latency = float(data[2])
145 mark.minLatency: latency,
146 mark.avgLatency: latency,
147 mark.maxLatency: latency
154 perf.receive_start(namespace='receive')
158 "protocol": "udp_lat",
160 {"ip": "192.168.1.102"}
164 print perf.send_start(**send)
166 print perf.send_stop()
167 print perf.receive_stop()
170 if __name__ == "__main__":
171 from vstf.common.log import setup_logging
175 log_file="/var/log/vstf/vstf-qperf.log",
176 clevel=logging.DEBUG)