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 """Executes an iperf3 benchmark between two hosts"""
26 __scenario_type__ = "Iperf3"
28 def __init__(self, context):
29 self.context = context
30 self.user = context.get('user', 'ubuntu')
31 self.host_ipaddr = context['host']
32 self.target_ipaddr = context['target_ipaddr']
33 self.key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
34 self.setup_done = False
37 LOG.debug("setup, key %s", self.key_filename)
38 LOG.debug("host:%s, user:%s", self.host_ipaddr, self.user)
39 self.host = ssh.SSH(self.user, self.host_ipaddr,
40 key_filename=self.key_filename)
41 self.host.wait(timeout=600)
43 LOG.debug("target:%s, user:%s", self.target_ipaddr, self.user)
44 self.target = ssh.SSH(self.user, self.target_ipaddr,
45 key_filename=self.key_filename)
46 self.target.wait(timeout=600)
49 LOG.debug("Starting iperf3 server with command: %s", cmd)
50 status, _, stderr = self.target.execute(cmd)
52 raise RuntimeError(stderr)
57 status, stdout, stderr = self.target.execute("pkill iperf3")
63 """execute the benchmark"""
65 # if run by a duration runner, get the duration time and setup as arg
66 time = self.context.get('duration', None)
67 options = args['options']
69 cmd = "iperf3 -c %s --json" % (self.target_ipaddr)
75 if "nodelay" in options:
78 # these options are mutually exclusive in iperf3
81 elif "bytes" in options:
82 # number of bytes to transmit (instead of --time)
83 cmd += " --bytes %d" % options["bytes"]
84 elif "blockcount" in options:
85 cmd += " --blockcount %d" % options["blockcount"]
87 LOG.debug("Executing command: %s", cmd)
89 status, stdout, stderr = self.host.execute(cmd)
91 # error cause in json dict on stdout
92 raise RuntimeError(stdout)
94 output = json.loads(stdout)
96 # convert bits per second to bytes per second
98 int((output["end"]["sum_received"]["bits_per_second"])) / 8
101 sla_bytes_per_second = int(args["sla"]["bytes_per_second"])
102 assert bytes_per_second >= sla_bytes_per_second, \
103 "bytes_per_second %d < sla (%d)" % \
104 (bytes_per_second, sla_bytes_per_second)
110 '''internal test function'''
112 logger = logging.getLogger('yardstick')
113 logger.setLevel(logging.DEBUG)
115 key_filename = pkg_resources.resource_filename('yardstick.resources',
116 'files/yardstick_key')
118 runner_cfg['type'] = 'Duration'
119 runner_cfg['duration'] = 5
120 runner_cfg['host'] = '10.0.2.33'
121 runner_cfg['target_ipaddr'] = '10.0.2.53'
122 runner_cfg['user'] = 'ubuntu'
123 runner_cfg['output_filename'] = "/tmp/yardstick.out"
124 runner_cfg['key_filename'] = key_filename
127 scenario_args['options'] = {"bytes": 10000000000}
128 scenario_args['sla'] = \
129 {"bytes_per_second": 2900000000, "action": "monitor"}
131 from yardstick.benchmark.runners import base as base_runner
132 runner = base_runner.Runner.get(runner_cfg)
133 runner.run("Iperf3", scenario_args)
135 base_runner.Runner.release(runner)
137 if __name__ == '__main__':