Merge "DRAFT: remove apexlake"
[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
40         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
41         self.client.wait(timeout=600)
42
43         self.setup_done = True
44
45     def match(self, key, field, line, results):
46         """match data in the output"""
47         if key in line:
48             results[key] = int(line.split()[field])
49
50     def calculate_error_rate(self, x, y):
51         """calculate error rate"""
52         try:
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
57             if x:
58                 return 1
59             else:
60                 return 0
61
62     def process_output(self, out):
63         """process output"""
64         results = {}
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'],
85                                       results['TcpInSegs'])
86         results['Udp_datagram_error_rate'] = \
87             self.calculate_error_rate(results['UdpInErrors'],
88                                       results['UdpInDatagrams'])
89         return results
90
91     def run(self, result):
92         """execute the benchmark"""
93
94         if not self.setup_done:
95             self.setup()
96
97         options = self.scenario_cfg['options']
98         duration = options.get('duration', 60)
99
100         time.sleep(duration)
101
102         cmd = "nstat -z"
103
104         LOG.debug("Executing command: %s", cmd)
105         status, stdout, stderr = self.client.execute(cmd)
106
107         if status:
108             raise RuntimeError(stderr)
109
110         results = self.process_output(stdout)
111
112         result.update(results)
113
114         if "sla" in self.scenario_cfg:
115             sla_error = ""
116             for i, rate in result.items():
117                 if i not in self.scenario_cfg['sla']:
118                     continue
119                 sla_rate = float(self.scenario_cfg['sla'][i])
120                 rate = float(rate)
121                 if rate > sla_rate:
122                     sla_error += "%s rate %f > sla:%s_rate(%f); " % \
123                         (i, rate, i, sla_rate)
124             assert sla_error == "", sla_error