Merge "autopep8 fix for flake8"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / perf / iperf.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 import subprocess
11 import signal
12 import os
13 import time
14 import logging
15
16 import vstf.common.decorator as deco
17 import vstf.agent.perf.utils as utils
18 from vstf.common.utils import kill_by_name
19
20 LOG = logging.getLogger(__name__)
21
22
23 class Iperf(object):
24
25     def __init__(self):
26         self._send_processes = []
27         self._receive_processes = []
28         self._typemap = {
29             "tcp_bw": "",
30             "udp_bw": " -u ",
31         }
32
33     @deco.check("protocol", choices=['tcp_bw', 'udp_bw'])
34     @deco.check("namespace", defaults=None)
35     @deco.check("dst")
36     @deco.check("time", defaults=600)
37     @deco.check("size", defaults=64)
38     @deco.check("threads", defaults=1)
39     def send_start(self, **kwargs):
40
41         cmd = self.format_send_start(**kwargs)
42         LOG.debug("cmd:%s", cmd)
43
44         process = subprocess.Popen(
45             cmd.split(),
46             stdout=subprocess.PIPE,
47             stderr=subprocess.PIPE)
48         time.sleep(1)
49         ret = process.poll()
50         if ret is None:
51             ret = 0
52             error_str = "start iperf send success"
53             self._send_processes.append(process)
54         else:
55             print ret
56             error_str = "start iperf send failed, %s", (str(kwargs))
57
58         return ret, error_str
59
60     @deco.namespace()
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 "
63         context = {
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'],
69         }
70         cmd = cmd % context
71         return cmd
72
73     def send_stop(self):
74         results = []
75         for process in self._send_processes:
76             poll = process.poll()
77             if poll is None:
78                 process.kill()
79                 ret = 0
80                 read = "process is stopped by killed"
81                 results.append((ret, read))
82
83         self._send_processes = []
84         return results
85
86     @deco.namespace()
87     def format_receive_start(self, **kwargs):
88         cmd = 'iperf %s -s ' % (self._typemap[kwargs['protocol']])
89         return cmd
90
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)
96
97         process = subprocess.Popen(
98             cmd.split(),
99             stdout=subprocess.PIPE,
100             stderr=subprocess.PIPE)
101         time.sleep(1)
102         ret = process.poll()
103         if ret is None:
104             ret = 0
105             error_str = "start iperf receive success"
106             self._receive_processes.append(process)
107         else:
108             print ret
109             error_str = "start iperf receive failed, %s" % (str(kwargs))
110         return ret, error_str
111
112     def receive_stop(self):
113         ret = 0
114         for process in self._receive_processes:
115             process.kill()
116             ret = process.wait()
117         self._receive_processes = []
118         return ret
119
120     def receive_kill(self):
121         ret = 0
122         receive_pids = utils.get_pid_by_name('iperf')
123         for pid in receive_pids:
124             os.kill(pid, signal.SIGKILL)
125             time.sleep(0.5)
126         error_str = "stop iperf receive success"
127         LOG.debug(error_str)
128         return ret, error_str
129
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 = []
135         return True
136
137
138 def unit_test():
139     perf = Iperf()
140     pro = 'udp_bw'
141     print perf.receive_start(namespace='receive', protocol=pro)
142
143     send = {
144         "namespace": "send",
145         "protocol": "udp_bw",
146         "dst": [
147             {"ip": "192.168.1.102"}
148         ],
149         "size": 64,
150         "time": 5,
151     }
152     print perf.send_start(**send)
153     time.sleep(10)
154     print perf.send_stop()
155     print perf.receive_stop()
156
157
158 if __name__ == "__main__":
159     from vstf.common.log import setup_logging
160
161     setup_logging(
162         level=logging.DEBUG,
163         log_file="/var/log/vstf-iperf.log",
164         clevel=logging.DEBUG)
165     unit_test()