def test_histogram_of_numbers_1_through_10000(self): sample = UniformSample(100000) histogram = HistogramMetricReporter(sample) for i in range(1, 10001): histogram.update(i) self.assertEqual(histogram.count, 10000, 'Should have a count of 10000') self.assertEqual(histogram.max(), 10000, 'Should have a max of 10000') self.assertEqual(histogram.min(), 1, 'Should have a min of 1') self.assertTrue((math.fabs(histogram.mean() - 5000.5) < 0.01), 'Should have a mean value of 5000.5') self.assertTrue((math.fabs(histogram.std_dev() - 2886.89) < 0.01), 'Should have a standard deviation of X') percentiles = histogram.percentiles(0.5, 0.75, 0.99) self.assertTrue((math.fabs(percentiles[0] - 5000.5) < 0.01), 'Should calculate percentiles') self.assertTrue((math.fabs(percentiles[1] - 7500.75) < 0.01), 'Should calculate percentiles') self.assertTrue((math.fabs(percentiles[2] - 9900.99) < 0.01), 'Should calculate percentiles') values = [i for i in range(1, 10001)] self.assertEqual(histogram.get_values(), values, 'Should have 10000 values')
def using_uniform_sample(cls, prefix=""): """ Uses a uniform sample of 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution. """ sample = UniformSample(1028) return HistogramMetricReporter(sample, prefix=prefix)
def test_histogram_histogram(self): sample = UniformSample(100000) histogram = HistogramMetricReporter(sample) for i in range(1001, 11001): histogram.update(i) hist = histogram.histogram() self.assertEqual(sum(hist), 10000) total = sum(hist) binsize = int(total / len(hist)) for i in hist: self.assertTrue(abs(i - binsize) <= 1)
def test_100_out_of_10_elements(self): population = [i for i in range(0, 10)] sample = UniformSample(100) for i in population: sample.update(i) self.assertEqual(sample.size(), 10, 'Should have 10 elements') 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 __init__(self, name, wall_time_func=time.time, prefix=""): """Construct a metric we expect to be periodically updated. @param name: Indicates what is being instrumented. @param wall_time_func: Function for obtaining wall time. @param prefix: If present, a string to prepend to the message composed when C{report} is called. """ self.name = name self.wall_time_func = wall_time_func if prefix: prefix += "." self.prefix = prefix sample = UniformSample(1028) self.histogram = HistogramMetricReporter(sample) # total number of values seen self.count = 0 self.clear()
def test_histogram_with_zero_recorded_values(self): sample = UniformSample(100) histogram = HistogramMetricReporter(sample) self.assertEqual(histogram.count, 0, 'Should have a count of 0') self.assertEqual(histogram.max(), 0, 'Should have a max of 0') self.assertEqual(histogram.min(), 0, 'Should have a min of 0') self.assertEqual(histogram.mean(), 0, 'Should have a mean of 0') self.assertEqual(histogram.std_dev(), 0, 'Should have a standard deviation of 0') percentiles = histogram.percentiles(0.5, 0.75, 0.99) self.assertTrue((math.fabs(percentiles[0] - 0) < 0.01), 'Should calculate percentiles') self.assertTrue((math.fabs(percentiles[1] - 0) < 0.01), 'Should calculate percentiles') self.assertTrue((math.fabs(percentiles[2] - 0) < 0.01), 'Should calculate percentiles') self.assertEqual(len(histogram.get_values()), 0, 'Should have no values')