Exemplo n.º 1
0
    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')
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    def test_histogram_histogram(self):
        sample = UniformSample(100000)
        histogram = HistogramMetricReporter(sample)
        for i in range(1001, 11001):
            histogram.update(i)

        hist = histogram.histogram()
        self.assertEquals(sum(hist), 10000)

        total = sum(hist)
        binsize = int(total / len(hist))
        for i in hist:
            self.assertTrue(abs(i - binsize) <= 1)
Exemplo n.º 4
0
    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')
Exemplo n.º 5
0
    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()
Exemplo n.º 6
0
    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")
Exemplo n.º 7
0
    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()
Exemplo n.º 8
0
    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')
Exemplo n.º 9
0
class TimerMetricReporter(object):
    """
    A timer metric which aggregates timing durations and provides duration
    statistics, plus throughput statistics via L{MeterMetricReporter}.
    """

    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 clear(self, timestamp=None):
        """Clears all recorded durations."""
        self.histogram.clear()
        if timestamp is None:
            timestamp = self.wall_time_func()
        self.last_time = float(timestamp)

    def rate(self, timestamp):
        """The number of values seen since last clear."""
        dt = (timestamp - self.last_time)
        if dt == 0:
            return 0
        return self.histogram.count / dt

    def max(self):
        """Returns the longest recorded duration."""
        return self.histogram.max()

    def min(self):
        """Returns the shortest recorded duration."""
        return self.histogram.min()

    def mean(self):
        """Returns the arithmetic mean of all recorded durations."""
        return self.histogram.mean()

    def std_dev(self):
        """Returns the standard deviation of all recorded durations."""
        return self.histogram.std_dev()

    def getResource(self):
        """Return an http resource to represent this."""
        from txstatsd.server.httpinfo import TimerResource
        return TimerResource(self)

    def percentiles(self, *percentiles):
        """
        Returns an array of durations at the given percentiles.

        @param percentiles: One or more percentiles.
        """
        return [percentile for percentile in
                self.histogram.percentiles(*percentiles)]

    def get_values(self):
        """Returns a list of all recorded durations in the timer's sample."""
        return [value for value in self.histogram.get_values()]

    def update(self, duration):
        """Adds a recorded duration.

        @param duration: The length of the duration in seconds.
        """
        self.count += 1
        if duration >= 0:
            self.histogram.update(duration)

    def report(self, timestamp):
        # median, 75, 95, 98, 99, 99.9 percentile
        percentiles = self.percentiles(0.5, 0.75, 0.95, 0.98, 0.99, 0.999)
        metrics = []
        items = {".min": self.min(),
                 ".max": self.max(),
                 ".mean": self.mean(),
                 ".stddev": self.std_dev(),
                 ".99percentile": percentiles[4],
                 ".999percentile": percentiles[5],
                 ".count": self.count,
                 ".rate": self.rate(timestamp),
                 }
        for item, value in sorted(items.iteritems()):
            metrics.append((self.prefix + self.name + item,
                            round(value, 6), timestamp))
        self.clear(timestamp)
        return metrics
Exemplo n.º 10
0
class TimerMetricReporter(object):
    """
    A timer metric which aggregates timing durations and provides duration
    statistics, plus throughput statistics via L{MeterMetricReporter}.
    """

    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 clear(self, timestamp=None):
        """Clears all recorded durations."""
        self.histogram.clear()
        if timestamp is None:
            timestamp = self.wall_time_func()
        self.last_time = float(timestamp)

    def rate(self, timestamp):
        """The number of values seen since last clear."""
        dt = (timestamp - self.last_time)
        if dt == 0:
            return 0
        return self.histogram.count / dt

    def max(self):
        """Returns the longest recorded duration."""
        return self.histogram.max()

    def min(self):
        """Returns the shortest recorded duration."""
        return self.histogram.min()

    def mean(self):
        """Returns the arithmetic mean of all recorded durations."""
        return self.histogram.mean()

    def std_dev(self):
        """Returns the standard deviation of all recorded durations."""
        return self.histogram.std_dev()

    def getResource(self):
        """Return an http resource to represent this."""
        from txstatsd.server.httpinfo import TimerResource
        return TimerResource(self)

    def percentiles(self, *percentiles):
        """
        Returns an array of durations at the given percentiles.

        @param percentiles: One or more percentiles.
        """
        return [percentile for percentile in
            self.histogram.percentiles(*percentiles)]

    def get_values(self):
        """Returns a list of all recorded durations in the timer's sample."""
        return [value for value in self.histogram.get_values()]

    def update(self, duration):
        """Adds a recorded duration.

        @param duration: The length of the duration in seconds.
        """
        self.count += 1
        if duration >= 0:
            self.histogram.update(duration)

    def report(self, timestamp):
        # median, 75, 95, 98, 99, 99.9 percentile
        percentiles = self.percentiles(0.5, 0.75, 0.95, 0.98, 0.99, 0.999)
        metrics = []
        items = {".min": self.min(),
                 ".max": self.max(),
                 ".mean": self.mean(),
                 ".stddev": self.std_dev(),
                 ".99percentile": percentiles[4],
                 ".999percentile": percentiles[5],
                 ".count": self.count,
                 ".rate": self.rate(timestamp),
            }
        for item, value in items.iteritems():
            metrics.append((self.prefix + self.name + item,
                            round(value, 6), timestamp))
        self.clear(timestamp)
        return metrics