Merge "add unit test for ping"
[yardstick.git] / yardstick / benchmark / scenarios / networking / iperf3.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
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 ##############################################################################
9
10 # iperf3 scenario
11 # iperf3 homepage at: http://software.es.net/iperf/
12
13 import logging
14 import json
15 import pkg_resources
16
17 import yardstick.ssh as ssh
18 from yardstick.benchmark.scenarios import base
19
20 LOG = logging.getLogger(__name__)
21 LOG.setLevel(logging.DEBUG)
22
23
24 class Iperf(base.Scenario):
25     """Executes an iperf3 benchmark between two hosts"""
26     __scenario_type__ = "Iperf3"
27
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
35
36     def setup(self):
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)
42
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)
47
48         cmd = "iperf3 -s -D"
49         LOG.debug("Starting iperf3 server with command: %s", cmd)
50         status, _, stderr = self.target.execute(cmd)
51         if status:
52             raise RuntimeError(stderr)
53
54     def teardown(self):
55         LOG.debug("teardown")
56         self.host.close()
57         status, stdout, stderr = self.target.execute("pkill iperf3")
58         if status:
59             LOG.warn(stderr)
60         self.target.close()
61
62     def run(self, args):
63         """execute the benchmark"""
64
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']
68
69         cmd = "iperf3 -c %s --json" % (self.target_ipaddr)
70
71         if "udp" in options:
72             cmd += " --udp"
73         else:
74             # tcp obviously
75             if "nodelay" in options:
76                 cmd += " --nodelay"
77
78         # these options are mutually exclusive in iperf3
79         if time:
80             cmd += " %d" % time
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"]
86
87         LOG.debug("Executing command: %s", cmd)
88
89         status, stdout, stderr = self.host.execute(cmd)
90         if status:
91             # error cause in json dict on stdout
92             raise RuntimeError(stdout)
93
94         output = json.loads(stdout)
95
96         # convert bits per second to bytes per second
97         bytes_per_second = \
98             int((output["end"]["sum_received"]["bits_per_second"])) / 8
99
100         if "sla" in args:
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)
105
106         return output
107
108
109 def _test():
110     '''internal test function'''
111
112     logger = logging.getLogger('yardstick')
113     logger.setLevel(logging.DEBUG)
114
115     key_filename = pkg_resources.resource_filename('yardstick.resources',
116                                                    'files/yardstick_key')
117     runner_cfg = {}
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
125
126     scenario_args = {}
127     scenario_args['options'] = {"bytes": 10000000000}
128     scenario_args['sla'] = \
129         {"bytes_per_second": 2900000000, "action": "monitor"}
130
131     from yardstick.benchmark.runners import base as base_runner
132     runner = base_runner.Runner.get(runner_cfg)
133     runner.run("Iperf3", scenario_args)
134     runner.join()
135     base_runner.Runner.release(runner)
136
137 if __name__ == '__main__':
138     _test()