Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 1 | from math import sqrt |
| 2 | |
| 3 | class 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 Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 12 | def update(self, packets = 0, t = 0, usecs = False): |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 13 | self.count += packets |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 14 | if usecs == False: |
| 15 | t *= 1000000 ##convert to usecs |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 16 | 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 | |