aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/networking/nstat.py
blob: df96dbda766af02613f15d3156f4490a0f2e1777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# 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
##############################################################################
from __future__ import print_function
from __future__ import absolute_import

import time
import logging

import yardstick.ssh as ssh
from yardstick.benchmark.scenarios import base

LOG = logging.getLogger(__name__)

PRECISION = 3


class Nstat(base.Scenario):
    """Use nstat to monitor network metrics and measure IP datagram error rate
    and etc.
    """

    __scenario_type__ = "Nstat"

    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")
        ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
        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,
                              port=ssh_port)
        self.client.wait(timeout=600)

        self.setup_done = True

    def match(self, key, field, line, results):
        """match data in the output"""
        if key in line:
            results[key] = int(line.split()[field])

    def calculate_error_rate(self, x, y):
        """calculate error rate"""
        try:
            return round(float(x) / float(y), PRECISION)
        except ZeroDivisionError:
            # If incoming Errors is non-zero, but incoming data is zero
            # consider it as 100% error rate
            if x:
                return 1
            else:
                return 0

    def process_output(self, out):
        """process output"""
        results = {}
        for line in out.splitlines():
            self.match('IpInReceives', 1, line, results)
            self.match('IpInHdrErrors', 1, line, results)
            self.match('IpInAddrErrors', 1, line, results)
            self.match('IcmpInMsgs', 1, line, results)
            self.match('IcmpInErrors', 1, line, results)
            self.match('TcpInSegs', 1, line, results)
            self.match('TcpInErrs', 1, line, results)
            self.match('UdpInDatagrams', 1, line, results)
            self.match('UdpInErrors', 1, line, results)
        results['IpErrors'] = \
            results['IpInHdrErrors'] + results['IpInAddrErrors']
        results['IP_datagram_error_rate'] = \
            self.calculate_error_rate(results['IpErrors'],
                                      results['IpInReceives'])
        results['Icmp_message_error_rate'] = \
            self.calculate_error_rate(results['IcmpInErrors'],
                                      results['IcmpInMsgs'])
        results['Tcp_segment_error_rate'] = \
            self.calculate_error_rate(results['TcpInErrs'],
                                      results['TcpInSegs'])
        results['Udp_datagram_error_rate'] = \
            self.calculate_error_rate(results['UdpInErrors'],
                                      results['UdpInDatagrams'])
        return results

    def run(self, result):
        """execute the benchmark"""

        if not self.setup_done:
            self.setup()

        options = self.scenario_cfg['options']
        duration = options.get('duration', 60)

        time.sleep(duration)

        cmd = "nstat -z"

        LOG.debug("Executing command: %s", cmd)
        status, stdout, stderr = self.client.execute(cmd)

        if status:
            raise RuntimeError(stderr)

        results = self.process_output(stdout)

        result.update(results)

        if "sla" in self.scenario_cfg:
            sla_error = ""
            for i, rate in result.items():
                if i not in self.scenario_cfg['sla']:
                    continue
                sla_rate = float(self.scenario_cfg['sla'][i])
                rate = float(rate)
                if rate > sla_rate:
                    sla_error += "%s rate %f > sla:%s_rate(%f); " % \
                        (i, rate, i, sla_rate)
            assert sla_error == "", sla_error