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
14 import yardstick.ssh as ssh
15 from yardstick.benchmark.scenarios import base
17 LOG = logging.getLogger(__name__)
20 class NetperfNode(base.Scenario):
21 """Execute netperf between two nodes
24 testname - to specify the test you wish to perform.
25 the valid testnames are TCP_STREAM, TCP_RR, UDP_STREAM, UDP_RR
29 send_msg_size - value set the local send size to value bytes.
33 recv_msg_size - setting the receive size for the remote system.
37 req_rsp_size - set the request and/or response sizes based on sizespec.
41 duration - duration of the test
46 read link below for more netperf args description:
47 http://www.netperf.org/netperf/training/Netperf.html
49 __scenario_type__ = "NetperfNode"
50 TARGET_SCRIPT = 'netperf_benchmark.bash'
51 INSTALL_SCRIPT = 'netperf_install.bash'
52 REMOVE_SCRIPT = 'netperf_remove.bash'
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
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)
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)
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)
87 self.client.run("cat > ~/netperf.sh",
88 stdin=open(self.target_script, "rb"))
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)
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")
109 self.setup_done = True
111 def run(self, result):
112 """execute the benchmark"""
114 if not self.setup_done:
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)
126 testlen = duration_time
127 elif arithmetic_time:
128 testlen = arithmetic_time
132 cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
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]])
145 cmd = "sudo bash netperf.sh %s" % (cmd_args)
146 LOG.debug("Executing command: %s", cmd)
147 status, stdout, stderr = self.client.execute(cmd)
150 raise RuntimeError(stderr)
152 result.update(json.loads(stdout))
154 if result['mean_latency'] == '':
155 raise RuntimeError(stdout)
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"])
163 assert mean_latency <= sla_max_mean_latency, \
164 "mean_latency %f > sla_max_mean_latency(%f); " % \
165 (mean_latency, sla_max_mean_latency)
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")
173 def _test(): # pragma: no cover
174 '''internal test function'''
177 "ip": "192.168.10.10",
182 "ip": "192.168.10.11",
188 logger = logging.getLogger("yardstick")
189 logger.setLevel(logging.DEBUG)
192 "testname": 'TCP_STREAM'
195 args = {"options": options}
198 netperf = NetperfNode(args, ctx)
202 if __name__ == '__main__':