Update Intel Copyright for files edited in 2019
[yardstick.git] / yardstick / benchmark / scenarios / networking / netperf.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 # bulk data test and req/rsp test are supported
10 from __future__ import absolute_import
11 from __future__ import print_function
12
13 import logging
14
15 import pkg_resources
16 from oslo_serialization import jsonutils
17
18 import yardstick.ssh as ssh
19 from yardstick.benchmark.scenarios import base
20
21 LOG = logging.getLogger(__name__)
22
23
24 class Netperf(base.Scenario):
25     """Execute netperf between two hosts
26
27   Parameters
28     testname - to specify the test you wish to perform.
29     the valid testnames are TCP_STREAM, TCP_RR, UDP_STREAM, UDP_RR
30         type:    string
31         unit:    na
32         default: TCP_STREAM
33     send_msg_size - value set the local send size to value bytes.
34         type:    int
35         unit:    bytes
36         default: na
37     recv_msg_size - setting the receive size for the remote system.
38         type:    int
39         unit:    bytes
40         default: na
41     req_rsp_size - set the request and/or response sizes based on sizespec.
42         type:    string
43         unit:    na
44         default: na
45     duration - duration of the test
46         type:    int
47         unit:    seconds
48         default: 20
49
50     read link below for more netperf args description:
51     http://www.netperf.org/netperf/training/Netperf.html
52     """
53     __scenario_type__ = "Netperf"
54
55     TARGET_SCRIPT = 'netperf_benchmark.bash'
56
57     def __init__(self, scenario_cfg, context_cfg):
58         self.scenario_cfg = scenario_cfg
59         self.context_cfg = context_cfg
60         self.setup_done = False
61
62     def setup(self):
63         """scenario setup"""
64         self.target_script = pkg_resources.resource_filename(
65             'yardstick.benchmark.scenarios.networking',
66             Netperf.TARGET_SCRIPT)
67         host = self.context_cfg['host']
68         target = self.context_cfg['target']
69
70         # netserver start automatically during the vm boot
71         LOG.info("user:%s, target:%s", target['user'], target['ip'])
72         self.server = ssh.SSH.from_node(target, defaults={"user": "ubuntu"})
73         self.server.wait(timeout=600)
74
75         LOG.info("user:%s, host:%s", host['user'], host['ip'])
76         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
77         self.client.wait(timeout=600)
78
79         # copy script to host
80         self.client._put_file_shell(self.target_script, '~/netperf.sh')
81
82         self.setup_done = True
83
84     def run(self, result):
85         """execute the benchmark"""
86
87         if not self.setup_done:
88             self.setup()
89
90         # get global options
91         ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
92         options = self.scenario_cfg['options']
93         testname = options.get("testname", 'TCP_STREAM')
94         duration_time = self.scenario_cfg["runner"].get("duration", None) \
95             if "runner" in self.scenario_cfg else None
96         arithmetic_time = options.get("duration", None)
97         if duration_time:
98             testlen = duration_time
99         elif arithmetic_time:
100             testlen = arithmetic_time
101         else:
102             testlen = 20
103
104         cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
105
106         # get test specific options
107         output_opt = options.get(
108             "output_opt", "THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY")
109         default_args = "-O %s" % output_opt
110         cmd_args += " -- %s" % default_args
111         option_pair_list = [("send_msg_size", "-m"),
112                             ("recv_msg_size", "-M"),
113                             ("req_rsp_size", "-r")]
114         for option_pair in option_pair_list:
115             if option_pair[0] in options:
116                 cmd_args += " %s %s" % (option_pair[1],
117                                         options[option_pair[0]])
118
119         # Enable IP routing for UDP_STREAM test
120         if testname == "UDP_STREAM":
121             cmd_args += " -R 1"
122
123         cmd = "sudo bash netperf.sh %s" % (cmd_args)
124         LOG.debug("Executing command: %s", cmd)
125         status, stdout, stderr = self.client.execute(cmd)
126
127         if status:
128             raise RuntimeError(stderr)
129
130         result.update(jsonutils.loads(stdout))
131
132         if result['mean_latency'] == '':
133             raise RuntimeError(stdout)
134
135         # sla check
136         mean_latency = float(result['mean_latency'])
137         if "sla" in self.scenario_cfg:
138             sla_max_mean_latency = int(
139                 self.scenario_cfg["sla"]["mean_latency"])
140
141             self.verify_SLA(mean_latency <= sla_max_mean_latency,
142                             "mean_latency %f > sla_max_mean_latency(%f); "
143                             % (mean_latency, sla_max_mean_latency))
144
145
146 def _test():
147     """internal test function"""
148     key_filename = pkg_resources.resource_filename("yardstick.resources",
149                                                    "files/yardstick_key")
150     ctx = {
151         "host": {
152             "ip": "10.229.47.137",
153             "user": "root",
154             "key_filename": key_filename
155         },
156         "target": {
157             "ip": "10.229.47.137",
158             "user": "root",
159             "key_filename": key_filename,
160             "ipaddr": "10.229.47.137"
161         }
162     }
163
164     logger = logging.getLogger("yardstick")
165     logger.setLevel(logging.DEBUG)
166
167     options = {
168         "testname": 'TCP_STREAM'
169     }
170
171     args = {"options": options}
172     result = {}
173
174     netperf = Netperf(args, ctx)
175     netperf.run(result)
176     print(result)
177
178
179 if __name__ == '__main__':
180     _test()