blob: dd5efe3e4d6456c74c9a06d4e9168c2b09268ab4 [file] [log] [blame]
Matteo Scandolo48d3d2d2017-08-08 13:05:27 -07001
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 Gaonkercfcce782016-05-10 10:10:42 -070017#
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 Gaonkereb2b24b2016-03-01 14:04:45 -080032from math import sqrt
33
34class 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 Gaonkercbe79642016-03-09 17:45:58 -080043 def update(self, packets = 0, t = 0, usecs = False):
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080044 self.count += packets
Chetan Gaonkercbe79642016-03-09 17:45:58 -080045 if usecs == False:
46 t *= 1000000 ##convert to usecs
Chetan Gaonkereb2b24b2016-03-01 14:04:45 -080047 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