Пример #1
0
    def test_bad_construction(self):
        mm = MetricManager()

        class NotMetricReporter(object):
            pass

        mr = [NotMetricReporter()]
        with self.assertRaises(GoblinMetricsException):
            mm.setup_reporters(mr)
Пример #2
0
    def test_multiple_reporter_constuction(self):
        mm = MetricManager()
        r1 = [MetricsRegistry(), MetricsRegistry()]
        mr1 = BaseMetricsReporter(registry=r1)
        r2 = [MetricsRegistry(), MetricsRegistry()]
        mr2 = BaseMetricsReporter(registry=r2)
        mm.setup_reporters([mr1, mr2])

        self.assertIsInstance(mm.metric_reporters, (tuple, list))
        self.assertEqual(len(mm.metric_reporters), 2)
        self.assertIsInstance(mm.metric_reporters[0], BaseMetricsReporter)
        self.assertIsInstance(mm.metric_reporters[1], BaseMetricsReporter)
        self.assertEqual(mm.metric_reporters, [mr1, mr2])
Пример #3
0
    def test_multiple_decorators(self):
        mm = MetricManager()
        mr = BaseMetricsReporter()
        mm.setup_reporters(mr)
        mm.start()

        @mm.meter_calls
        @mm.count_calls
        @mm.hist_calls
        @mm.time_calls
        def somefunc(i):
            return i

        self.assertEqual(somefunc(1), 1)
        self.assertEqual(somefunc(1, context='test'), 1)

        # check metrics collected
        for reporter in mm.metric_reporters:
            timestamp, metrics = reporter.get_metrics()
            self.assertIsInstance(timestamp, integer_types)
            self.assertIsInstance(metrics, dict)
            self.assertEqual(len(metrics), 3)

            # context aware
            self.assertIn('test.meter', metrics)
            self.assertIsInstance(metrics['test.meter'], dict)
            self.assertGreater(metrics['test.meter']['mean_rate'], 100)

            # normal one
            self.assertIn('somefunc.calls', metrics)
            self.assertIsInstance(metrics['somefunc.calls'], dict)
            self.assertEqual(metrics['somefunc.calls']['count'], 2)

        mm.stop()
Пример #4
0
 def test_start_stop_mechanism(self):
     mm = MetricManager()
     mr = BaseMetricsReporter()
     mm.setup_reporters(mr)
     mm.start()
     for mr in mm.metric_reporters:
         self.assertTrue(mr.task.running)
     mm.stop()
     for mr in mm.metric_reporters:
         self.assertFalse(mr.task.running)
Пример #5
0
    def test_metric_getter(self):
        mm = MetricManager()
        mr = BaseMetricsReporter()
        mm.setup_reporters(mr)
        mm.start()
        # increment counters
        for counter in mm.counters('test'):
            counter.inc()

        timestamp, metrics = mr.get_metrics()
        self.assertIsInstance(timestamp, integer_types)
        self.assertIsInstance(metrics, dict)
        self.assertEqual(len(metrics), 1)
        self.assertIn('test', metrics)
        self.assertIsInstance(metrics['test'], dict)
        self.assertEqual(metrics['test']['count'], 1)
        mm.stop()
Пример #6
0
    def test_default_construction(self):
        mm = MetricManager()
        self.assertEqual(mm.metric_reporters, [])
        with self.assertRaises(GoblinMetricsException):
            mm.setup_reporters(None)
        mr = BaseMetricsReporter()
        mm.setup_reporters(mr)

        self.assertIsInstance(mm.metric_reporters, (tuple, list))
        self.assertEqual(len(mm.metric_reporters), 1)
        self.assertIsInstance(mm.metric_reporters[0], BaseMetricsReporter)
        self.assertEqual(mm.metric_reporters, [mr])
Пример #7
0
    def test_get_all_counters(self):
        mm = MetricManager()
        mr = BaseMetricsReporter()
        mm.setup_reporters(mr)

        # test default implementation
        for counter in mm.counters():
            self.assertIsNotNone(get_method_self(counter))
            self.assertIsInstance(get_method_self(counter), MetricsRegistry)

        # test context aware
        for counter in mm.counters('test'):
            from pyformance.meters.counter import Counter
            self.assertIsInstance(counter, Counter)
Пример #8
0
    def test_get_all_histograms(self):
        mm = MetricManager()
        mr = BaseMetricsReporter()
        mm.setup_reporters(mr)

        # test default implementation
        for hist in mm.histograms():
            self.assertIsNotNone(get_method_self(hist))
            self.assertIsInstance(get_method_self(hist), MetricsRegistry)

        # test context aware
        for hist in mm.histograms('test'):
            from pyformance.meters.histogram import Histogram
            self.assertIsInstance(hist, Histogram)
Пример #9
0
    def test_timer_decorator(self):
        mm = MetricManager()
        mr = BaseMetricsReporter()
        mm.setup_reporters(mr)
        mm.start()

        @mm.time_calls
        def somefunc(i):
            return i

        @mm.time_calls
        def badfunc(i):
            raise Exception("test exception")

        self.assertEqual(somefunc(1), 1)
        self.assertEqual(somefunc(1, context='test'), 1)
        with self.assertRaises(Exception):
            badfunc(1)

        # check metrics collected
        for reporter in mm.metric_reporters:
            timestamp, metrics = reporter.get_metrics()

            self.assertIsInstance(timestamp, integer_types)
            self.assertIsInstance(metrics, dict)
            self.assertEqual(len(metrics), 5)

            # context aware
            self.assertIn('test.timer', metrics)
            self.assertIsInstance(metrics['test.timer'], dict)
            self.assertLess(metrics['test.timer']['max'], 0.01)

            # normal one
            self.assertIn('somefunc.timer', metrics)
            self.assertIsInstance(metrics['somefunc.timer'], dict)
            self.assertLess(metrics['somefunc.timer']['max'], 0.01)

        mm.stop()