blob: ef3707d01455963fd29eda4752f24756452ffade [file] [log] [blame]
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -08001from math import sqrt
2
3class Stats:
4 def __init__(self):
5 self.count = 0
6 self.start = 0
7 self.delta = 0
8 self.min = 0
9 self.max = 0
10 self.delta_squares = 0
11
Chetan Gaonkercbe79642016-03-09 17:45:58 -080012 def update(self, packets = 0, t = 0, usecs = False):
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080013 self.count += packets
Chetan Gaonkercbe79642016-03-09 17:45:58 -080014 if usecs == False:
15 t *= 1000000 ##convert to usecs
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080016 if self.start == 0:
17 self.start = t
18 self.delta += t
19 self.delta_squares += t*t
20 if self.min == 0 or t < self.min:
21 self.min = t
22 if self.max == 0 or t > self.max:
23 self.max = t
24
25 def __repr__(self):
26 if self.count == 0:
27 self.count = 1
28 mean = self.delta/self.count
29 mean_square = mean*mean
30 delta_square_mean = self.delta_squares/self.count
31 std_mean = sqrt(delta_square_mean - mean_square)
32 r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %(
33 mean, std_mean, self.min, self.max, self.count)
34 return r
35