Exemple #1
0
    def testWithRescale(self):
        """Excercise rescaling."""
        # Not a good test, but at least we cover a little more of the code.
        random.seed(42)

        sample = ExponentiallyDecayingReservoir(rescale_threshold=-1)
        sample.update(random.gauss(42.0, 13.0))
        self.assertAlmostEqual(sample.mean, 40.12682571548693, places=5)
Exemple #2
0
  def testWithRescale(self):
    """Excercise rescaling."""
    # Not a good test, but at least we cover a little more of the code.
    random.seed(42)

    sample = ExponentiallyDecayingReservoir(rescale_threshold=-1)
    sample.update(random.gauss(42.0, 13.0))
    self.assertAlmostEqual(sample.mean, 40.12682571548693, places=5)
Exemple #3
0
 def __init__(self, sample=None):
     UserDict.__init__(self)
     if sample:
         self.__sample = sample
     else:
         self.__sample = ExponentiallyDecayingReservoir()
     self.__timestamp = 0
     self.percentile99 = None
     self['count'] = 0
Exemple #4
0
    def testGaussian(self):
        """Test with gaussian random numbers."""
        random.seed(42)

        sample = ExponentiallyDecayingReservoir()
        for _ in range(300):
            sample.update(random.gauss(42.0, 13.0))
        self.assertAlmostEqual(sample.mean, 41.974069434931714, places=5)
        self.assertAlmostEqual(sample.stddev, 12.982363860393766, places=5)
Exemple #5
0
  def testGaussian(self):
    """Test with gaussian random numbers."""
    random.seed(42)

    sample = ExponentiallyDecayingReservoir()
    for _ in range(300):
      sample.update(random.gauss(42.0, 13.0))
    self.assertAlmostEqual(sample.mean, 41.974069434931714, places=5)
    self.assertAlmostEqual(sample.stddev, 12.982363860393766, places=5)
Exemple #6
0
 def __init__(self, sample = None):
   UserDict.__init__(self)
   if sample:
       self.__sample = sample
   else:
       self.__sample = ExponentiallyDecayingReservoir()
   self.__timestamp = 0
   self.percentile99 = None
   self['count'] = 0
Exemple #7
0
class PmfStatDict(UserDict):
  """Ugly hack defaultdict-like thing."""

  class TimeManager(object):
    """Context manager for timing."""

    def __init__(self, container):
      self.container = container
      self.msg99 = None
      self.start = None
      self.__discard = False


    def __enter__(self):
      self.start = time.time()
      return self


    def __exit__(self, *_):
      if not self.__discard:
        latency = time.time() - self.start
        self.container.addValue(latency)

        if self.container.percentile99 is not None and latency >= self.container.percentile99:
          if self.msg99 is not None:
            logger, msg, args = self.msg99
            logger.warn(msg, *args)


    def warn99(self, logger, msg, *args):
      """If this time through the timed section of code takes longer
      than the 99th percentile time, then this will call
      logger.warn(msg, *args) at the end of the section."""
      self.msg99 = (logger, msg, args)


    def discard(self):
      """Discard this sample."""
      self.__discard = True


  def __init__(self, sample = None):
    UserDict.__init__(self)
    if sample:
        self.__sample = sample
    else:
        self.__sample = ExponentiallyDecayingReservoir()
    self.__timestamp = 0
    self.percentile99 = None
    self['count'] = 0


  def __getitem__(self, item):
    if item in self:
      return UserDict.__getitem__(self, item)
    else:
      return 0.0


  def addValue(self, value):
    """Updates the dictionary."""
    self['count'] += 1
    self.__sample.update(value)
    if time.time() > self.__timestamp + 20 and len(self.__sample) > 1:
      self.__timestamp = time.time()
      self['min'] = self.__sample.min
      self['max'] = self.__sample.max
      self['mean'] = self.__sample.mean
      self['stddev'] = self.__sample.stddev

      percentiles = self.__sample.percentiles([0.5, 0.75, 0.95, 0.98, 0.99, 0.999])
      self['median'] = percentiles[0]
      self['75percentile'] = percentiles[1]
      self['95percentile'] = percentiles[2]
      self['98percentile'] = percentiles[3]
      self['99percentile'] = percentiles[4]
      self.percentile99 = percentiles[4]
      self['999percentile'] = percentiles[5]


  def time(self):
    """Measure the time this section of code takes. For use in with statements."""
    return self.TimeManager(self)
Exemple #8
0
class PmfStatDict(UserDict):
    """Ugly hack defaultdict-like thing."""
    class TimeManager(object):
        """Context manager for timing."""
        def __init__(self, container):
            self.container = container
            self.msg99 = None
            self.start = None
            self.__discard = False

        def __enter__(self):
            self.start = time.time()
            return self

        def __exit__(self, *_):
            if not self.__discard:
                latency = time.time() - self.start
                self.container.addValue(latency)

                if self.container.percentile99 is not None and latency >= self.container.percentile99:
                    if self.msg99 is not None:
                        logger, msg, args = self.msg99
                        logger.warn(msg, *args)

        def warn99(self, logger, msg, *args):
            """If this time through the timed section of code takes longer
      than the 99th percentile time, then this will call
      logger.warn(msg, *args) at the end of the section."""
            self.msg99 = (logger, msg, args)

        def discard(self):
            """Discard this sample."""
            self.__discard = True

    def __init__(self, sample=None):
        UserDict.__init__(self)
        if sample:
            self.__sample = sample
        else:
            self.__sample = ExponentiallyDecayingReservoir()
        self.__timestamp = 0
        self.percentile99 = None
        self['count'] = 0

    def __getitem__(self, item):
        if item in self:
            return UserDict.__getitem__(self, item)
        else:
            return 0.0

    def addValue(self, value):
        """Updates the dictionary."""
        self['count'] += 1
        self.__sample.update(value)
        if time.time() > self.__timestamp + 20 and len(self.__sample) > 1:
            self.__timestamp = time.time()
            self['min'] = self.__sample.min
            self['max'] = self.__sample.max
            self['mean'] = self.__sample.mean
            self['stddev'] = self.__sample.stddev

            percentiles = self.__sample.percentiles(
                [0.5, 0.75, 0.95, 0.98, 0.99, 0.999])
            self['median'] = percentiles[0]
            self['75percentile'] = percentiles[1]
            self['95percentile'] = percentiles[2]
            self['98percentile'] = percentiles[3]
            self['99percentile'] = percentiles[4]
            self.percentile99 = percentiles[4]
            self['999percentile'] = percentiles[5]

    def time(self):
        """Measure the time this section of code takes. For use in with statements."""
        return self.TimeManager(self)