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/
17 import yardstick.ssh as ssh
18 from yardstick.benchmark.scenarios import base
20 LOG = logging.getLogger(__name__)
23 class Iperf(base.Scenario):
24 """Execute iperf3 between two hosts
26 By default TCP is used but UDP can also be configured.
27 For more info see http://software.es.net/iperf
30 bytes - number of bytes to transmit
31 only valid with a non duration runner, mutually exclusive with blockcount
35 udp - use UDP rather than TCP
39 nodelay - set TCP no delay, disabling Nagle's Algorithm
43 blockcount - number of blocks (packets) to transmit,
44 only valid with a non duration runner, mutually exclusive with bytes
49 __scenario_type__ = "Iperf3"
51 def __init__(self, scenario_cfg, context_cfg):
52 self.scenario_cfg = scenario_cfg
53 self.context_cfg = context_cfg
54 self.setup_done = False
57 host = self.context_cfg['host']
58 host_user = host.get('user', 'ubuntu')
59 host_ip = host.get('ip', None)
60 host_key_filename = host.get('key_filename', '~/.ssh/id_rsa')
61 target = self.context_cfg['target']
62 target_user = target.get('user', 'ubuntu')
63 target_ip = target.get('ip', None)
64 target_key_filename = target.get('key_filename', '~/.ssh/id_rsa')
66 LOG.info("user:%s, target:%s", target_user, target_ip)
67 self.target = ssh.SSH(target_user, target_ip,
68 key_filename=target_key_filename)
69 self.target.wait(timeout=600)
71 LOG.info("user:%s, host:%s", host_user, host_ip)
72 self.host = ssh.SSH(host_user, host_ip,
73 key_filename=host_key_filename)
74 self.host.wait(timeout=600)
77 LOG.debug("Starting iperf3 server with command: %s", cmd)
78 status, _, stderr = self.target.execute(cmd)
80 raise RuntimeError(stderr)
82 self.setup_done = True
87 status, stdout, stderr = self.target.execute("pkill iperf3")
92 def run(self, result):
93 """execute the benchmark"""
94 if not self.setup_done:
97 # if run by a duration runner, get the duration time and setup as arg
98 time = self.scenario_cfg["runner"].get("duration", None) \
99 if "runner" in self.scenario_cfg else None
100 options = self.scenario_cfg['options']
102 cmd = "iperf3 -c %s --json" % (self.context_cfg['target']['ipaddr'])
104 # If there are no options specified
112 if "bandwidth" in options:
113 cmd += " --bandwidth %s" % options["bandwidth"]
116 if "nodelay" in options:
119 # these options are mutually exclusive in iperf3
122 elif "bytes" in options:
123 # number of bytes to transmit (instead of --time)
124 cmd += " --bytes %d" % options["bytes"]
125 elif "blockcount" in options:
126 cmd += " --blockcount %d" % options["blockcount"]
128 LOG.debug("Executing command: %s", cmd)
130 status, stdout, stderr = self.host.execute(cmd)
132 # error cause in json dict on stdout
133 raise RuntimeError(stdout)
135 # Note: convert all ints to floats in order to avoid
136 # schema conflicts in influxdb. We probably should add
137 # a format func in the future.
138 result.update(json.loads(stdout, parse_int=float))
140 if "sla" in self.scenario_cfg:
141 sla_iperf = self.scenario_cfg["sla"]
143 sla_bytes_per_second = int(sla_iperf["bytes_per_second"])
145 # convert bits per second to bytes per second
147 int(result["end"]["sum_received"]["bits_per_second"])
148 bytes_per_second = bit_per_second / 8
149 assert bytes_per_second >= sla_bytes_per_second, \
150 "bytes_per_second %d < sla:bytes_per_second (%d); " % \
151 (bytes_per_second, sla_bytes_per_second)
153 sla_jitter = float(sla_iperf["jitter"])
155 jitter_ms = float(result["end"]["sum"]["jitter_ms"])
156 assert jitter_ms <= sla_jitter, \
157 "jitter_ms %f > sla:jitter %f; " % \
158 (jitter_ms, sla_jitter)
162 '''internal test function'''
163 key_filename = pkg_resources.resource_filename('yardstick.resources',
164 'files/yardstick_key')
167 'ip': '10.229.47.137',
169 'key_filename': key_filename
172 'ip': '10.229.47.137',
174 'key_filename': key_filename,
175 'ipaddr': '10.229.47.137',
179 logger = logging.getLogger('yardstick')
180 logger.setLevel(logging.DEBUG)
182 options = {'packetsize': 120}
183 args = {'options': options}
190 if __name__ == '__main__':