Package Products :: Package ZenStatus :: Module CollectionStatistic |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenStatus.CollectionStatistic

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2011, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  __doc__ = """CollectionStatistic
    12  Class that calculates common stats for a collected value.
    13  """
    14
    15  from math import fsum, sqrt, pow
    16  import logging
    17  log = logging.getLogger("zen.CollectionStatistic")
    18
    19 -class CollectionStatistic(object):
    20      """
    21      Class that calculates common stats for a collected value.
    22      """
    23 -    def __init__(self):
    24          self.reset()
    25
    26 -    def reset(self):
    27          self.start = 0
    28          self.sent = 0
    29          self.rcvCount = 0
    30          self.loss = 0
    31
    32          self.results = []
    33          self.rtt = 0
    34          self.rtt_max = 0.0
    35          self.rtt_min = 0.0
    36          self.rtt_avg = 0.0
    37          self.rtt_stddev = 0.0
    38          self.rtt_losspct = 0.0
    39
    40 -    def logRequest(self):
    41          self.sent += 1
    42          raise NotImplementedError
    43
    44 -    def logResponse(self, val):
    45          self.results.append(val)
    46          self.rcvCount += 1
    47          n = len(self.results)
    48          if n == 0:
    49              return
    50
    51          elif n == 1:
    52              self.rtt_avg = self.rtt
    53              self.rtt_max = self.rtt
    54              self.rtt_min = self.rtt
    55              self.rtt_stddev = 0.0
    56              return
    57
    58          total = fsum(self.results)
    59          self.rtt_avg = total / n
    60          self.rtt_stddev = self.stdDev(self.rtt_avg)
    61
    62          self.rtt_min = min(self.results)
    63          self.rtt_max = max(self.results)
    64
    65          self.rtt_losspct = ((self.sent - self.loss) / self.sent) * 100
    66
    67 -    def stdDev(self, avg):
    68          """
    69          Calculate the sample standard deviation.
    70          """
    71          n = len(self.results)
    72          if n < 1:
    73              return 0
    74          # sum of squared differences from the average
    75          total = fsum( map(lambda x: pow(x - avg, 2), self.results) )
    76          # Bessel's correction uses n - 1 rather than n
    77          return sqrt( total / (n - 1) )
    78