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 ##############################################################################
9 from __future__ import absolute_import
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15 from six.moves import zip
17 LOG = logging.getLogger(__name__)
20 class NetUtilization(base.Scenario):
21 """Collect network utilization statistics.
23 This scenario reads statistics from the network devices on a Linux host.
24 Network utilization statistics are read using the utility 'sar'.
26 The following values are displayed:
28 IFACE: Name of the network interface for which statistics are reported.
30 rxpck/s: Total number of packets received per second.
32 txpck/s: Total number of packets transmitted per second.
34 rxkB/s: Total number of kilobytes received per second.
36 txkB/s: Total number of kilobytes transmitted per second.
38 rxcmp/s: Number of compressed packets received per second (for cslip etc.).
40 txcmp/s: Number of compressed packets transmitted per second.
42 rxmcst/s: Number of multicast packets received per second.
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.
50 interval - Time interval to measure network utilization.
55 count - Number of times to measure network utilization.
61 __scenario_type__ = "NetUtilization"
63 NET_UTILIZATION_FIELD_SIZE = 8
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
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')
79 LOG.info("user:%s, host:%s", user, ip)
80 self.client = ssh.SSH(user, ip, key_filename=key_filename,
82 self.client.wait(timeout=600)
84 self.setup_done = True
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)
91 raise RuntimeError("Failed executing command: ",
95 def _filtrate_result(self, raw_result):
96 """Filtrate network utilization statistics."""
102 time_marker = re.compile("^([0-9]+):([0-9]+):([0-9]+)$")
103 ampm_marker = re.compile("(AM|PM)$")
105 # Parse network utilization stats
106 for row in raw_result.split('\n'):
109 if line and re.match(time_marker, line[0]):
110 if re.match(ampm_marker, line[1]):
113 if line[0] == 'IFACE':
116 if len(fields) != NetUtilization.\
117 NET_UTILIZATION_FIELD_SIZE:
118 raise RuntimeError("network_utilization: unexpected\
122 net_interface = line[0]
125 if values and len(values) == len(fields):
126 temp_dict = dict(list(zip(fields, values)))
127 if net_interface not in maximum:
128 maximum[net_interface] = temp_dict
130 for item in temp_dict:
131 if float(maximum[net_interface][item]) <\
132 float(temp_dict[item]):
133 maximum[net_interface][item] = \
136 if net_interface not in minimum:
137 minimum[net_interface] = temp_dict
139 for item in temp_dict:
140 if float(minimum[net_interface][item]) >\
141 float(temp_dict[item]):
142 minimum[net_interface][item] = \
145 raise RuntimeError("network_utilization: parse error",
148 elif line and line[0] == 'Average:':
151 if line[0] == 'IFACE':
154 if len(fields) != NetUtilization.\
155 NET_UTILIZATION_FIELD_SIZE:
156 raise RuntimeError("network_utilization average: \
157 unexpected field size", fields)
160 net_interface = line[0]
162 if values and len(values) == len(fields):
163 average[net_interface] = dict(
164 list(zip(fields, values)))
166 raise RuntimeError("network_utilization average: \
167 parse error", fields, line)
169 return {'network_utilization_maximun': maximum,
170 'network_utilization_minimum': minimum,
171 'network_utilization_average': average}
173 def _get_network_utilization(self):
174 """Get network utilization statistics using sar."""
175 options = self.scenario_cfg["options"]
176 interval = options.get('interval', 1)
177 count = options.get('count', 1)
179 cmd = "sudo sar -n DEV %d %d" % (interval, count)
181 raw_result = self._execute_command(cmd)
182 result = self._filtrate_result(raw_result)
186 def run(self, result):
187 """Read statistics."""
188 if not self.setup_done:
191 result.update(self._get_network_utilization())