Merge "Refactor remote command execution in vsperf"
[yardstick.git] / yardstick / benchmark / scenarios / networking / vsperf.py
1 # Copyright 2016 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Vsperf specific scenario definition """
15
16 from __future__ import absolute_import
17 import logging
18 import os
19 import subprocess
20 import csv
21
22 import yardstick.ssh as ssh
23 from yardstick.benchmark.scenarios import base
24
25 LOG = logging.getLogger(__name__)
26
27
28 class Vsperf(base.Scenario):
29     """Execute vsperf with defined parameters
30
31   Parameters:
32     traffic_type - to specify the type of traffic executed by traffic generator
33     the valid values are "rfc2544", "continuous", "back2back"
34         type:    string
35         default: "rfc2544"
36     frame_size - a frame size for which test should be executed;
37         Multiple frame sizes can be tested by modification of sequence runner
38         section inside TC YAML definition.
39         type:    string
40         default: "64"
41     bidirectional - speficies if traffic will be uni (False) or bi-directional
42         (True)
43         type:    string
44         default: False
45     iload - specifies frame rate
46         type:    string
47         default: 100
48     multistream - the number of simulated streams
49         type:    string
50         default: 0 (disabled)
51     stream_type - specifies network layer used for multistream simulation
52         the valid values are "L4", "L3" and "L2"
53         type:    string
54         default: "L4"
55     test_params - specifies a string with a list of vsperf configuration
56         parameters, which will be passed to the '--test-params' CLI argument;
57         Parameters should be stated in the form of 'param=value' and separated
58         by a semicolon. Please check VSPERF documentation for details about
59         available configuration parameters and their data types.
60         In case that both 'test_params' and 'conf_file' are specified,
61         then values from 'test_params' will override values defined
62         in the configuration file.
63         type:    string
64         default: NA
65     conf_file - path to the vsperf configuration file, which will be uploaded
66         to the VM;
67         In case that both 'test_params' and 'conf_file' are specified,
68         then values from 'test_params' will override values defined
69         in configuration file.
70         type:   string
71         default: NA
72     setup_script - path to the setup script, which will be executed during
73         setup and teardown phases
74         type:   string
75         default: NA
76     trafficgen_port1 - specifies device name of 1st interface connected to
77         the trafficgen
78         type:   string
79         default: NA
80     trafficgen_port2 - specifies device name of 2nd interface connected to
81         the trafficgen
82         type:   string
83         default: NA
84     external_bridge - specifies name of external bridge configured in OVS
85         type:   string
86         default: "br-ex"
87
88     """
89     __scenario_type__ = "Vsperf"
90
91     def __init__(self, scenario_cfg, context_cfg):
92         self.scenario_cfg = scenario_cfg
93         self.context_cfg = context_cfg
94         self.setup_done = False
95         self.client = None
96         self.tg_port1 = self.scenario_cfg['options'].get('trafficgen_port1',
97                                                          None)
98         self.tg_port2 = self.scenario_cfg['options'].get('trafficgen_port2',
99                                                          None)
100         self.br_ex = self.scenario_cfg['options'].get('external_bridge',
101                                                       'br-ex')
102         self.vsperf_conf = self.scenario_cfg['options'].get('conf_file', None)
103         if self.vsperf_conf:
104             self.vsperf_conf = os.path.expanduser(self.vsperf_conf)
105
106         self.setup_script = self.scenario_cfg['options'].get('setup_script',
107                                                              None)
108         if self.setup_script:
109             self.setup_script = os.path.expanduser(self.setup_script)
110
111         self.test_params = self.scenario_cfg['options'].get('test-params',
112                                                             None)
113
114     def setup(self):
115         """scenario setup"""
116         vsperf = self.context_cfg['host']
117
118         # add trafficgen interfaces to the external bridge
119         if self.tg_port1:
120             subprocess.call('sudo bash -c "ovs-vsctl add-port %s %s"' %
121                             (self.br_ex, self.tg_port1), shell=True)
122         if self.tg_port2:
123             subprocess.call('sudo bash -c "ovs-vsctl add-port %s %s"' %
124                             (self.br_ex, self.tg_port2), shell=True)
125
126         # copy vsperf conf to VM
127         self.client = ssh.SSH.from_node(vsperf, defaults={
128             "user": "ubuntu", "password": "ubuntu"
129         })
130         # traffic generation could last long
131         self.client.wait(timeout=1800)
132
133         # copy script to host
134         self.client._put_file_shell(self.vsperf_conf, '~/vsperf.conf')
135
136         # execute external setup script
137         if self.setup_script:
138             cmd = "%s setup" % (self.setup_script)
139             LOG.info("Execute setup script \"%s\"", cmd)
140             subprocess.call(cmd, shell=True)
141
142         self.setup_done = True
143
144     def run(self, result):
145         """ execute the vsperf benchmark and return test results
146             within result dictionary
147         """
148         def add_test_params(options, option, default_value):
149             """return parameter and its value as a string to be passed
150                to the VSPERF inside --test-params argument
151
152                Parameters:
153                 options - dictionary with scenario options
154                 option  - a name of option to be added to the string
155                 default_value - value to be used in case that option
156                     is not defined inside scenario options
157             """
158             if option in options:
159                 return "%s=%s" % (option, options[option])
160             elif default_value is not None:
161                 return "%s=%s" % (option, default_value)
162             else:
163                 return None
164
165         if not self.setup_done:
166             self.setup()
167
168         # remove results from previous tests
169         self.client.execute("rm -rf /tmp/results*")
170
171         # get vsperf options
172         options = self.scenario_cfg['options']
173         test_params = []
174         test_params.append(add_test_params(options, "traffic_type", "rfc2544"))
175         test_params.append(add_test_params(options, "bidirectional", "False"))
176         test_params.append(add_test_params(options, "iload", 100))
177         test_params.append(add_test_params(options, "multistream", None))
178         test_params.append(add_test_params(options, "stream_type", None))
179         if 'frame_size' in options:
180             test_params.append("%s=(%s,)" % ('TRAFFICGEN_PKT_SIZES',
181                                              options['frame_size']))
182         if 'test_params' in options:
183             test_params.append(options['test_params'])
184
185         # filter empty parameters and escape quotes and double quotes
186         test_params = [tp.replace('"', '\\"').replace("'", "\\'")
187                        for tp in test_params if tp]
188
189         # execute vsperf
190         cmd = "source ~/vsperfenv/bin/activate ; cd vswitchperf ; "
191         cmd += "./vsperf --mode trafficgen "
192         if self.vsperf_conf:
193             cmd += "--conf-file ~/vsperf.conf "
194         cmd += "--test-params=\"%s\"" % (';'.join(test_params))
195         LOG.debug("Executing command: %s", cmd)
196         self.client.run(cmd)
197
198         # get test results
199         cmd = "cat /tmp/results*/result.csv"
200         LOG.debug("Executing command: %s", cmd)
201         _, stdout, _ = self.client.execute(cmd, raise_on_error=True)
202
203         # convert result.csv to JSON format
204         reader = csv.DictReader(stdout.split('\r\n'), strict=True)
205         try:
206             result.update(next(reader))
207         except StopIteration:
208             pass
209
210         # sla check; go through all defined SLAs and check if values measured
211         # by VSPERF are higher then those defined by SLAs
212         if 'sla' in self.scenario_cfg and \
213            'metrics' in self.scenario_cfg['sla']:
214             for metric in self.scenario_cfg['sla']['metrics'].split(','):
215                 self.verify_SLA(metric in result,
216                                 '%s was not collected by VSPERF' % metric)
217                 self.verify_SLA(metric in self.scenario_cfg['sla'],
218                                 '%s is not defined in SLA' % metric)
219                 vs_res = float(result[metric])
220                 sla_res = float(self.scenario_cfg['sla'][metric])
221                 self.verify_SLA(vs_res >= sla_res,
222                                 'VSPERF_%s(%f) < SLA_%s(%f)'
223                                 % (metric, vs_res, metric, sla_res))
224
225     def teardown(self):
226         """cleanup after the test execution"""
227         # remove trafficgen interfaces from the external bridge
228         if self.tg_port1:
229             subprocess.call('sudo bash -c "ovs-vsctl del-port %s %s"' %
230                             (self.br_ex, self.tg_port1), shell=True)
231         if self.tg_port2:
232             subprocess.call('sudo bash -c "ovs-vsctl del-port %s %s"' %
233                             (self.br_ex, self.tg_port2), shell=True)
234
235         # execute external setup script
236         if self.setup_script:
237             cmd = "%s teardown" % (self.setup_script)
238             LOG.info("Execute setup script \"%s\"", cmd)
239             subprocess.call(cmd, shell=True)
240
241         self.setup_done = False