blob: 3d8e40ce659db91ba5a3a285fca54044a98d57b4 [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
12 def update(self, packets = 0, t = 0):
13 self.count += packets
14 t *= 1000000 ##convert to usecs
15 if self.start == 0:
16 self.start = t
17 self.delta += t
18 self.delta_squares += t*t
19 if self.min == 0 or t < self.min:
20 self.min = t
21 if self.max == 0 or t > self.max:
22 self.max = t
23
24 def __repr__(self):
25 if self.count == 0:
26 self.count = 1
27 mean = self.delta/self.count
28 mean_square = mean*mean
29 delta_square_mean = self.delta_squares/self.count
30 std_mean = sqrt(delta_square_mean - mean_square)
31 r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %(
32 mean, std_mean, self.min, self.max, self.count)
33 return r
34