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__)
21 LOG.setLevel(logging.DEBUG)
24 class Iperf(base.Scenario):
25 """Execute iperf3 between two hosts
27 By default TCP is used but UDP can also be configured.
28 For more info see http://software.es.net/iperf
31 bytes - number of bytes to transmit
32 only valid with a non duration runner, mutually exclusive with blockcount
36 udp - use UDP rather than TCP
40 nodelay - set TCP no delay, disabling Nagle's Algorithm
44 blockcount - number of blocks (packets) to transmit,
45 only valid with a non duration runner, mutually exclusive with bytes
50 __scenario_type__ = "Iperf3"
52 def __init__(self, context):
53 self.context = context
54 self.user = context.get('user', 'ubuntu')
55 self.host_ipaddr = context['host']
56 self.target_ipaddr = context['target']
57 self.key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
58 self.setup_done = False
61 LOG.debug("setup, key %s", self.key_filename)
62 LOG.debug("host:%s, user:%s", self.host_ipaddr, self.user)
63 self.host = ssh.SSH(self.user, self.host_ipaddr,
64 key_filename=self.key_filename)
65 self.host.wait(timeout=600)
67 LOG.debug("target:%s, user:%s", self.target_ipaddr, self.user)
68 self.target = ssh.SSH(self.user, self.target_ipaddr,
69 key_filename=self.key_filename)
70 self.target.wait(timeout=600)
73 LOG.debug("Starting iperf3 server with command: %s", cmd)
74 status, _, stderr = self.target.execute(cmd)
76 raise RuntimeError(stderr)
81 status, stdout, stderr = self.target.execute("pkill iperf3")
87 """execute the benchmark"""
89 # if run by a duration runner, get the duration time and setup as arg
90 time = self.context.get('duration', None)
91 options = args['options']
93 cmd = "iperf3 -c %s --json" % (self.target_ipaddr)
95 # If there are no options specified
103 if "nodelay" in options:
106 # these options are mutually exclusive in iperf3
109 elif "bytes" in options:
110 # number of bytes to transmit (instead of --time)
111 cmd += " --bytes %d" % options["bytes"]
112 elif "blockcount" in options:
113 cmd += " --blockcount %d" % options["blockcount"]
115 LOG.debug("Executing command: %s", cmd)
117 status, stdout, stderr = self.host.execute(cmd)
119 # error cause in json dict on stdout
120 raise RuntimeError(stdout)
122 output = json.loads(stdout)
124 # convert bits per second to bytes per second
126 int((output["end"]["sum_received"]["bits_per_second"])) / 8
129 sla_bytes_per_second = int(args["sla"]["bytes_per_second"])
130 assert bytes_per_second >= sla_bytes_per_second, \
131 "bytes_per_second %d < sla (%d)" % \
132 (bytes_per_second, sla_bytes_per_second)
138 '''internal test function'''
140 logger = logging.getLogger('yardstick')
141 logger.setLevel(logging.DEBUG)
143 key_filename = pkg_resources.resource_filename('yardstick.resources',
144 'files/yardstick_key')
146 runner_cfg['type'] = 'Duration'
147 runner_cfg['duration'] = 5
148 runner_cfg['host'] = '10.0.2.33'
149 runner_cfg['target_ipaddr'] = '10.0.2.53'
150 runner_cfg['user'] = 'ubuntu'
151 runner_cfg['output_filename'] = "/tmp/yardstick.out"
152 runner_cfg['key_filename'] = key_filename
155 scenario_args['options'] = {"bytes": 10000000000}
156 scenario_args['sla'] = \
157 {"bytes_per_second": 2900000000, "action": "monitor"}
159 from yardstick.benchmark.runners import base as base_runner
160 runner = base_runner.Runner.get(runner_cfg)
161 runner.run("Iperf3", scenario_args)
163 base_runner.Runner.release(runner)
165 if __name__ == '__main__':