blob: 560c89e1cedddc4ad91845e099ae344996793514 [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002#
3# Copyright 2016-present Ciena Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080017from math import sqrt
18
19class Stats:
20 def __init__(self):
21 self.count = 0
22 self.start = 0
23 self.delta = 0
24 self.min = 0
25 self.max = 0
26 self.delta_squares = 0
27
Chetan Gaonkercbe79642016-03-09 17:45:58 -080028 def update(self, packets = 0, t = 0, usecs = False):
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080029 self.count += packets
Chetan Gaonkercbe79642016-03-09 17:45:58 -080030 if usecs == False:
31 t *= 1000000 ##convert to usecs
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080032 if self.start == 0:
33 self.start = t
34 self.delta += t
35 self.delta_squares += t*t
36 if self.min == 0 or t < self.min:
37 self.min = t
38 if self.max == 0 or t > self.max:
39 self.max = t
40
41 def __repr__(self):
42 if self.count == 0:
43 self.count = 1
44 mean = self.delta/self.count
45 mean_square = mean*mean
46 delta_square_mean = self.delta_squares/self.count
47 std_mean = sqrt(delta_square_mean - mean_square)
48 r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %(
49 mean, std_mean, self.min, self.max, self.count)
50 return r
51