def test_ewma_overflow(self):
        """Long pauses on metric input should not overflow weight."""
        _time = [10000]

        def wtime():
            return _time[0]

        sample = ExponentiallyDecayingSample(100, 0.99, wall_time=wtime)
        for i in xrange(100):
            sample.update(random.normalvariate(0, 10))
            _time[0] += 10000

        self.assertEqual(sample.size(), 100)
        self.assertEqual(len(sample.get_values()), 100,
                         'Should have 100 elements')
 def using_exponentially_decaying_sample(cls, prefix=""):
     """
     Uses an exponentially decaying sample of 1028 elements, which offers
     a 99.9% confidence level with a 5% margin of error assuming a normal
     distribution, and an alpha factor of 0.015, which heavily biases
     the sample to the past 5 minutes of measurements.
     """
     sample = ExponentiallyDecayingSample(1028, 0.015)
     return HistogramMetricReporter(sample, prefix=prefix)
    def test_ewma_sample_load(self):

        _time = [10000]

        def wtime():
            return _time[0]

        sample = ExponentiallyDecayingSample(100, 0.99, wall_time=wtime)
        sample.RESCALE_THRESHOLD = 100
        sample.clear()
        for i in xrange(10000000):
            sample.update(random.normalvariate(0, 10))
            _time[0] += 1

        self.assertEqual(sample.size(), 100)
        self.assertEqual(len(sample.get_values()), 100,
                         'Should have 100 elements')
    def test_100_out_of_10_elements(self):
        population = [i for i in range(0, 10)]
        sample = ExponentiallyDecayingSample(100, 0.99)
        for i in population:
            sample.update(i)

        self.assertEqual(sample.size(), 10)
        self.assertEqual(len(sample.get_values()), 10,
                         'Should have 10 elements')
        self.assertEqual(
            len(set(sample.get_values()).difference(set(population))), 0,
            'Should only have elements from the population')
    def test_ewma_sample_load(self):

        _time = [10000]

        def wtime():
            return _time[0]

        sample = ExponentiallyDecayingSample(100, 0.99, wall_time=wtime)
        sample.RESCALE_THRESHOLD = 100
        sample.clear()
        for i in xrange(10000000):
            sample.update(random.normalvariate(0, 10))
            _time[0] += 1

        self.assertEqual(sample.size(), 100)
        self.assertEqual(len(sample.get_values()), 100,
                         'Should have 100 elements')
    def test_100_out_of_10_elements(self):
        population = [i for i in range(0, 10)]
        sample = ExponentiallyDecayingSample(100, 0.99)
        for i in population:
            sample.update(i)

        self.assertEqual(sample.size(), 10)
        self.assertEqual(len(sample.get_values()), 10,
                         'Should have 10 elements')
        self.assertEqual(
            len(set(sample.get_values()).difference(set(population))), 0,
            'Should only have elements from the population')
    def test_ewma_overflow(self):
        """Long pauses on metric input should not overflow weight."""
        _time = [10000]

        def wtime():
            return _time[0]

        sample = ExponentiallyDecayingSample(100, 0.99, wall_time=wtime)
        for i in xrange(100):
            sample.update(random.normalvariate(0, 10))
            _time[0] += 10000

        self.assertEqual(sample.size(), 100)
        self.assertEqual(len(sample.get_values()), 100,
                         'Should have 100 elements')