1 ##############################################################################
2 # Copyright (c) 2015 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 Netperf(base.Scenario):
21 """Execute netperf between two hosts
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__ = "Netperf"
51 TARGET_SCRIPT = 'netperf_benchmark.bash'
53 def __init__(self, scenario_cfg, context_cfg):
54 self.scenario_cfg = scenario_cfg
55 self.context_cfg = context_cfg
56 self.setup_done = False
60 self.target_script = pkg_resources.resource_filename(
61 'yardstick.benchmark.scenarios.networking',
62 Netperf.TARGET_SCRIPT)
63 host = self.context_cfg['host']
64 host_user = host.get('user', 'ubuntu')
65 host_ip = host.get('ip', None)
66 host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
67 target = self.context_cfg['target']
68 target_user = target.get('user', 'ubuntu')
69 target_ip = target.get('ip', None)
70 target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
72 # netserver start automatically during the vm boot
73 LOG.info("user:%s, target:%s", target_user, target_ip)
74 self.server = ssh.SSH(target_user, target_ip,
75 key_filename=target_key_filename)
76 self.server.wait(timeout=600)
78 LOG.info("user:%s, host:%s", host_user, host_ip)
79 self.client = ssh.SSH(host_user, host_ip,
80 key_filename=host_key_filename)
81 self.client.wait(timeout=600)
84 self.client.run("cat > ~/netperf.sh",
85 stdin=open(self.target_script, "rb"))
87 self.setup_done = True
89 def run(self, result):
90 """execute the benchmark"""
92 if not self.setup_done:
96 ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
97 options = self.scenario_cfg['options']
98 testname = options.get("testname", 'TCP_STREAM')
99 duration_time = self.scenario_cfg["runner"].get("duration", None) \
100 if "runner" in self.scenario_cfg else None
101 arithmetic_time = options.get("duration", None)
103 testlen = duration_time
104 elif arithmetic_time:
105 testlen = arithmetic_time
109 cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
111 # get test specific options
112 default_args = "-O 'THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY'"
113 cmd_args += " -- %s" % default_args
114 option_pair_list = [("send_msg_size", "-m"),
115 ("recv_msg_size", "-M"),
116 ("req_rsp_size", "-r")]
117 for option_pair in option_pair_list:
118 if option_pair[0] in options:
119 cmd_args += " %s %s" % (option_pair[1],
120 options[option_pair[0]])
122 cmd = "sudo bash netperf.sh %s" % (cmd_args)
123 LOG.debug("Executing command: %s", cmd)
124 status, stdout, stderr = self.client.execute(cmd)
127 raise RuntimeError(stderr)
129 result.update(json.loads(stdout))
131 if result['mean_latency'] == '':
132 raise RuntimeError(stdout)
135 mean_latency = float(result['mean_latency'])
136 if "sla" in self.scenario_cfg:
137 sla_max_mean_latency = int(
138 self.scenario_cfg["sla"]["mean_latency"])
140 assert mean_latency <= sla_max_mean_latency, \
141 "mean_latency %f > sla_max_mean_latency(%f); " % \
142 (mean_latency, sla_max_mean_latency)
146 '''internal test function'''
147 key_filename = pkg_resources.resource_filename("yardstick.resources",
148 "files/yardstick_key")
151 "ip": "10.229.47.137",
153 "key_filename": key_filename
156 "ip": "10.229.47.137",
158 "key_filename": key_filename,
159 "ipaddr": "10.229.47.137"
163 logger = logging.getLogger("yardstick")
164 logger.setLevel(logging.DEBUG)
167 "testname": 'TCP_STREAM'
170 args = {"options": options}
173 netperf = Netperf(args, ctx)
177 if __name__ == '__main__':