-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_collector.py
83 lines (59 loc) · 2.33 KB
/
stats_collector.py
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
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