1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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
16 from oslo_serialization import jsonutils
18 import yardstick.ssh as ssh
19 from yardstick.benchmark.scenarios import base
21 LOG = logging.getLogger(__name__)
24 class NetperfNode(base.Scenario):
25 """Execute netperf between two nodes
28 testname - to specify the test you wish to perform.
29 the valid testnames are TCP_STREAM, TCP_RR, UDP_STREAM, UDP_RR
33 send_msg_size - value set the local send size to value bytes.
37 recv_msg_size - setting the receive size for the remote system.
41 req_rsp_size - set the request and/or response sizes based on sizespec.
45 duration - duration of the test
50 read link below for more netperf args description:
51 http://www.netperf.org/netperf/training/Netperf.html
53 __scenario_type__ = "NetperfNode"
54 TARGET_SCRIPT = 'netperf_benchmark.bash'
55 INSTALL_SCRIPT = 'netperf_install.bash'
56 REMOVE_SCRIPT = 'netperf_remove.bash'
58 def __init__(self, scenario_cfg, context_cfg):
59 self.scenario_cfg = scenario_cfg
60 self.context_cfg = context_cfg
61 self.setup_done = False
65 self.target_script = pkg_resources.resource_filename(
66 'yardstick.benchmark.scenarios.networking',
67 NetperfNode.TARGET_SCRIPT)
68 host = self.context_cfg['host']
69 target = self.context_cfg['target']
70 self.target_ip = target['ip']
72 # netserver start automatically during the vm boot
73 LOG.info("user:%s, target:%s", target['user'], target['ip'])
74 self.server = ssh.SSH.from_node(target, defaults={"user": "ubuntu"})
75 self.server.wait(timeout=600)
77 LOG.info("user:%s, host:%s", host['user'], host['ip'])
78 self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
79 self.client.wait(timeout=600)
82 with open(self.target_script, "rb") as file_run:
83 self.client.run("cat > ~/netperf.sh", stdin=file_run)
84 # copy script to host and client
85 self.install_script = pkg_resources.resource_filename(
86 'yardstick.benchmark.scenarios.networking',
87 NetperfNode.INSTALL_SCRIPT)
88 self.remove_script = pkg_resources.resource_filename(
89 'yardstick.benchmark.scenarios.networking',
90 NetperfNode.REMOVE_SCRIPT)
92 with open(self.install_script, "rb") as file_install:
93 self.server.run("cat > ~/netperf_install.sh", stdin=file_install)
94 with open(self.install_script, "rb") as file_install:
95 self.client.run("cat > ~/netperf_install.sh", stdin=file_install)
96 with open(self.remove_script, "rb") as file_remove:
97 self.server.run("cat > ~/netperf_remove.sh", stdin=file_remove)
98 with open(self.remove_script, "rb") as file_remove:
99 self.client.run("cat > ~/netperf_remove.sh", stdin=file_remove)
100 self.server.execute("sudo bash netperf_install.sh")
101 self.client.execute("sudo bash netperf_install.sh")
103 self.setup_done = True
105 def run(self, result):
106 """execute the benchmark"""
108 if not self.setup_done:
112 ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
113 ipaddr = self.target_ip
114 options = self.scenario_cfg['options']
115 testname = options.get("testname", 'TCP_STREAM')
116 duration_time = self.scenario_cfg["runner"].get("duration", None) \
117 if "runner" in self.scenario_cfg else None
118 arithmetic_time = options.get("duration", None)
120 testlen = duration_time
121 elif arithmetic_time:
122 testlen = arithmetic_time
126 cmd_args = "-H %s -l %s -t %s -c -C" % (ipaddr, testlen, testname)
128 # get test specific options
129 output_opt = options.get(
130 "output_opt", "THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY")
131 default_args = "-O %s" % output_opt
132 cmd_args += " -- %s" % default_args
133 option_pair_list = [("send_msg_size", "-m"),
134 ("recv_msg_size", "-M"),
135 ("req_rsp_size", "-r")]
136 for option_pair in option_pair_list:
137 if option_pair[0] in options:
138 cmd_args += " %s %s" % (option_pair[1],
139 options[option_pair[0]])
141 cmd = "sudo bash netperf.sh %s" % (cmd_args)
142 LOG.debug("Executing command: %s", cmd)
143 status, stdout, stderr = self.client.execute(cmd)
146 raise RuntimeError(stderr)
148 result.update(jsonutils.loads(stdout))
150 if result['mean_latency'] == '':
151 raise RuntimeError(stdout)
154 mean_latency = float(result['mean_latency'])
155 if "sla" in self.scenario_cfg:
156 sla_max_mean_latency = int(
157 self.scenario_cfg["sla"]["mean_latency"])
159 assert mean_latency <= sla_max_mean_latency, \
160 "mean_latency %f > sla_max_mean_latency(%f); " % \
161 (mean_latency, sla_max_mean_latency)
164 """remove netperf from nodes after test"""
165 self.server.execute("sudo bash netperf_remove.sh")
166 self.client.execute("sudo bash netperf_remove.sh")
169 def _test(): # pragma: no cover
170 """internal test function"""
173 "ip": "192.168.10.10",
178 "ip": "192.168.10.11",
184 logger = logging.getLogger("yardstick")
185 logger.setLevel(logging.DEBUG)
188 "testname": 'TCP_STREAM'
191 args = {"options": options}
194 netperf = NetperfNode(args, ctx)
199 if __name__ == '__main__':