def test_clear(self): client = BatchClient("localhost", batch_size=20) client._socket = self.mock_socket client.increment("first") client.decrement("second") client.timing("db.query", 1) client.clear() client.flush() self.assertEqual(self.mock_sendto.call_count, 0)
def test_sending_batch_metrics(self): start = datetime.now() start_timestamp = time() client = BatchClient("localhost", self.__class__.port) client.increment("1.test", 8) client.increment("2.login") client.timing("3.query", 9600) client.gauge("4.memory", 102600) client.gauge_delta("5.memory", 2560) client.gauge_delta("6.memory", -1280) client.set("7.ip", "127.0.0.2") client.flush() expected = [ "1.test:8|c", "2.login:1|c", "3.query:9600|ms", "4.memory:102600|g", "5.memory:+2560|g", "6.memory:-1280|g", "7.ip:127.0.0.2|s", ] self.assert_server_received_expected_requests(expected) self.__class__.server.requests.clear() client.timing_since("1.query", start_timestamp) client.timing_since("2.other_query", start) client.flush() chronometer = client.chronometer() chronometer.time_callable("3.sleepy", sleep, 1, (0.02, )) @chronometer.wrap("4.wait_a_sec") def wait_a_sec(): sleep(0.01) wait_a_sec() with client.stopwatch("5.my_with_block"): sleep(0.02) client.flush() expected_patterns = [ "1.query:[1-9]\d{0,4}\|ms", "2.other_query:[1-9]\d{0,4}\|ms", "3.sleepy:[1-9]\d{0,4}\|ms", "4.wait_a_sec:[1-9]\d{0,4}\|ms", "5.my_with_block:[1-9]\d{0,4}\|ms", ] self.assert_server_received_expected_request_regex(expected_patterns)
def test_sending_batch_metrics(self): start = datetime.now() start_timestamp = time() client = BatchClient("localhost", self.__class__.port) client.increment("1.test", 8) client.increment("2.login") client.timing("3.query", 9600) client.gauge("4.memory", 102600) client.gauge_delta("5.memory", 2560) client.gauge_delta("6.memory", -1280) client.set("7.ip", "127.0.0.2") client.flush() expected = [ "1.test:8|c", "2.login:1|c", "3.query:9600|ms", "4.memory:102600|g", "5.memory:+2560|g", "6.memory:-1280|g", "7.ip:127.0.0.2|s", ] self.assert_server_received_expected_requests(expected) self.__class__.server.requests.clear() client.timing_since("1.query", start_timestamp) client.timing_since("2.other_query", start) client.flush() chronometer = client.chronometer() chronometer.time_callable("3.sleepy", sleep, 1, (0.02,)) @chronometer.wrap("4.wait_a_sec") def wait_a_sec(): sleep(0.01) wait_a_sec() with client.stopwatch("5.my_with_block"): sleep(0.02) client.flush() expected_patterns = [ "1.query:[1-9]\d{0,4}\|ms", "2.other_query:[1-9]\d{0,4}\|ms", "3.sleepy:[1-9]\d{0,4}\|ms", "4.wait_a_sec:[1-9]\d{0,4}\|ms", "5.my_with_block:[1-9]\d{0,4}\|ms", ] self.assert_server_received_expected_request_regex(expected_patterns)
def test_increment(self): client = BatchClient("localhost") client._socket = self.mock_socket client.increment("event", 2, 0.5) client.flush() self.mock_sendto.assert_called_with( bytearray("event:2|c|@0.5\n".encode()), ("127.0.0.2", 8125) ) self.mock_sendto.reset_mock() client.prefix = "pre." client.increment("login") client.increment("login.fail", 5, 0.2) client.increment("login.ok", rate=0.8) client.flush() self.mock_sendto.assert_called_once_with( bytearray("pre.login:1|c\npre.login.ok:1|c|@0.8\n".encode()), ("127.0.0.2", 8125) )
def test_metrics_partitioned_into_batches(self): client = BatchClient("localhost", batch_size=20) client._socket = self.mock_socket client.increment("fit.a.batch.123") client.timing("_", 1) client.increment("larger.than.batch.becomes.a.batch", 5, 0.9) client.decrement("12") client.set("ab", 'z') client.timing("small", 9) client.gauge("overflow.previous", 10) client.gauge_delta("next", -10) client.increment("_") client.flush() expected_calls = [ mock.call(bytearray("fit.a.batch.123:1|c\n".encode()), ("127.0.0.2", 8125)), mock.call(bytearray("_:1|ms\n".encode()), ("127.0.0.2", 8125)), mock.call(bytearray("larger.than.batch.becomes.a.batch:5|c|@0.9\n".encode()), ("127.0.0.2", 8125)), mock.call(bytearray("12:-1|c\nab:z|s\n".encode()), ("127.0.0.2", 8125)), mock.call(bytearray("small:9|ms\n".encode()), ("127.0.0.2", 8125)), mock.call(bytearray("overflow.previous:10|g\n".encode()), ("127.0.0.2", 8125)), mock.call(bytearray("next:-10|g\n_:1|c\n".encode()), ("127.0.0.2", 8125)), ] self.assertEqual(self.mock_sendto.mock_calls, expected_calls)