blob: 245d259bde696835eadff81cd0449ac08df0a544 [file] [log] [blame]
Chetan Gaonkercfcce782016-05-10 10:10:42 -07001#
2# Copyright 2016-present Ciena Corporation
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#
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080016from math import sqrt
17
18class Stats:
19 def __init__(self):
20 self.count = 0
21 self.start = 0
22 self.delta = 0
23 self.min = 0
24 self.max = 0
25 self.delta_squares = 0
26
Chetan Gaonkercbe79642016-03-09 17:45:58 -080027 def update(self, packets = 0, t = 0, usecs = False):
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080028 self.count += packets
Chetan Gaonkercbe79642016-03-09 17:45:58 -080029 if usecs == False:
30 t *= 1000000 ##convert to usecs
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080031 if self.start == 0:
32 self.start = t
33 self.delta += t
34 self.delta_squares += t*t
35 if self.min == 0 or t < self.min:
36 self.min = t
37 if self.max == 0 or t > self.max:
38 self.max = t
39
40 def __repr__(self):
41 if self.count == 0:
42 self.count = 1
43 mean = self.delta/self.count
44 mean_square = mean*mean
45 delta_square_mean = self.delta_squares/self.count
46 std_mean = sqrt(delta_square_mean - mean_square)
47 r = 'Avg %.3f usecs, Std deviation %.3f usecs, Min %.3f, Max %.3f for %d packets\n' %(
48 mean, std_mean, self.min, self.max, self.count)
49 return r
50