1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
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 print_function
10 from __future__ import absolute_import
15 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
18 LOG = logging.getLogger(__name__)
23 class Nstat(base.Scenario):
24 """Use nstat to monitor network metrics and measure IP datagram error rate
28 __scenario_type__ = "Nstat"
30 def __init__(self, scenario_cfg, context_cfg):
31 """Scenario construction"""
32 self.scenario_cfg = scenario_cfg
33 self.context_cfg = context_cfg
34 self.setup_done = False
38 host = self.context_cfg["host"]
39 user = host.get("user", "ubuntu")
40 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
41 ip = host.get("ip", None)
42 key_filename = host.get('key_filename', "~/.ssh/id_rsa")
44 LOG.info("user:%s, host:%s", user, ip)
45 self.client = ssh.SSH(user, ip, key_filename=key_filename,
47 self.client.wait(timeout=600)
49 self.setup_done = True
51 def match(self, key, field, line, results):
52 """match data in the output"""
54 results[key] = int(line.split()[field])
56 def calculate_error_rate(self, x, y):
57 """calculate error rate"""
59 return round(float(x) / float(y), PRECISION)
60 except ZeroDivisionError:
61 # If incoming Errors is non-zero, but incoming data is zero
62 # consider it as 100% error rate
68 def process_output(self, out):
71 for line in out.splitlines():
72 self.match('IpInReceives', 1, line, results)
73 self.match('IpInHdrErrors', 1, line, results)
74 self.match('IpInAddrErrors', 1, line, results)
75 self.match('IcmpInMsgs', 1, line, results)
76 self.match('IcmpInErrors', 1, line, results)
77 self.match('TcpInSegs', 1, line, results)
78 self.match('TcpInErrs', 1, line, results)
79 self.match('UdpInDatagrams', 1, line, results)
80 self.match('UdpInErrors', 1, line, results)
81 results['IpErrors'] = \
82 results['IpInHdrErrors'] + results['IpInAddrErrors']
83 results['IP_datagram_error_rate'] = \
84 self.calculate_error_rate(results['IpErrors'],
85 results['IpInReceives'])
86 results['Icmp_message_error_rate'] = \
87 self.calculate_error_rate(results['IcmpInErrors'],
88 results['IcmpInMsgs'])
89 results['Tcp_segment_error_rate'] = \
90 self.calculate_error_rate(results['TcpInErrs'],
92 results['Udp_datagram_error_rate'] = \
93 self.calculate_error_rate(results['UdpInErrors'],
94 results['UdpInDatagrams'])
97 def run(self, result):
98 """execute the benchmark"""
100 if not self.setup_done:
103 options = self.scenario_cfg['options']
104 duration = options.get('duration', 60)
110 LOG.debug("Executing command: %s", cmd)
111 status, stdout, stderr = self.client.execute(cmd)
114 raise RuntimeError(stderr)
116 results = self.process_output(stdout)
118 result.update(results)
120 if "sla" in self.scenario_cfg:
122 for i, rate in result.items():
123 if i not in self.scenario_cfg['sla']:
125 sla_rate = float(self.scenario_cfg['sla'][i])
128 sla_error += "%s rate %f > sla:%s_rate(%f); " % \
129 (i, rate, i, sla_rate)
130 assert sla_error == "", sla_error