1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and other.
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 ##############################################################################
12 import yardstick.ssh as ssh
13 from yardstick.benchmark.scenarios import base
15 LOG = logging.getLogger(__name__)
18 class NetUtilization(base.Scenario):
19 """Collect network utilization statistics.
21 This scenario reads statistics from the network devices on a Linux host.
22 Network utilization statistics are read using the utility 'sar'.
24 The following values are displayed:
26 IFACE: Name of the network interface for which statistics are reported.
28 rxpck/s: Total number of packets received per second.
30 txpck/s: Total number of packets transmitted per second.
32 rxkB/s: Total number of kilobytes received per second.
34 txkB/s: Total number of kilobytes transmitted per second.
36 rxcmp/s: Number of compressed packets received per second (for cslip etc.).
38 txcmp/s: Number of compressed packets transmitted per second.
40 rxmcst/s: Number of multicast packets received per second.
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.
48 interval - Time interval to measure network utilization.
53 count - Number of times to measure network utilization.
59 __scenario_type__ = "NetUtilization"
61 NET_UTILIZATION_FIELD_SIZE = 8
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
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')
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)
80 self.setup_done = True
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)
87 raise RuntimeError("Failed executing command: ",
91 def _filtrate_result(self, raw_result):
92 """Filtrate network utilization statistics."""
98 time_marker = re.compile("^([0-9]+):([0-9]+):([0-9]+)$")
99 ampm_marker = re.compile("(AM|PM)$")
101 # Parse network utilization stats
102 for row in raw_result.split('\n'):
105 if line and re.match(time_marker, line[0]):
106 if re.match(ampm_marker, line[1]):
109 if line[0] == 'IFACE':
112 if len(fields) != NetUtilization.\
113 NET_UTILIZATION_FIELD_SIZE:
114 raise RuntimeError("network_utilization: unexpected\
118 net_interface = line[0]
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
126 for item in temp_dict:
127 if float(maximum[net_interface][item]) <\
128 float(temp_dict[item]):
129 maximum[net_interface][item] = \
132 if net_interface not in minimum:
133 minimum[net_interface] = temp_dict
135 for item in temp_dict:
136 if float(minimum[net_interface][item]) >\
137 float(temp_dict[item]):
138 minimum[net_interface][item] = \
141 raise RuntimeError("network_utilization: parse error",
144 elif line and line[0] == 'Average:':
147 if line[0] == 'IFACE':
150 if len(fields) != NetUtilization.\
151 NET_UTILIZATION_FIELD_SIZE:
152 raise RuntimeError("network_utilization average: \
153 unexpected field size", fields)
156 net_interface = line[0]
158 if values and len(values) == len(fields):
159 average[net_interface] = dict(zip(fields, values))
161 raise RuntimeError("network_utilization average: \
162 parse error", fields, line)
164 return {'network_utilization_maximun': maximum,
165 'network_utilization_minimum': minimum,
166 'network_utilization_average': average}
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)
174 cmd = "sudo sar -n DEV %d %d" % (interval, count)
176 raw_result = self._execute_command(cmd)
177 result = self._filtrate_result(raw_result)
181 def run(self, result):
182 """Read statistics."""
183 if not self.setup_done:
186 result.update(self._get_network_utilization())