21
21
RE_TIME_MESSAGE = re .compile (TIME_PREFIX + r" 0\.\d{4} seconds" )
22
22
23
23
24
+ def waste_time (num = 1000 ):
25
+ """Just waste a little bit of time"""
26
+ sum (n ** 2 for n in range (num ))
27
+
28
+
24
29
@Timer (text = TIME_MESSAGE )
25
- def timewaster (num ):
30
+ def decorated_timewaste (num = 1000 ):
26
31
"""Just waste a little bit of time"""
27
32
sum (n ** 2 for n in range (num ))
28
33
29
34
30
35
@Timer (name = "accumulator" , text = TIME_MESSAGE )
31
- def accumulated_timewaste (num ):
36
+ def accumulated_timewaste (num = 1000 ):
32
37
"""Just waste a little bit of time"""
33
38
sum (n ** 2 for n in range (num ))
34
39
@@ -48,7 +53,7 @@ def __call__(self, message):
48
53
#
49
54
def test_timer_as_decorator (capsys ):
50
55
"""Test that decorated function prints timing information"""
51
- timewaster ( 1000 )
56
+ decorated_timewaste ( )
52
57
stdout , stderr = capsys .readouterr ()
53
58
assert RE_TIME_MESSAGE .match (stdout )
54
59
assert stdout .count ("\n " ) == 1
@@ -58,7 +63,7 @@ def test_timer_as_decorator(capsys):
58
63
def test_timer_as_context_manager (capsys ):
59
64
"""Test that timed context prints timing information"""
60
65
with Timer (text = TIME_MESSAGE ):
61
- sum ( n ** 2 for n in range ( 1000 ) )
66
+ waste_time ( )
62
67
stdout , stderr = capsys .readouterr ()
63
68
assert RE_TIME_MESSAGE .match (stdout )
64
69
assert stdout .count ("\n " ) == 1
@@ -69,7 +74,7 @@ def test_explicit_timer(capsys):
69
74
"""Test that timed section prints timing information"""
70
75
t = Timer (text = TIME_MESSAGE )
71
76
t .start ()
72
- sum ( n ** 2 for n in range ( 1000 ) )
77
+ waste_time ( )
73
78
t .stop ()
74
79
stdout , stderr = capsys .readouterr ()
75
80
assert RE_TIME_MESSAGE .match (stdout )
@@ -96,14 +101,14 @@ def test_custom_logger():
96
101
"""Test that we can use a custom logger"""
97
102
logger = CustomLogger ()
98
103
with Timer (text = TIME_MESSAGE , logger = logger ):
99
- sum ( n ** 2 for n in range ( 1000 ) )
104
+ waste_time ( )
100
105
assert RE_TIME_MESSAGE .match (logger .messages )
101
106
102
107
103
108
def test_timer_without_text (capsys ):
104
109
"""Test that timer with logger=None does not print anything"""
105
110
with Timer (logger = None ):
106
- sum ( n ** 2 for n in range ( 1000 ) )
111
+ waste_time ( )
107
112
108
113
stdout , stderr = capsys .readouterr ()
109
114
assert stdout == ""
@@ -112,8 +117,8 @@ def test_timer_without_text(capsys):
112
117
113
118
def test_accumulated_decorator (capsys ):
114
119
"""Test that decorated timer can accumulate"""
115
- accumulated_timewaste (1000 )
116
- accumulated_timewaste (1000 )
120
+ accumulated_timewaste ()
121
+ accumulated_timewaste ()
117
122
118
123
stdout , stderr = capsys .readouterr ()
119
124
lines = stdout .strip ().split ("\n " )
@@ -127,9 +132,9 @@ def test_accumulated_context_manager(capsys):
127
132
"""Test that context manager timer can accumulate"""
128
133
t = Timer (name = "accumulator" , text = TIME_MESSAGE )
129
134
with t :
130
- sum ( n ** 2 for n in range ( 1000 ) )
135
+ waste_time ( )
131
136
with t :
132
- sum ( n ** 2 for n in range ( 1000 ) )
137
+ waste_time ( )
133
138
134
139
stdout , stderr = capsys .readouterr ()
135
140
lines = stdout .strip ().split ("\n " )
@@ -144,10 +149,10 @@ def test_accumulated_explicit_timer(capsys):
144
149
t = Timer (name = "accumulated_explicit_timer" , text = TIME_MESSAGE )
145
150
total = 0
146
151
t .start ()
147
- sum ( n ** 2 for n in range ( 1000 ) )
152
+ waste_time ( )
148
153
total += t .stop ()
149
154
t .start ()
150
- sum ( n ** 2 for n in range ( 1000 ) )
155
+ waste_time ( )
151
156
total += t .stop ()
152
157
153
158
stdout , stderr = capsys .readouterr ()
@@ -179,3 +184,57 @@ def test_timer_sets_last():
179
184
time .sleep (0.02 )
180
185
181
186
assert t .last >= 0.02
187
+
188
+
189
+ def test_timers_cleared ():
190
+ """Test that timers can be cleared"""
191
+ with Timer (name = "timer_to_be_cleared" ):
192
+ waste_time ()
193
+
194
+ assert "timer_to_be_cleared" in Timer .timers
195
+ Timer .timers .clear ()
196
+ assert not Timer .timers
197
+
198
+
199
+ def test_running_cleared_timers ():
200
+ """Test that timers can still be run after they're cleared"""
201
+ t = Timer (name = "timer_to_be_cleared" )
202
+ Timer .timers .clear ()
203
+
204
+ accumulated_timewaste ()
205
+ with t :
206
+ waste_time ()
207
+
208
+ assert "accumulator" in Timer .timers
209
+ assert "timer_to_be_cleared" in Timer .timers
210
+
211
+
212
+ def test_timers_stats ():
213
+ """Test that we can get basic statistics from timers"""
214
+ name = "timer_with_stats"
215
+ t = Timer (name = name )
216
+ for num in range (5 , 10 ):
217
+ with t :
218
+ waste_time (num = 100 * num )
219
+
220
+ stats = Timer .timers
221
+ assert stats .total (name ) == stats [name ]
222
+ assert stats .count (name ) == 5
223
+ assert stats .min (name ) <= stats .median (name ) <= stats .max (name )
224
+ assert stats .mean (name ) >= stats .min (name )
225
+ assert stats .stdev (name ) >= 0
226
+
227
+
228
+ def test_stats_missing_timers ():
229
+ """Test that getting statistics from non-existent timers raises exception"""
230
+ with pytest .raises (KeyError ):
231
+ Timer .timers .count ("non_existent_timer" )
232
+
233
+ with pytest .raises (KeyError ):
234
+ Timer .timers .stdev ("non_existent_timer" )
235
+
236
+
237
+ def test_setting_timers_exception ():
238
+ """Test that setting .timers items raises exception"""
239
+ with pytest .raises (TypeError ):
240
+ Timer .timers ["set_timer" ] = 1.23
0 commit comments