throughput between nodes (in progress)
[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_ip = host.get('ip', None)
67         target = self.context_cfg['target']
68         target_user = target.get('user', 'ubuntu')
69         target_ip = target.get('ip', None)
70         self.target_ip = target.get('ip', None)
71         host_password = host.get('password', None)
72         target_password = target.get('password', None)
73
74         LOG.info("host_pw:%s, target_pw:%s", host_password, target_password)
75         # netserver start automatically during the vm boot
76         LOG.info("user:%s, target:%s", target_user, target_ip)
77         self.server = ssh.SSH(target_user, target_ip,
78                               password=target_password)
79         self.server.wait(timeout=600)
80
81         LOG.info("user:%s, host:%s", host_user, host_ip)
82         self.client = ssh.SSH(host_user, host_ip,
83                               password=host_password)
84         self.client.wait(timeout=600)
85
86         # copy script to host
87         self.client.run("cat > ~/netperf.sh",
88                         stdin=open(self.target_script, "rb"))
89
90         # copy script to host and client
91         self.install_script = pkg_resources.resource_filename(
92             'yardstick.benchmark.scenarios.networking',
93             NetperfNode.INSTALL_SCRIPT)
94         self.remove_script = pkg_resources.resource_filename(
95             'yardstick.benchmark.scenarios.networking',
96             NetperfNode.REMOVE_SCRIPT)
97
98         self.server.run("cat > ~/netperf_install.sh",
99                         stdin=open(self.install_script, "rb"))
100         self.client.run("cat > ~/netperf_install.sh",
101                         stdin=open(self.install_script, "rb"))
102         self.server.run("cat > ~/netperf_remove.sh",
103                         stdin=open(self.remove_script, "rb"))
104         self.client.run("cat > ~/netperf_remove.sh",
105                         stdin=open(self.remove_script, "rb"))
106         self.server.execute("sudo bash netperf_install.sh")
107         self.client.execute("sudo bash netperf_install.sh")
108
109         self.setup_done = True
110
111     def run(self, result):
112         """execute the benchmark"""
113
114         if not self.setup_done:
115             self.setup()
116
117         # get global options
118         ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
119         ipaddr = self.target_ip
120         options = self.scenario_cfg['options']
121         testname = options.get("testname", 'TCP_STREAM')
122         duration_time = self.scenario_cfg["runner"].get("duration", None) \
123             if "runner" in self.scenario_cfg else None
124         arithmetic_time = options.get("duration", None)
125         if duration_time:
126             testlen = duration_time
127         elif arithmetic_time:
128             testlen = arithmetic_time
129         else:
130             testlen = 20
131
132         cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
133
134         # get test specific options
135         default_args = "-O 'THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY'"
136         cmd_args += " -- %s" % default_args
137         option_pair_list = [("send_msg_size", "-m"),
138                             ("recv_msg_size", "-M"),
139                             ("req_rsp_size", "-r")]
140         for option_pair in option_pair_list:
141             if option_pair[0] in options:
142                 cmd_args += " %s %s" % (option_pair[1],
143                                         options[option_pair[0]])
144
145         cmd = "sudo bash netperf.sh %s" % (cmd_args)
146         LOG.debug("Executing command: %s", cmd)
147         status, stdout, stderr = self.client.execute(cmd)
148
149         if status:
150             raise RuntimeError(stderr)
151
152         result.update(json.loads(stdout))
153
154         if result['mean_latency'] == '':
155             raise RuntimeError(stdout)
156
157         # sla check
158         mean_latency = float(result['mean_latency'])
159         if "sla" in self.scenario_cfg:
160             sla_max_mean_latency = int(
161                 self.scenario_cfg["sla"]["mean_latency"])
162
163             assert mean_latency <= sla_max_mean_latency, \
164                 "mean_latency %f > sla_max_mean_latency(%f); " % \
165                 (mean_latency, sla_max_mean_latency)
166
167     def teardown(self):
168         '''remove netperf from nodes after test'''
169         self.server.execute("sudo bash netperf_remove.sh")
170         self.client.execute("sudo bash netperf_remove.sh")
171
172
173 def _test():    # pragma: no cover
174     '''internal test function'''
175     ctx = {
176         "host": {
177             "ip": "192.168.10.10",
178             "user": "root",
179             "password": "root"
180         },
181         "target": {
182             "ip": "192.168.10.11",
183             "user": "root",
184             "password": "root"
185         }
186     }
187
188     logger = logging.getLogger("yardstick")
189     logger.setLevel(logging.DEBUG)
190
191     options = {
192         "testname": 'TCP_STREAM'
193     }
194
195     args = {"options": options}
196     result = {}
197
198     netperf = NetperfNode(args, ctx)
199     netperf.run(result)
200     print result
201
202 if __name__ == '__main__':
203     _test()