Merge "Add a new runner to test end-to-end fast data path"
[yardstick.git] / yardstick / benchmark / scenarios / networking / nstat.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
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
11
12 import time
13 import logging
14
15 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
17
18 LOG = logging.getLogger(__name__)
19
20 PRECISION = 3
21
22
23 class Nstat(base.Scenario):
24     """Use nstat to monitor network metrics and measure IP datagram error rate
25     and etc.
26     """
27
28     __scenario_type__ = "Nstat"
29
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
35
36     def setup(self):
37         """scenario setup"""
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")
43
44         LOG.info("user:%s, host:%s", user, ip)
45         self.client = ssh.SSH(user, ip, key_filename=key_filename,
46                               port=ssh_port)
47         self.client.wait(timeout=600)
48
49         self.setup_done = True
50
51     def match(self, key, field, line, results):
52         """match data in the output"""
53         if key in line:
54             results[key] = int(line.split()[field])
55
56     def calculate_error_rate(self, x, y):
57         """calculate error rate"""
58         try:
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
63             if x:
64                 return 1
65             else:
66                 return 0
67
68     def process_output(self, out):
69         """process output"""
70         results = {}
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'],
91                                       results['TcpInSegs'])
92         results['Udp_datagram_error_rate'] = \
93             self.calculate_error_rate(results['UdpInErrors'],
94                                       results['UdpInDatagrams'])
95         return results
96
97     def run(self, result):
98         """execute the benchmark"""
99
100         if not self.setup_done:
101             self.setup()
102
103         options = self.scenario_cfg['options']
104         duration = options.get('duration', 60)
105
106         time.sleep(duration)
107
108         cmd = "nstat -z"
109
110         LOG.debug("Executing command: %s", cmd)
111         status, stdout, stderr = self.client.execute(cmd)
112
113         if status:
114             raise RuntimeError(stderr)
115
116         results = self.process_output(stdout)
117
118         result.update(results)
119
120         if "sla" in self.scenario_cfg:
121             sla_error = ""
122             for i, rate in result.items():
123                 if i not in self.scenario_cfg['sla']:
124                     continue
125                 sla_rate = float(self.scenario_cfg['sla'][i])
126                 rate = float(rate)
127                 if rate > sla_rate:
128                     sla_error += "%s rate %f > sla:%s_rate(%f); " % \
129                         (i, rate, i, sla_rate)
130             assert sla_error == "", sla_error