def test_populate_buckets(self): pb = metrics_pb2.MetricsData() m = metrics.DistributionMetric('test') d = distribution.Distribution( distribution.FixedWidthBucketer(10)) d.add(5) d.add(15) d.add(35) d.add(65) m._populate_value(pb, d) self.assertEquals([1, 1, -1, 1, -2, 1], pb.distribution.bucket) self.assertEquals(0, pb.distribution.underflow) self.assertEquals(0, pb.distribution.overflow) self.assertEquals(30, pb.distribution.mean) pb = metrics_pb2.MetricsData() d = distribution.Distribution( distribution.FixedWidthBucketer(10, num_finite_buckets=1)) d.add(5) d.add(15) d.add(25) m._populate_value(pb, d) self.assertEquals([1], pb.distribution.bucket) self.assertEquals(0, pb.distribution.underflow) self.assertEquals(2, pb.distribution.overflow) self.assertEquals(15, pb.distribution.mean)
def add(self, value, fields=None): with self._thread_lock: dist = self.get(fields) if dist is None: dist = distribution.Distribution(self.bucketer) dist.add(value) self._set_and_send_value(dist, fields)
def test_populate_canonical(self): pb = metrics_pb2.MetricsData() m = metrics.DistributionMetric('test') m._populate_value(pb, distribution.Distribution(distribution.GeometricBucketer())) self.assertEquals(pb.distribution.spec_type, metrics_pb2.PrecomputedDistribution.CANONICAL_POWERS_OF_10_P_0_2) m._populate_value(pb, distribution.Distribution(distribution.GeometricBucketer(2))) self.assertEquals(pb.distribution.spec_type, metrics_pb2.PrecomputedDistribution.CANONICAL_POWERS_OF_2) m._populate_value(pb, distribution.Distribution(distribution.GeometricBucketer(10))) self.assertEquals(pb.distribution.spec_type, metrics_pb2.PrecomputedDistribution.CANONICAL_POWERS_OF_10)
def test_populate_custom(self): pb = metrics_pb2.MetricsData() m = metrics.DistributionMetric('test') m._populate_value(pb, distribution.Distribution(distribution.GeometricBucketer(4))) self.assertEquals(pb.distribution.spec_type, metrics_pb2.PrecomputedDistribution.CUSTOM_PARAMETERIZED) self.assertEquals(0, pb.distribution.width) self.assertEquals(4, pb.distribution.growth_factor) self.assertEquals(100, pb.distribution.num_buckets) m._populate_value(pb, distribution.Distribution(distribution.FixedWidthBucketer(10))) self.assertEquals(pb.distribution.spec_type, metrics_pb2.PrecomputedDistribution.CUSTOM_PARAMETERIZED) self.assertEquals(10, pb.distribution.width) self.assertEquals(0, pb.distribution.growth_factor) self.assertEquals(100, pb.distribution.num_buckets)
def test_populate_buckets_last_zero(self): pb = metrics_pb2.MetricsData() m = metrics.DistributionMetric('test') d = distribution.Distribution( distribution.FixedWidthBucketer(10, num_finite_buckets=10)) d.add(5) d.add(105) m._populate_value(pb, d) self.assertEquals([1], pb.distribution.bucket) self.assertEquals(1, pb.distribution.overflow)
def test_populate_is_cumulative(self): pb = metrics_pb2.MetricsData() d = distribution.Distribution( distribution.FixedWidthBucketer(10, num_finite_buckets=10)) m = metrics.CumulativeDistributionMetric('test') m._populate_value(pb, d) self.assertTrue(pb.distribution.is_cumulative) m = metrics.NonCumulativeDistributionMetric('test2') m._populate_value(pb, d) self.assertFalse(pb.distribution.is_cumulative)
def test_populate_buckets_underflow(self): pb = metrics_pb2.MetricsData() m = metrics.DistributionMetric('test') d = distribution.Distribution( distribution.FixedWidthBucketer(10, num_finite_buckets=10)) d.add(-5) d.add(-1000000) m._populate_value(pb, d) self.assertEquals([], pb.distribution.bucket) self.assertEquals(2, pb.distribution.underflow) self.assertEquals(0, pb.distribution.overflow) self.assertEquals(-500002.5, pb.distribution.mean)
def test_overflow_bucket(self): d = distribution.Distribution( distribution.FixedWidthBucketer(width=10, num_finite_buckets=10)) d.add(100) self.assertEqual(100, d.sum) self.assertEqual(1, d.count) self.assertEqual({11: 1}, d.buckets) d.add(1000000) self.assertEqual(1000100, d.sum) self.assertEqual(2, d.count) self.assertEqual({11: 2}, d.buckets)
def test_underflow_bucket(self): d = distribution.Distribution( distribution.FixedWidthBucketer(width=10)) d.add(-1) self.assertEqual(-1, d.sum) self.assertEqual(1, d.count) self.assertEqual({0: 1}, d.buckets) d.add(-1000000) self.assertEqual(-1000001, d.sum) self.assertEqual(2, d.count) self.assertEqual({0: 2}, d.buckets)
def test_add_on_bucket_boundary(self): d = distribution.Distribution( distribution.FixedWidthBucketer(width=10)) d.add(10) self.assertEqual(10, d.sum) self.assertEqual(1, d.count) self.assertEqual({2: 1}, d.buckets) d.add(0) self.assertEqual(10, d.sum) self.assertEqual(2, d.count) self.assertEqual({1: 1, 2: 1}, d.buckets)
def test_add(self): d = distribution.Distribution(distribution.GeometricBucketer()) self.assertEqual(0, d.sum) self.assertEqual(0, d.count) self.assertEqual({}, d.buckets) d.add(1) d.add(10) d.add(100) self.assertEqual(111, d.sum) self.assertEqual(3, d.count) self.assertEqual({2: 1, 6: 1, 11: 1}, d.buckets) d.add(50) self.assertEqual(161, d.sum) self.assertEqual(4, d.count) self.assertEqual({2: 1, 6: 1, 10: 1, 11: 1}, d.buckets)
def test_set(self): d = distribution.Distribution( distribution.FixedWidthBucketer(10, num_finite_buckets=10)) d.add(1) d.add(10) d.add(100) m = metrics.CumulativeDistributionMetric('test') with self.assertRaises(TypeError): m.set(d) m = metrics.NonCumulativeDistributionMetric('test2') m.set(d) self.assertEquals(d, m.get()) with self.assertRaises(errors.MonitoringInvalidValueTypeError): m.set(1) with self.assertRaises(errors.MonitoringInvalidValueTypeError): m.set('foo')