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