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_ssh_port = host.get('ssh_port', ssh.DEFAULT_PORT)
66 host_ip = host.get('ip', None)
67 host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
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 target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
74 # netserver start automatically during the vm boot
75 LOG.info("user:%s, target:%s", target_user, target_ip)
76 self.server = ssh.SSH(target_user, target_ip,
77 key_filename=target_key_filename,
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 key_filename=host_key_filename,
85 self.client.wait(timeout=600)
88 self.client.run("cat > ~/netperf.sh",
89 stdin=open(self.target_script, "rb"))
91 self.setup_done = True
93 def run(self, result):
94 """execute the benchmark"""
96 if not self.setup_done:
100 ipaddr = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
101 options = self.scenario_cfg['options']
102 testname = options.get("testname", 'TCP_STREAM')
103 duration_time = self.scenario_cfg["runner"].get("duration", None) \
104 if "runner" in self.scenario_cfg else None
105 arithmetic_time = options.get("duration", None)
107 testlen = duration_time
108 elif arithmetic_time:
109 testlen = arithmetic_time
113 cmd_args = "-H %s -l %s -t %s" % (ipaddr, testlen, testname)
115 # get test specific options
116 default_args = "-O 'THROUGHPUT,THROUGHPUT_UNITS,MEAN_LATENCY'"
117 cmd_args += " -- %s" % default_args
118 option_pair_list = [("send_msg_size", "-m"),
119 ("recv_msg_size", "-M"),
120 ("req_rsp_size", "-r")]
121 for option_pair in option_pair_list:
122 if option_pair[0] in options:
123 cmd_args += " %s %s" % (option_pair[1],
124 options[option_pair[0]])
126 cmd = "sudo bash netperf.sh %s" % (cmd_args)
127 LOG.debug("Executing command: %s", cmd)
128 status, stdout, stderr = self.client.execute(cmd)
131 raise RuntimeError(stderr)
133 result.update(json.loads(stdout))
135 if result['mean_latency'] == '':
136 raise RuntimeError(stdout)
139 mean_latency = float(result['mean_latency'])
140 if "sla" in self.scenario_cfg:
141 sla_max_mean_latency = int(
142 self.scenario_cfg["sla"]["mean_latency"])
144 assert mean_latency <= sla_max_mean_latency, \
145 "mean_latency %f > sla_max_mean_latency(%f); " % \
146 (mean_latency, sla_max_mean_latency)
150 '''internal test function'''
151 key_filename = pkg_resources.resource_filename("yardstick.resources",
152 "files/yardstick_key")
155 "ip": "10.229.47.137",
157 "key_filename": key_filename
160 "ip": "10.229.47.137",
162 "key_filename": key_filename,
163 "ipaddr": "10.229.47.137"
167 logger = logging.getLogger("yardstick")
168 logger.setLevel(logging.DEBUG)
171 "testname": 'TCP_STREAM'
174 args = {"options": options}
177 netperf = Netperf(args, ctx)
182 if __name__ == '__main__':