Remove setting logger level to debug in scenarios
[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
22
23 class Iperf(base.Scenario):
24     """Execute iperf3 between two hosts
25
26 By default TCP is used but UDP can also be configured.
27 For more info see http://software.es.net/iperf
28
29   Parameters
30     bytes - number of bytes to transmit
31       only valid with a non duration runner, mutually exclusive with blockcount
32         type:    int
33         unit:    bytes
34         default: 56
35     udp - use UDP rather than TCP
36         type:    bool
37         unit:    na
38         default: false
39     nodelay - set TCP no delay, disabling Nagle's Algorithm
40         type:    bool
41         unit:    na
42         default: false
43     blockcount - number of blocks (packets) to transmit,
44       only valid with a non duration runner, mutually exclusive with bytes
45         type:    int
46         unit:    bytes
47         default: -
48     """
49     __scenario_type__ = "Iperf3"
50
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
58
59     def setup(self):
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)
65
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)
70
71         cmd = "iperf3 -s -D"
72         LOG.debug("Starting iperf3 server with command: %s", cmd)
73         status, _, stderr = self.target.execute(cmd)
74         if status:
75             raise RuntimeError(stderr)
76
77     def teardown(self):
78         LOG.debug("teardown")
79         self.host.close()
80         status, stdout, stderr = self.target.execute("pkill iperf3")
81         if status:
82             LOG.warn(stderr)
83         self.target.close()
84
85     def run(self, args):
86         """execute the benchmark"""
87
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']
91
92         cmd = "iperf3 -c %s --json" % (self.target_ipaddr)
93
94         # If there are no options specified
95         if not options:
96             options = ""
97
98         if "udp" in options:
99             cmd += " --udp"
100         else:
101             # tcp obviously
102             if "nodelay" in options:
103                 cmd += " --nodelay"
104
105         # these options are mutually exclusive in iperf3
106         if time:
107             cmd += " %d" % time
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"]
113
114         LOG.debug("Executing command: %s", cmd)
115
116         status, stdout, stderr = self.host.execute(cmd)
117         if status:
118             # error cause in json dict on stdout
119             raise RuntimeError(stdout)
120
121         output = json.loads(stdout)
122
123         # convert bits per second to bytes per second
124         bytes_per_second = \
125             int((output["end"]["sum_received"]["bits_per_second"])) / 8
126
127         if "sla" in args:
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)
132
133         return output
134
135
136 def _test():
137     '''internal test function'''
138
139     logger = logging.getLogger('yardstick')
140     logger.setLevel(logging.DEBUG)
141
142     key_filename = pkg_resources.resource_filename('yardstick.resources',
143                                                    'files/yardstick_key')
144     runner_cfg = {}
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
152
153     scenario_args = {}
154     scenario_args['options'] = {"bytes": 10000000000}
155     scenario_args['sla'] = \
156         {"bytes_per_second": 2900000000, "action": "monitor"}
157
158     from yardstick.benchmark.runners import base as base_runner
159     runner = base_runner.Runner.get(runner_cfg)
160     runner.run("Iperf3", scenario_args)
161     runner.join()
162     base_runner.Runner.release(runner)
163
164 if __name__ == '__main__':
165     _test()