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, context):
52 self.context = context
53 self.user = context.get('user', 'ubuntu')
54 self.host_ipaddr = context['host']
55 self.target_ipaddr = context['target']
56 self.key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
57 self.setup_done = False
60 LOG.debug("setup, key %s", self.key_filename)
61 LOG.info("host:%s, user:%s", self.host_ipaddr, self.user)
62 self.host = ssh.SSH(self.user, self.host_ipaddr,
63 key_filename=self.key_filename)
64 self.host.wait(timeout=600)
66 LOG.info("target:%s, user:%s", self.target_ipaddr, self.user)
67 self.target = ssh.SSH(self.user, self.target_ipaddr,
68 key_filename=self.key_filename)
69 self.target.wait(timeout=600)
72 LOG.debug("Starting iperf3 server with command: %s", cmd)
73 status, _, stderr = self.target.execute(cmd)
75 raise RuntimeError(stderr)
80 status, stdout, stderr = self.target.execute("pkill iperf3")
86 """execute the benchmark"""
88 # if run by a duration runner, get the duration time and setup as arg
89 time = self.context.get('duration', None)
90 options = args['options']
92 cmd = "iperf3 -c %s --json" % (self.target_ipaddr)
94 # If there are no options specified
102 if "bandwidth" in options:
103 cmd += " --bandwidth %s" % options["bandwidth"]
106 if "nodelay" in options:
109 # these options are mutually exclusive in iperf3
112 elif "bytes" in options:
113 # number of bytes to transmit (instead of --time)
114 cmd += " --bytes %d" % options["bytes"]
115 elif "blockcount" in options:
116 cmd += " --blockcount %d" % options["blockcount"]
118 LOG.debug("Executing command: %s", cmd)
120 status, stdout, stderr = self.host.execute(cmd)
122 # error cause in json dict on stdout
123 raise RuntimeError(stdout)
125 output = json.loads(stdout)
128 sla_iperf = args["sla"]
130 sla_bytes_per_second = int(sla_iperf["bytes_per_second"])
132 # convert bits per second to bytes per second
134 int(output["end"]["sum_received"]["bits_per_second"])
135 bytes_per_second = bit_per_second / 8
136 assert bytes_per_second >= sla_bytes_per_second, \
137 "bytes_per_second %d < sla:bytes_per_second (%d)" % \
138 (bytes_per_second, sla_bytes_per_second)
140 sla_jitter = float(sla_iperf["jitter"])
142 jitter_ms = float(output["end"]["sum"]["jitter_ms"])
143 assert jitter_ms <= sla_jitter, \
144 "jitter_ms %f > sla:jitter %f" % \
145 (jitter_ms, sla_jitter)
151 '''internal test function'''
153 logger = logging.getLogger('yardstick')
154 logger.setLevel(logging.DEBUG)
156 key_filename = pkg_resources.resource_filename('yardstick.resources',
157 'files/yardstick_key')
159 runner_cfg['type'] = 'Duration'
160 runner_cfg['duration'] = 5
161 runner_cfg['host'] = '10.0.2.33'
162 runner_cfg['target_ipaddr'] = '10.0.2.53'
163 runner_cfg['user'] = 'ubuntu'
164 runner_cfg['output_filename'] = "/tmp/yardstick.out"
165 runner_cfg['key_filename'] = key_filename
168 scenario_args['options'] = {"bytes": 10000000000}
169 scenario_args['sla'] = \
170 {"bytes_per_second": 2900000000, "action": "monitor"}
172 from yardstick.benchmark.runners import base as base_runner
173 runner = base_runner.Runner.get(runner_cfg)
174 runner.run("Iperf3", scenario_args)
176 base_runner.Runner.release(runner)
178 if __name__ == '__main__':