Matteo Scandolo | 48d3d2d | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 17 | # |
| 18 | # Copyright 2016-present Ciena Corporation |
| 19 | # |
| 20 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 21 | # you may not use this file except in compliance with the License. |
| 22 | # You may obtain a copy of the License at |
| 23 | # |
| 24 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | # |
| 26 | # Unless required by applicable law or agreed to in writing, software |
| 27 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 28 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 | # See the License for the specific language governing permissions and |
| 30 | # limitations under the License. |
| 31 | # |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 32 | from math import sqrt |
| 33 | |
| 34 | class Stats: |
| 35 | def __init__(self): |
| 36 | self.count = 0 |
| 37 | self.start = 0 |
| 38 | self.delta = 0 |
| 39 | self.min = 0 |
| 40 | self.max = 0 |
| 41 | self.delta_squares = 0 |
| 42 | |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 43 | def update(self, packets = 0, t = 0, usecs = False): |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 44 | self.count += packets |
Chetan Gaonker | cbe7964 | 2016-03-09 17:45:58 -0800 | [diff] [blame] | 45 | if usecs == False: |
| 46 | t *= 1000000 ##convert to usecs |
Chetan Gaonker | eb2b24b | 2016-03-01 14:04:45 -0800 | [diff] [blame] | 47 | if self.start == 0: |
| 48 | self.start = t |
| 49 | self.delta += t |
| 50 | self.delta_squares += t*t |
| 51 | if self.min == 0 or t < self.min: |
| 52 | self.min = t |
| 53 | if self.max == 0 or t > self.max: |
| 54 | self.max = t |
| 55 | |
| 56 | def __repr__(self): |
| 57 | if self.count == 0: |
| 58 | self.count = 1 |
| 59 | mean = self.delta/self.count |
| 60 | mean_square = mean*mean |
| 61 | delta_square_mean = self.delta_squares/self.count |
| 62 | std_mean = sqrt(delta_square_mean - mean_square) |
| 63 | r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %( |
| 64 | mean, std_mean, self.min, self.max, self.count) |
| 65 | return r |
| 66 | |