From 54c77207b0340c89eb3476bd42ba9f9980c8224d Mon Sep 17 00:00:00 2001 From: JingLu5 Date: Tue, 19 Jul 2016 12:15:20 +0800 Subject: Add Network Utilization Scenario This scenario reads network interface utilization stats and data sent/receive rate using "sar -n". Change-Id: I9c69f03c017bc2f8a5d87a4de286af147e8a086a Signed-off-by: JingLu5 --- .../scenarios/networking/netutilization.py | 186 +++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 yardstick/benchmark/scenarios/networking/netutilization.py (limited to 'yardstick/benchmark') diff --git a/yardstick/benchmark/scenarios/networking/netutilization.py b/yardstick/benchmark/scenarios/networking/netutilization.py new file mode 100644 index 000000000..ea43e6077 --- /dev/null +++ b/yardstick/benchmark/scenarios/networking/netutilization.py @@ -0,0 +1,186 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and other. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import logging +import re + +import yardstick.ssh as ssh +from yardstick.benchmark.scenarios import base + +LOG = logging.getLogger(__name__) + + +class NetUtilization(base.Scenario): + """Collect network utilization statistics. + + This scenario reads statistics from the network devices on a Linux host. + Network utilization statistics are read using the utility 'sar'. + + The following values are displayed: + + IFACE: Name of the network interface for which statistics are reported. + + rxpck/s: Total number of packets received per second. + + txpck/s: Total number of packets transmitted per second. + + rxkB/s: Total number of kilobytes received per second. + + txkB/s: Total number of kilobytes transmitted per second. + + rxcmp/s: Number of compressed packets received per second (for cslip etc.). + + txcmp/s: Number of compressed packets transmitted per second. + + rxmcst/s: Number of multicast packets received per second. + + %ifutil: Utilization percentage of the network interface. For half-duplex + interfaces, utilization is calculated using the sum of rxkB/s and txkB/s + as a percentage of the interface speed. For full-duplex, this is the + greater of rxkB/S or txkB/s. + + Parameters + interval - Time interval to measure network utilization. + type: [int] + unit: seconds + default: 1 + + count - Number of times to measure network utilization. + type: [int] + unit: N/A + default: 1 + """ + + __scenario_type__ = "NetUtilization" + + NET_UTILIZATION_FIELD_SIZE = 8 + + def __init__(self, scenario_cfg, context_cfg): + """Scenario construction.""" + self.scenario_cfg = scenario_cfg + self.context_cfg = context_cfg + self.setup_done = False + + def setup(self): + """Scenario setup.""" + host = self.context_cfg['host'] + user = host.get('user', 'ubuntu') + ip = host.get('ip', None) + key_filename = host.get('key_filename', '~/.ssh/id_rsa') + + LOG.info("user:%s, host:%s", user, ip) + self.client = ssh.SSH(user, ip, key_filename=key_filename) + self.client.wait(timeout=600) + + self.setup_done = True + + def _execute_command(self, cmd): + """Execute a command on target.""" + LOG.info("Executing: %s" % cmd) + status, stdout, stderr = self.client.execute(cmd) + if status: + raise RuntimeError("Failed executing command: ", + cmd, stderr) + return stdout + + def _filtrate_result(self, raw_result): + """Filtrate network utilization statistics.""" + fields = [] + maximum = {} + minimum = {} + average = {} + + time_marker = re.compile("^([0-9]+):([0-9]+):([0-9]+)$") + ampm_marker = re.compile("(AM|PM)$") + + # Parse network utilization stats + for row in raw_result.split('\n'): + line = row.split() + + if line and re.match(time_marker, line[0]): + if re.match(ampm_marker, line[1]): + del line[:2] + + if line[0] == 'IFACE': + # header fields + fields = line[1:] + if len(fields) != NetUtilization.\ + NET_UTILIZATION_FIELD_SIZE: + raise RuntimeError("network_utilization: unexpected\ + field size", fields) + else: + # value fields + net_interface = line[0] + values = line[1:] + + if values and len(values) == len(fields): + temp_dict = dict(zip(fields, values)) + if net_interface not in maximum: + maximum[net_interface] = temp_dict + else: + for item in temp_dict: + if float(maximum[net_interface][item]) <\ + float(temp_dict[item]): + maximum[net_interface][item] = \ + temp_dict[item] + + if net_interface not in minimum: + minimum[net_interface] = temp_dict + else: + for item in temp_dict: + if float(minimum[net_interface][item]) >\ + float(temp_dict[item]): + minimum[net_interface][item] = \ + temp_dict[item] + else: + raise RuntimeError("network_utilization: parse error", + fields, line) + + elif line and line[0] == 'Average:': + del line[:1] + + if line[0] == 'IFACE': + # header fields + fields = line[1:] + if len(fields) != NetUtilization.\ + NET_UTILIZATION_FIELD_SIZE: + raise RuntimeError("network_utilization average: \ + unexpected field size", fields) + else: + # value fields + net_interface = line[0] + values = line[1:] + if values and len(values) == len(fields): + average[net_interface] = dict(zip(fields, values)) + else: + raise RuntimeError("network_utilization average: \ + parse error", fields, line) + + return {'network_utilization_maximun': maximum, + 'network_utilization_minimum': minimum, + 'network_utilization_average': average} + + def _get_network_utilization(self): + """Get network utilization statistics using sar.""" + options = self.scenario_cfg["options"] + interval = options.get('interval', 1) + count = options.get('count', 1) + + cmd = "sudo sar -n DEV %d %d" % (interval, count) + + raw_result = self._execute_command(cmd) + result = self._filtrate_result(raw_result) + + return result + + def run(self, result): + """Read statistics.""" + if not self.setup_done: + self.setup() + + result.update(self._get_network_utilization()) -- cgit 1.2.3-korg