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