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"]
40 self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
41 self.client.wait(timeout=600)
43 self.setup_done = True
45 def match(self, key, field, line, results):
46 """match data in the output"""
48 results[key] = int(line.split()[field])
50 def calculate_error_rate(self, x, y):
51 """calculate error rate"""
53 return round(float(x) / float(y), PRECISION)
54 except ZeroDivisionError:
55 # If incoming Errors is non-zero, but incoming data is zero
56 # consider it as 100% error rate
62 def process_output(self, out):
65 for line in out.splitlines():
66 self.match('IpInReceives', 1, line, results)
67 self.match('IpInHdrErrors', 1, line, results)
68 self.match('IpInAddrErrors', 1, line, results)
69 self.match('IcmpInMsgs', 1, line, results)
70 self.match('IcmpInErrors', 1, line, results)
71 self.match('TcpInSegs', 1, line, results)
72 self.match('TcpInErrs', 1, line, results)
73 self.match('UdpInDatagrams', 1, line, results)
74 self.match('UdpInErrors', 1, line, results)
75 results['IpErrors'] = \
76 results['IpInHdrErrors'] + results['IpInAddrErrors']
77 results['IP_datagram_error_rate'] = \
78 self.calculate_error_rate(results['IpErrors'],
79 results['IpInReceives'])
80 results['Icmp_message_error_rate'] = \
81 self.calculate_error_rate(results['IcmpInErrors'],
82 results['IcmpInMsgs'])
83 results['Tcp_segment_error_rate'] = \
84 self.calculate_error_rate(results['TcpInErrs'],
86 results['Udp_datagram_error_rate'] = \
87 self.calculate_error_rate(results['UdpInErrors'],
88 results['UdpInDatagrams'])
91 def run(self, result):
92 """execute the benchmark"""
94 if not self.setup_done:
97 options = self.scenario_cfg['options']
98 duration = options.get('duration', 60)
104 LOG.debug("Executing command: %s", cmd)
105 status, stdout, stderr = self.client.execute(cmd)
108 raise RuntimeError(stderr)
110 results = self.process_output(stdout)
112 result.update(results)
114 if "sla" in self.scenario_cfg:
116 for i, rate in result.items():
117 if i not in self.scenario_cfg['sla']:
119 sla_rate = float(self.scenario_cfg['sla'][i])
122 sla_error += "%s rate %f > sla:%s_rate(%f); " % \
123 (i, rate, i, sla_rate)
124 assert sla_error == "", sla_error