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 ##############################################################################
12 import vstf.common.constants as cst
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
19 LOG = logging.getLogger(__name__)
22 class Netperf(object):
24 self._send_processes = []
27 "tcp_lat": "TCP_STREAM",
28 "tcp_bw": "TCP_STREAM",
29 "udp_lat": "UDP_STREAM",
30 "udp_bw": "UDP_STREAM",
33 @deco.check("protocol", choices=cst.PROTOCOLS)
34 @deco.check("namespace", defaults=None)
36 @deco.check("time", defaults=0)
37 @deco.check("size", defaults=64)
38 @deco.check("threads", defaults=1)
39 def send_start(self, **kwargs):
40 threads = kwargs.pop('threads')
41 kwargs['buf'] = cst.SOCKET_BUF
42 if kwargs['protocol'] in ['tcp_lat', 'udp_lat']:
47 cmd = self.format_send_start(**kwargs)
48 LOG.info("cmd:%s", cmd)
50 for _ in range(threads):
51 process = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
52 self._send_processes.append(process)
54 for process in self._send_processes:
58 error_str = "start netperf send success"
60 error_str = "start netperf send failed, %s" % (str(kwargs))
62 self._send_processes.remove(process)
66 def send_stop(self, **kwargs):
70 for process in self._send_processes:
75 read = "process is stopped by killed"
78 read = process.stdout.read()
79 read = self._parse_data(read)
80 results.append((ret, read))
81 self._send_processes = []
86 def _parse_data(data):
87 buf = data.splitlines()
88 data = buf[2].strip().split(',')
90 mark.minLatency: float(data[0]),
91 mark.avgLatency: float(data[1]),
92 mark.maxLatency: float(data[2])
97 def format_send_start(self, **kwargs):
98 # cmd = "netperf -H %(dst_ip)s -t %(type)s -l %(time)s -- -m %(pkt_size)s "
99 cmd = "netperf -H %(dst_ip)s -t %(type)s -l %(time)s " \
100 "-- -m %(pkt_size)s -s %(buf)s -S %(buf)s -o MIN_LATENCY,MEAN_LATENCY,MAX_LATENCY"
102 'dst_ip': kwargs['dst'][0]['ip'],
103 'type': self._typemap[kwargs['protocol']],
104 'time': kwargs['time'],
105 'pkt_size': kwargs['size'],
106 'buf': kwargs['buf'],
112 def format_receive_start(self, **kwargs):
116 @deco.check("namespace")
117 def receive_start(self, **kwargs):
119 cmd = self.format_receive_start(**kwargs)
120 LOG.info("cmd:%s", cmd)
122 process = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
126 error_str = "start netserver failed, %s" % (str(kwargs))
129 error_str = "start netserver success"
131 return ret, error_str
133 def receive_stop(self, **kwargs):
134 LOG.info("receive_stop")
136 kill_by_name('netserver')
138 error_str = "stop netserver success"
139 return ret, error_str
146 def force_clean(self):
147 LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
148 kill_by_name('netserver')
149 kill_by_name('netperf')
150 self._send_processes = []
151 self._receive_processes = []
157 ret = perf.receive_start(namespace='receive')
158 print "*********receive_start***********"
162 "protocol": "udp_lat",
164 {"ip": "192.168.1.102"}
170 print perf.send_start(**send)
171 print perf._send_processes
173 print perf.send_stop()
174 print perf.receive_stop()
177 if __name__ == "__main__":
178 from vstf.common.log import setup_logging
180 setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-netperf.log", clevel=logging.DEBUG)