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 "nodelay" in options:
105 # these options are mutually exclusive in iperf3
108 elif "bytes" in options:
109 # number of bytes to transmit (instead of --time)
110 cmd += " --bytes %d" % options["bytes"]
111 elif "blockcount" in options:
112 cmd += " --blockcount %d" % options["blockcount"]
114 LOG.debug("Executing command: %s", cmd)
116 status, stdout, stderr = self.host.execute(cmd)
118 # error cause in json dict on stdout
119 raise RuntimeError(stdout)
121 output = json.loads(stdout)
123 # convert bits per second to bytes per second
125 int((output["end"]["sum_received"]["bits_per_second"])) / 8
128 sla_bytes_per_second = int(args["sla"]["bytes_per_second"])
129 assert bytes_per_second >= sla_bytes_per_second, \
130 "bytes_per_second %d < sla (%d)" % \
131 (bytes_per_second, sla_bytes_per_second)
137 '''internal test function'''
139 logger = logging.getLogger('yardstick')
140 logger.setLevel(logging.DEBUG)
142 key_filename = pkg_resources.resource_filename('yardstick.resources',
143 'files/yardstick_key')
145 runner_cfg['type'] = 'Duration'
146 runner_cfg['duration'] = 5
147 runner_cfg['host'] = '10.0.2.33'
148 runner_cfg['target_ipaddr'] = '10.0.2.53'
149 runner_cfg['user'] = 'ubuntu'
150 runner_cfg['output_filename'] = "/tmp/yardstick.out"
151 runner_cfg['key_filename'] = key_filename
154 scenario_args['options'] = {"bytes": 10000000000}
155 scenario_args['sla'] = \
156 {"bytes_per_second": 2900000000, "action": "monitor"}
158 from yardstick.benchmark.runners import base as base_runner
159 runner = base_runner.Runner.get(runner_cfg)
160 runner.run("Iperf3", scenario_args)
162 base_runner.Runner.release(runner)
164 if __name__ == '__main__':