def test_process_timers(self): start_timestamp = time() expected_flushed_metrics_count = 2 + 5 # each timer has 5 separate metrics metrics = (Counter('user.jump', 2), Set('username', 'navdoon'), Timer('db.query', 300), Set('username', 'navdoon2'), Counter('user.jump', -1), Timer('db.query', 309), Timer('db.query', 303)) queue_ = Queue() destination = StubDestination() destination.expected_count = expected_flushed_metrics_count processor = QueueProcessor(queue_) processor.set_destinations([destination]) process_thread = Thread(target=processor.process) process_thread.start() processor.wait_until_processing(5) for metric in metrics: queue_.put(metric.to_request()) destination.wait_until_expected_count_items(5) processor.shutdown() processor.wait_until_shutdown(5) self.assertEqual(expected_flushed_metrics_count, len(destination.metrics)) metrics_dict = dict() for (name, value, timestamp) in destination.metrics: metrics_dict[name] = value self.assertGreaterEqual(timestamp, start_timestamp) self.assertEqual(metrics_dict['user.jump'], 1) self.assertEqual(metrics_dict['username'], 2) self.assertEqual(metrics_dict['db.query.count'], 3) self.assertEqual(metrics_dict['db.query.max'], 309) self.assertEqual(metrics_dict['db.query.min'], 300) self.assertEqual(metrics_dict['db.query.mean'], 304) self.assertEqual(metrics_dict['db.query.median'], 303)
def test_sample_rate_should_be_numeric(self): self.assertRaises( AssertionError, Timer, 'string_sample_rate', 1.0, 's') timer = Timer('ok', 0.1) timer.sample_rate = 0.3 self.assertEqual(timer.sample_rate, 0.3) timer.sample_rate = 2 self.assertEqual(timer.sample_rate, 2)
def test_timers_data(self): shelf = StatsShelf() self.assertEqual(dict(), shelf.timers_data()) shelf.add(Timer("query.user", 3.223)) shelf.add(Timer("query.user", 4.12)) shelf.add(Timer("api.auth", 9.78)) shelf.add(Timer("api.auth", 8.45)) expected = {"query.user": [3.223, 4.12], "api.auth": [9.78, 8.45]} self.assertEqual(expected, shelf.timers_data())
def test_to_request(self): timer = Timer('ok', 0.2) self.assertEqual(timer.to_request(), 'ok:0.2|ms') timer2 = Timer('another', 45.2) self.assertEqual(timer2.to_request(), 'another:45.2|ms') timer3 = Timer('again', 12.3, 0.8) self.assertEqual(timer3.to_request(), 'again:12.3|ms|@0.8')
def test_equality_check(self): timer = Timer('timer1', 10) with self.assertRaises(AssertionError): timer != 'I am not a Timer' different_name = Timer('timer2', 10) self.assertTrue(timer != different_name) different_time = Timer('timer1', 25) self.assertTrue(timer != different_time) different_rate = Timer('timer1', 10, 0.5) self.assertTrue(timer != different_rate) same = Timer('timer1', 10) self.assertTrue(same == timer)
def test_parse_timer_metric_from_request(self): self.assertEqual( Timer("exact", 1), parse_metric_from_request("exact:1|ms") ) self.assertEqual( Timer("db query?", 2.4), parse_metric_from_request("db query?:2.4|ms") ) self.assertEqual( Timer("db.query.with.rate", 10, 1), parse_metric_from_request("db.query.with.rate:10|ms|@1") ) self.assertEqual( Timer("db_query_float_rate", 23.5, 0.5), parse_metric_from_request("db_query_float_rate:23.5|ms|@0.5") )
def test_clear_all_metrics(self): shelf = StatsShelf() shelf.add(Set("users", "me")) shelf.add(Counter("mymetric", 3)) shelf.add(Timer("query", 4.12)) shelf.add(Gauge("cpu%", 38)) shelf.clear() self.assertEqual(dict(), shelf.counters()) self.assertEqual(dict(), shelf.sets()) self.assertEqual(dict(), shelf.timers_data()) self.assertEqual(dict(), shelf.gauges())
def test_timers(self): shelf = StatsShelf() self.assertEqual(dict(), shelf.timers()) shelf.add(Timer("query.user", 2.9)) shelf.add(Timer("query.user", 3)) shelf.add(Timer("query.user", 4.1)) shelf.add(Timer("api.auth", 8)) shelf.add(Timer("api.auth", 7.96)) shelf.add(Timer("cache.clear", 1.56)) expected = { "query.user": dict(count=3, min=2.9, max=4.1, mean=3.3333333333333335, median=3), "api.auth": dict(count=2, min=7.96, max=8, mean=7.98, median=7.98), "cache.clear": dict(count=1, min=1.56, max=1.56, mean=1.56, median=1.56), } self.assertEqual(expected, shelf.timers())
def test_millisecond_should_be_numeric(self): self.assertRaises(AssertionError, Timer, 'test', '') timer = Timer('ok', 0.3) self.assertEqual(timer.milliseconds, 0.3) timer.milliseconds = 2 self.assertEqual(timer.milliseconds, 2)
def test_millisecond_should_not_be_negative(self): self.assertRaises(AssertionError, Timer, 'test', -4.2) timer = Timer('ok', 0.0) self.assertEqual(timer.milliseconds, 0.0)
def test_default_sample_rate_is_one(self): timer = Timer('test', 0.1) self.assertEqual(timer.sample_rate, 1)
def test_constructor(self): timer = Timer('test', 5.1, 0.2) self.assertEqual(timer.name, 'test') self.assertEqual(timer.milliseconds, 5.1) self.assertEqual(timer.sample_rate, 0.2)