summaryrefslogtreecommitdiffstats
path: root/nfvbench/stats_collector.py
blob: dc750dbeef255894c7ff8ea4d6d548055a54317e (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
# Copyright 2016 Cisco Systems, Inc.  All rights reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.


import time


class StatsCollector(object):
    """Base class for all stats collector classes."""

    def __init__(self, start_time):
        self.start_time = start_time
        self.stats = []

    def get(self):
        return self.stats

    def peek(self):
        return self.stats[-1]

    @staticmethod
    def _get_drop_percentage(drop_pkts, total_pkts):
        return float(drop_pkts * 100) / total_pkts

    @staticmethod
    def _get_rx_pps(tx_pps, drop_percentage):
        return (tx_pps * (100 - drop_percentage)) / 100

    def _get_current_time_diff(self):
        return int((time.time() - self.start_time) * 1000)


class IntervalCollector(StatsCollector):
    """Collects stats while traffic is running. Frequency is specified by 'interval_sec' setting."""

    last_tx_pkts = 0
    last_rx_pkts = 0
    last_time = 0

    def __init__(self, start_time):
        StatsCollector.__init__(self, start_time)
        self.notifier = None

    def attach_notifier(self, notifier):
        self.notifier = notifier

    def add(self, stats):
        pass

    def reset(self):
        # don't reset time!
        self.last_rx_pkts = 0
        self.last_tx_pkts = 0

    def add_ndr_pdr(self, tag, stats):
        pass


class IterationCollector(StatsCollector):
    """Collects stats after traffic is stopped. Frequency is specified by 'duration_sec' setting."""

    def __init__(self, start_time):
        StatsCollector.__init__(self, start_time)

    def add(self, stats, tx_pps):
        drop_percentage = self._get_drop_percentage(stats['overall']['rx']['dropped_pkts'],
                                                    stats['overall']['tx']['total_pkts'])

        record = {
            'total_tx_pps': int(stats['total_tx_rate']),
            'tx_pps': tx_pps,
            'tx_pkts': stats['overall']['tx']['total_pkts'],
            'rx_pps': self._get_rx_pps(tx_pps, drop_percentage),
            'rx_pkts': stats['overall']['rx']['total_pkts'],
            'drop_pct': stats['overall']['rx']['dropped_pkts'],
            'drop_percentage': drop_percentage,
            'time_ms': int(time.time() * 1000)
        }

        if 'warning' in stats:
            record['warning'] = stats['warning']

        self.stats.append(record)

    def add_ndr_pdr(self, tag, rate):
        last_stats = self.peek()
        last_stats['{}_pps'.format(tag)] = rate