Merge "Add support for Storperf job status"
[yardstick.git] / yardstick / benchmark / scenarios / networking / netperf_node.py
1 ##############################################################################
2 # Copyright (c) 2016 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 # bulk data test and req/rsp test are supported
10 import pkg_resources
11 import logging
12 import json
13
14 import yardstick.ssh as ssh
15 from yardstick.benchmark.scenarios import base
16
17 LOG = logging.getLogger(__name__)
18
19
20 class NetperfNode(base.Scenario):
21     """Execute netperf between two nodes
22
23   Parameters
24     testname - to specify the test you wish to perform.
25     the valid testnames are TCP_STREAM, TCP_RR, UDP_STREAM, UDP_RR
26         type:    string
27         unit:    na
28         default: TCP_STREAM
29     send_msg_size - value set the local send size to value bytes.
30         type:    int
31         unit:    bytes
32         default: na
33     recv_msg_size - setting the receive size for the remote system.
34         type:    int
35         unit:    bytes
36         default: na
37     req_rsp_size - set the request and/or response sizes based on sizespec.
38         type:    string
39         unit:    na
40         default: na
41     duration - duration of the test
42         type:    int
43         unit:    seconds
44         default: 20
45
46     read link below for more netperf args description:
47     http://www.netperf.org/netperf/training/Netperf.html
48     """
49     __scenario_type__ = "NetperfNode"
50     TARGET_SCRIPT = 'netperf_benchmark.bash'
51     INSTALL_SCRIPT = 'netperf_install.bash'
52     REMOVE_SCRIPT = 'netperf_remove.bash'
53
54     def __init__(self, scenario_cfg, context_cfg):
55         self.scenario_cfg = scenario_cfg
56         self.context_cfg = context_cfg
57         self.setup_done = False
58
59     def setup(self):
60         '''scenario setup'''
61         self.target_script = pkg_resources.resource_filename(
62             'yardstick.benchmark.scenarios.networking',
63             NetperfNode.TARGET_SCRIPT)
64         host = self.context_cfg['host']
65         host_user = host.get('user', 'ubuntu')
66         host_ssh_port = host.get('ssh_port', ssh.DEFAULT_PORT)
67         host_ip = host.get('ip', None)
68         target = self.context_cfg['target']
69         target_user = target.get('user', 'ubuntu')
70         target_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
71         target_ip = target.get('ip', None)
72         self.target_ip = target.get('ip', None)
73         host_password = host.get('password', None)
74         target_password = target.get('password', None)
75
76         LOG.info("host_pw:%s, target_pw:%s", host_password, target_password)
77         # netserver start automatically during the vm boot
78         LOG.info("user:%s, target:%s", target_user, target_ip)
79         self.server = ssh.SSH(target_user, target_ip,
80                               password=target_password, port=target_ssh_port)
81         self.server.wait(timeout=600)
82
83         LOG.info("user:%s, host:%s", host_user, host_ip)
84         self.client = ssh.SSH(host_user, host_ip,
85                               password=host_password, port=host_ssh_port)
86         self.client.wait(timeout=600)
87
88         # copy script to host
89         with open(self.target_script, "rb") as file_run:
90             self.client.run("cat > ~/netperf.sh", stdin=file_run)
91         # copy script to host and client
92         self.install_script = pkg_resources.resource_filename(
93             'yardstick.benchmark.scenarios.networking',
94             NetperfNode.INSTALL_SCRIPT)
95         self.remove_script = pkg_resources.resource_filename(
96             'yardstick.benchmark.scenarios.networking',
97             NetperfNode.REMOVE_SCRIPT)
98
99         with open(self.install_script, "rb") as file_install:
100             self.server.run("cat > ~/netperf_install.sh", stdin=file_install)
101         with open(self.install_script, "rb") as file_install:
102             self.client.run("cat > ~/netperf_install.sh", stdin=file_install)
103         with open(self.remove_script, "rb") as file_remove:
104             self.server.run("cat > ~/netperf_remove.sh", stdin=file_remove)
105         with open(self.remove_script, "rb") as file_remove:
106             self.client.run("cat > ~/netperf_remove.sh", stdin=file_remove)
107         self.server.execute("sudo bash netperf_install.sh")
108         self.client.execute("sudo bash netperf_install.sh")
109
110         self.setup_done = True
111
112     def run(self, result):
113         """execute the benchmark"""
114
115         if not self.setup_done:
116             self.setup()
117
118         # get global options
119         ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
120         ipaddr = self.target_ip
121         options = self.scenario_cfg['options']
122         testname = options.get("testname", 'TCP_STREAM')
123         duration_time = self.scenario_cfg["runner"].get("duration", None) \
124             if "runner" in self.scenario_cfg else None
125         arithmetic_time = options.get("duration", None)
126         if duration_time:
127             testlen = duration_time
128         elif arithmetic_time:
129             testlen = arithmetic_time
130         else:
131             testlen = 20
132
133         cmd_args = "-H %s -l %s -t %s -c -C" % (ipaddr, testlen, testname)
134
135         # get test specific options
136         output_opt = options.get(
137             "output_opt", "THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY")
138         default_args = "-O %s" % output_opt
139         cmd_args += " -- %s" % default_args
140         option_pair_list = [("send_msg_size", "-m"),
141                             ("recv_msg_size", "-M"),
142                             ("req_rsp_size", "-r")]
143         for option_pair in option_pair_list:
144             if option_pair[0] in options:
145                 cmd_args += " %s %s" % (option_pair[1],
146                                         options[option_pair[0]])
147
148         cmd = "sudo bash netperf.sh %s" % (cmd_args)
149         LOG.debug("Executing command: %s", cmd)
150         status, stdout, stderr = self.client.execute(cmd)
151
152         if status:
153             raise RuntimeError(stderr)
154
155         result.update(json.loads(stdout))
156
157         if result['mean_latency'] == '':
158             raise RuntimeError(stdout)
159
160         # sla check
161         mean_latency = float(result['mean_latency'])
162         if "sla" in self.scenario_cfg:
163             sla_max_mean_latency = int(
164                 self.scenario_cfg["sla"]["mean_latency"])
165
166             assert mean_latency <= sla_max_mean_latency, \
167                 "mean_latency %f > sla_max_mean_latency(%f); " % \
168                 (mean_latency, sla_max_mean_latency)
169
170     def teardown(self):
171         '''remove netperf from nodes after test'''
172         self.server.execute("sudo bash netperf_remove.sh")
173         self.client.execute("sudo bash netperf_remove.sh")
174
175
176 def _test():    # pragma: no cover
177     '''internal test function'''
178     ctx = {
179         "host": {
180             "ip": "192.168.10.10",
181             "user": "root",
182             "password": "root"
183         },
184         "target": {
185             "ip": "192.168.10.11",
186             "user": "root",
187             "password": "root"
188         }
189     }
190
191     logger = logging.getLogger("yardstick")
192     logger.setLevel(logging.DEBUG)
193
194     options = {
195         "testname": 'TCP_STREAM'
196     }
197
198     args = {"options": options}
199     result = {}
200
201     netperf = NetperfNode(args, ctx)
202     netperf.run(result)
203     print result
204
205 if __name__ == '__main__':
206     _test()