Merge "yardstick offline support"
[yardstick.git] / yardstick / benchmark / scenarios / networking / netutilization.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and other.
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 from __future__ import absolute_import
10 import logging
11 import re
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15 from six.moves import zip
16
17 LOG = logging.getLogger(__name__)
18
19
20 class NetUtilization(base.Scenario):
21     """Collect network utilization statistics.
22
23     This scenario reads statistics from the network devices on a Linux host.
24     Network utilization statistics are read using the utility 'sar'.
25
26     The following values are displayed:
27
28     IFACE: Name of the network interface for which statistics are reported.
29
30     rxpck/s: Total number of packets received per second.
31
32     txpck/s: Total number of packets transmitted per second.
33
34     rxkB/s: Total number of kilobytes received per second.
35
36     txkB/s: Total number of kilobytes transmitted per second.
37
38     rxcmp/s: Number of compressed packets received per second (for cslip etc.).
39
40     txcmp/s: Number of compressed packets transmitted per second.
41
42     rxmcst/s: Number of multicast packets received per second.
43
44     %ifutil: Utilization percentage of the network interface. For half-duplex
45     interfaces, utilization is calculated using the sum of rxkB/s and txkB/s
46     as a percentage of the interface speed. For full-duplex, this is the
47     greater  of rxkB/S or txkB/s.
48
49     Parameters
50         interval - Time interval to measure network utilization.
51             type:       [int]
52             unit:       seconds
53             default:    1
54
55         count - Number of times to measure network utilization.
56             type:       [int]
57             unit:       N/A
58             default:    1
59     """
60
61     __scenario_type__ = "NetUtilization"
62
63     NET_UTILIZATION_FIELD_SIZE = 8
64
65     def __init__(self, scenario_cfg, context_cfg):
66         """Scenario construction."""
67         self.scenario_cfg = scenario_cfg
68         self.context_cfg = context_cfg
69         self.setup_done = False
70
71     def setup(self):
72         """Scenario setup."""
73         host = self.context_cfg['host']
74         user = host.get('user', 'ubuntu')
75         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
76         ip = host.get('ip', None)
77         key_filename = host.get('key_filename', '~/.ssh/id_rsa')
78
79         LOG.info("user:%s, host:%s", user, ip)
80         self.client = ssh.SSH(user, ip, key_filename=key_filename,
81                               port=ssh_port)
82         self.client.wait(timeout=600)
83
84         self.setup_done = True
85
86     def _execute_command(self, cmd):
87         """Execute a command on target."""
88         LOG.info("Executing: %s", cmd)
89         status, stdout, stderr = self.client.execute(cmd)
90         if status:
91             raise RuntimeError("Failed executing command: ",
92                                cmd, stderr)
93         return stdout
94
95     def _filtrate_result(self, raw_result):
96         """Filtrate network utilization statistics."""
97         fields = []
98         maximum = {}
99         minimum = {}
100         average = {}
101
102         time_marker = re.compile("^([0-9]+):([0-9]+):([0-9]+)$")
103
104         # Parse network utilization stats
105         for row in raw_result.splitlines():
106             line = row.split()
107
108             if line and re.match(time_marker, line[0]):
109
110                 try:
111                     index = line.index('IFACE')
112                 except ValueError:
113                     del line[:index]
114                     net_interface = line[0]
115                     values = line[1:]
116
117                     if values and len(values) == len(fields):
118                         temp_dict = dict(zip(fields, values))
119                         if net_interface not in maximum:
120                             maximum[net_interface] = temp_dict
121                         else:
122                             for item in temp_dict:
123                                 if float(maximum[net_interface][item]) <\
124                                    float(temp_dict[item]):
125                                     maximum[net_interface][item] = \
126                                         temp_dict[item]
127
128                         if net_interface not in minimum:
129                             minimum[net_interface] = temp_dict
130                         else:
131                             for item in temp_dict:
132                                 if float(minimum[net_interface][item]) >\
133                                    float(temp_dict[item]):
134                                     minimum[net_interface][item] = \
135                                         temp_dict[item]
136                     else:
137                         raise RuntimeError("network_utilization: parse error",
138                                            fields, line)
139                 else:
140                     del line[:index]
141                     fields = line[1:]
142                     if len(fields) != NetUtilization.\
143                             NET_UTILIZATION_FIELD_SIZE:
144                         raise RuntimeError("network_utilization: unexpected\
145                                            field size", fields)
146
147             elif line and line[0] == 'Average:':
148                 del line[:1]
149
150                 if line[0] == 'IFACE':
151                     # header fields
152                     fields = line[1:]
153                     if len(fields) != NetUtilization.\
154                             NET_UTILIZATION_FIELD_SIZE:
155                         raise RuntimeError("network_utilization average: \
156                                            unexpected field size", fields)
157                 else:
158                     # value fields
159                     net_interface = line[0]
160                     values = line[1:]
161                     if values and len(values) == len(fields):
162                         average[net_interface] = dict(
163                             zip(fields, values))
164                     else:
165                         raise RuntimeError("network_utilization average: \
166                                            parse error", fields, line)
167
168         return {'network_utilization_maximun': maximum,
169                 'network_utilization_minimum': minimum,
170                 'network_utilization_average': average}
171
172     def _get_network_utilization(self):
173         """Get network utilization statistics using sar."""
174         options = self.scenario_cfg["options"]
175         interval = options.get('interval', 1)
176         count = options.get('count', 1)
177
178         cmd = "sudo sar -n DEV %d %d" % (interval, count)
179
180         raw_result = self._execute_command(cmd)
181         result = self._filtrate_result(raw_result)
182
183         return result
184
185     def run(self, result):
186         """Read statistics."""
187         if not self.setup_done:
188             self.setup()
189
190         result.update(self._get_network_utilization())