forked from wolph/python-statsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timer.py
More file actions
192 lines (147 loc) · 5.67 KB
/
test_timer.py
File metadata and controls
192 lines (147 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from __future__ import with_statement
from unittest import TestCase
import mock
import statsd
class TestTimerBase(TestCase):
def tearDown(self):
self._time_patch.stop()
def get_time(self, mock_client, key):
return float(self.get_arg(mock_client, key).split('|')[0])
def get_arg(self, mock_client, key):
return mock_client._send.call_args[0][1][key]
class TestTimerDecorator(TestTimerBase):
def setUp(self):
self.timer = statsd.Timer('timer')
# get time.time() to always return the same value so that this test
# isn't system load dependant.
self._time_patch = mock.patch('time.time')
time_time = self._time_patch.start()
def generator():
i = 0.0
while True:
i += 0.1234
yield i
time_time.side_effect = generator()
@mock.patch('statsd.Client')
def test_decorator_a(self, mock_client):
@self.timer.decorate
def a():
pass
a()
assert self.get_time(mock_client, 'timer.a') == 123.4, \
'This test must execute within 2ms'
@mock.patch('statsd.Client')
def test_decorator_named_spam(self, mock_client):
@self.timer.decorate('spam')
def a():
pass
a()
assert self.get_time(mock_client, 'timer.spam') == 123.4, \
'This test must execute within 2ms'
assert a.__name__ == 'a'
@mock.patch('statsd.Client')
def test_nested_naming_decorator(self, mock_client):
timer = self.timer.get_client('eggs0')
@timer.decorate('d0')
def a():
pass
a()
assert self.get_time(mock_client, 'timer.eggs0.d0') == 123.4, \
'This test must execute within 2ms'
class TestTimerContextManager(TestTimerBase):
def setUp(self):
self.timer = statsd.Timer('cm')
# get time.time() to always return the same value so that this test
# isn't system load dependant.
self._time_patch = mock.patch('time.time')
time_time = self._time_patch.start()
def generator():
i = 0.0
while True:
i += 0.1234
yield i
time_time.side_effect = generator()
@mock.patch('statsd.Client')
def test_context_manager(self, mock_client):
timer = statsd.Timer('cm')
with timer:
# Do something here
pass
assert self.get_time(mock_client, 'cm.total') == 123.4, \
'This test must execute within 2ms'
@mock.patch('statsd.Client')
def test_context_manager_default(self, mock_client):
timer = self.timer.get_client('default')
with timer.time():
pass
assert self.get_time(mock_client, 'cm.default') == 123.4, \
'This test must execute within 2ms'
@mock.patch('statsd.Client')
def test_context_manager_named(self, mock_client):
timer = self.timer.get_client('named')
with timer.time('name'):
pass
assert self.get_time(mock_client, 'cm.named.name') == 123.4, \
'This test must execute within 2ms'
@mock.patch('statsd.Client')
def test_context_manager_class(self, mock_client):
timer = self.timer.get_client('named')
with timer.time(class_=statsd.Timer):
pass
assert self.get_time(mock_client, 'cm.named') == 123.4, \
'This test must execute within 2ms'
class TestTimerAdvancedUsage(TestTimerDecorator):
@mock.patch('statsd.Client')
def test_timer_total(self, mock_client):
timer4 = statsd.Timer('timer4')
timer4.start()
timer4.stop()
assert self.get_time(mock_client, 'timer4.total') == 123.4, \
'This test must execute within 2ms'
timer5 = statsd.Timer('timer5')
timer5.start()
timer5.stop('test')
assert self.get_time(mock_client, 'timer5.test') == 123.4, \
'This test must execute within 2ms'
@mock.patch('statsd.Client')
def test_timer_intermediate(self, mock_client):
timer6 = statsd.Timer('timer6')
timer6.start()
timer6.intermediate('extras')
assert self.get_time(mock_client, 'timer6.extras') == 123.4, \
'This test must execute within 2ms'
timer6.stop()
assert self.get_time(mock_client, 'timer6.total') == 370.2, \
'This test must execute within 2ms'
timer7 = statsd.Timer('timer7')
timer7.start()
timer7.intermediate('extras')
assert self.get_time(mock_client, 'timer7.extras') == 123.4, \
'This test must execute within 2ms'
timer7.stop('test')
assert self.get_time(mock_client, 'timer7.test') == 370.2, \
'This test must execute within 2ms'
class TestTimerZero(TestTimerBase):
def setUp(self):
# get time.time() to always return the same value so that this test
# isn't system load dependant.
self._time_patch = mock.patch('time.time')
time_time = self._time_patch.start()
def generator():
while True:
yield 0
time_time.side_effect = generator()
def tearDown(self):
self._time_patch.stop()
@mock.patch('statsd.Client')
def test_timer_zero(self, mock_client):
timer8 = statsd.Timer('timer8', min_send_threshold=0)
timer8.start()
timer8.stop()
assert mock_client._send.call_args is None, \
'0 timings shouldnt be sent'
timer9 = statsd.Timer('timer9', min_send_threshold=0)
timer9.start()
timer9.stop('test')
assert mock_client._send.call_args is None, \
'0 timings shouldnt be sent'