1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB 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 ##############################################################################
11 # iperf3 homepage at: http://software.es.net/iperf/
13 from __future__ import absolute_import
14 from __future__ import print_function
19 from oslo_serialization import jsonutils
21 import yardstick.ssh as ssh
22 from yardstick.benchmark.scenarios import base
24 LOG = logging.getLogger(__name__)
27 class Iperf(base.Scenario):
28 """Execute iperf3 between two hosts
30 By default TCP is used but UDP can also be configured.
31 For more info see http://software.es.net/iperf
34 bytes - number of bytes to transmit
35 only valid with a non duration runner, mutually exclusive with blockcount
39 udp - use UDP rather than TCP
43 nodelay - set TCP no delay, disabling Nagle's Algorithm
47 blockcount - number of blocks (packets) to transmit,
48 only valid with a non duration runner, mutually exclusive with bytes
53 __scenario_type__ = "Iperf3"
55 def __init__(self, scenario_cfg, context_cfg):
56 self.scenario_cfg = scenario_cfg
57 self.context_cfg = context_cfg
58 self.setup_done = False
61 host = self.context_cfg['host']
62 host_user = host.get('user', 'ubuntu')
63 host_ssh_port = host.get('ssh_port', ssh.DEFAULT_PORT)
64 host_ip = host.get('ip', None)
65 host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
66 target = self.context_cfg['target']
67 target_user = target.get('user', 'ubuntu')
68 target_ssh_port = target.get('ssh_port', ssh.DEFAULT_PORT)
69 target_ip = target.get('ip', None)
70 target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
72 LOG.info("user:%s, target:%s", target_user, target_ip)
73 self.target = ssh.SSH(target_user, target_ip,
74 key_filename=target_key_filename,
76 self.target.wait(timeout=600)
78 LOG.info("user:%s, host:%s", host_user, host_ip)
79 self.host = ssh.SSH(host_user, host_ip,
80 key_filename=host_key_filename, port=host_ssh_port)
81 self.host.wait(timeout=600)
84 LOG.debug("Starting iperf3 server with command: %s", cmd)
85 status, _, stderr = self.target.execute(cmd)
87 raise RuntimeError(stderr)
89 self.setup_done = True
94 status, stdout, stderr = self.target.execute("pkill iperf3")
99 def run(self, result):
100 """execute the benchmark"""
101 if not self.setup_done:
104 # if run by a duration runner, get the duration time and setup as arg
105 time = self.scenario_cfg["runner"].get("duration", None) \
106 if "runner" in self.scenario_cfg else None
107 options = self.scenario_cfg['options']
109 cmd = "iperf3 -c %s --json" % (self.context_cfg['target']['ipaddr'])
111 # If there are no options specified
119 if "bandwidth" in options:
120 cmd += " --bandwidth %s" % options["bandwidth"]
123 if "nodelay" in options:
126 # these options are mutually exclusive in iperf3
129 elif "bytes" in options:
130 # number of bytes to transmit (instead of --time)
131 cmd += " --bytes %d" % options["bytes"]
132 elif "blockcount" in options:
133 cmd += " --blockcount %d" % options["blockcount"]
135 LOG.debug("Executing command: %s", cmd)
137 status, stdout, stderr = self.host.execute(cmd)
139 # error cause in json dict on stdout
140 raise RuntimeError(stdout)
142 # Note: convert all ints to floats in order to avoid
143 # schema conflicts in influxdb. We probably should add
144 # a format func in the future.
146 jsonutils.loads(stdout, parse_int=float))
148 if "sla" in self.scenario_cfg:
149 sla_iperf = self.scenario_cfg["sla"]
151 sla_bytes_per_second = int(sla_iperf["bytes_per_second"])
153 # convert bits per second to bytes per second
155 int(result["end"]["sum_received"]["bits_per_second"])
156 bytes_per_second = bit_per_second / 8
157 assert bytes_per_second >= sla_bytes_per_second, \
158 "bytes_per_second %d < sla:bytes_per_second (%d); " % \
159 (bytes_per_second, sla_bytes_per_second)
161 sla_jitter = float(sla_iperf["jitter"])
163 jitter_ms = float(result["end"]["sum"]["jitter_ms"])
164 assert jitter_ms <= sla_jitter, \
165 "jitter_ms %f > sla:jitter %f; " % \
166 (jitter_ms, sla_jitter)
170 '''internal test function'''
171 key_filename = pkg_resources.resource_filename('yardstick.resources',
172 'files/yardstick_key')
175 'ip': '10.229.47.137',
177 'key_filename': key_filename
180 'ip': '10.229.47.137',
182 'key_filename': key_filename,
183 'ipaddr': '10.229.47.137',
187 logger = logging.getLogger('yardstick')
188 logger.setLevel(logging.DEBUG)
190 options = {'packetsize': 120}
191 args = {'options': options}
199 if __name__ == '__main__':