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