def test_context_mgr_reuse(mock_socket): statsd_reusable = FastatsClient(prefix = 'testing') with statsd_reusable as statsd: statsd.incr('test', 123) with statsd_reusable as statsd: statsd.incr('test2', 234) assert mock_socket.sent == [b'testing.test:123|c', b'testing.test2:234|c'] or mock_socket.sent == [b'testing.test:123|c\ntesting.test2:234|c']
def test_timer_no_rate(mock_socket): with FastatsClient(prefix = 'testing') as statsd: with statsd.timer('test'): time.sleep(0.1) stat, delay, unit = _decompose_message(mock_socket.sent[0]) assert stat == 'testing.test' assert 50 < delay < 150 assert unit == 'ms'
def test_incr_rate(mock_socket): with FastatsClient(prefix = 'testing') as statsd: for i in range(10000): statsd.incr('someALittleBitLongDescribtionOfTheMetric', 123, 0.5) approx_messages_count = 0 messages_length = 0 for buffer in mock_socket.sent: approx_messages_count += buffer.count(b':') messages_length += len(buffer) # Uncomment these for debugging output # print('Packet number', len(mock_socket.sent)) # print('Approx packet length', messages_length/len(mock_socket.sent)) # print('Rate limited msgs count', approx_messages_count) assert mock_socket.sent[0].startswith(b'testing.someALittleBitLongDescribtionOfTheMetric:123|c|@0.5') assert 1000 < approx_messages_count < 9000, 'Rate 10000 calls at rate 0.5 should produce about 5000 statsd calls'
def test_fastatsd_timer_10000x(benchmark): with FastatsClient() as client: benchmark(_timer_test, client)
def test_decr(mock_socket): with FastatsClient(prefix = 'testing') as statsd: statsd.decr('test', 123) assert mock_socket.sent == [b'testing.test:-123|c']
def test_no_context_mgr(mock_socket): statsd = FastatsClient(prefix = 'testing') statsd.incr('test', 123) statsd.stop() assert mock_socket.sent == [b'testing.test:123|c']
def test_incr_without_prefix(mock_socket): with FastatsClient() as statsd: statsd.incr('test', 123) assert mock_socket.sent == [b'test:123|c']
def test_set(mock_socket): with FastatsClient(prefix = 'testing') as statsd: statsd.set('test', 123) assert mock_socket.sent == [b'testing.test:123|s']
def test_gauge_delta_negative(mock_socket): with FastatsClient(prefix = 'testing') as statsd: statsd.gauge('test', -123, delta = True) assert mock_socket.sent == [b'testing.test:-123.000000|g']
def test_gauge(mock_socket): with FastatsClient(prefix = 'testing') as statsd: statsd.gauge('test', 123) assert mock_socket.sent == [b'testing.test:123.000000|g']