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_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)
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)
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)
89 self.client.run("cat > ~/netperf.sh",
90 stdin=open(self.target_script, "rb"))
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)
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")
111 self.setup_done = True
113 def run(self, result):
114 """execute the benchmark"""
116 if not self.setup_done:
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)
128 testlen = duration_time
129 elif arithmetic_time:
130 testlen = arithmetic_time
134 cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
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]])
147 cmd = "sudo bash netperf.sh %s" % (cmd_args)
148 LOG.debug("Executing command: %s", cmd)
149 status, stdout, stderr = self.client.execute(cmd)
152 raise RuntimeError(stderr)
154 result.update(json.loads(stdout))
156 if result['mean_latency'] == '':
157 raise RuntimeError(stdout)
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"])
165 assert mean_latency <= sla_max_mean_latency, \
166 "mean_latency %f > sla_max_mean_latency(%f); " % \
167 (mean_latency, sla_max_mean_latency)
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")
175 def _test(): # pragma: no cover
176 '''internal test function'''
179 "ip": "192.168.10.10",
184 "ip": "192.168.10.11",
190 logger = logging.getLogger("yardstick")
191 logger.setLevel(logging.DEBUG)
194 "testname": 'TCP_STREAM'
197 args = {"options": options}
200 netperf = NetperfNode(args, ctx)
204 if __name__ == '__main__':