2121RE_TIME_MESSAGE = re .compile (TIME_PREFIX + r" 0\.\d{4} seconds" )
2222
2323
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+
2429@Timer (text = TIME_MESSAGE )
25- def timewaster (num ):
30+ def decorated_timewaste (num = 1000 ):
2631 """Just waste a little bit of time"""
2732 sum (n ** 2 for n in range (num ))
2833
2934
3035@Timer (name = "accumulator" , text = TIME_MESSAGE )
31- def accumulated_timewaste (num ):
36+ def accumulated_timewaste (num = 1000 ):
3237 """Just waste a little bit of time"""
3338 sum (n ** 2 for n in range (num ))
3439
@@ -48,7 +53,7 @@ def __call__(self, message):
4853#
4954def test_timer_as_decorator (capsys ):
5055 """Test that decorated function prints timing information"""
51- timewaster ( 1000 )
56+ decorated_timewaste ( )
5257 stdout , stderr = capsys .readouterr ()
5358 assert RE_TIME_MESSAGE .match (stdout )
5459 assert stdout .count ("\n " ) == 1
@@ -58,7 +63,7 @@ def test_timer_as_decorator(capsys):
5863def test_timer_as_context_manager (capsys ):
5964 """Test that timed context prints timing information"""
6065 with Timer (text = TIME_MESSAGE ):
61- sum ( n ** 2 for n in range ( 1000 ) )
66+ waste_time ( )
6267 stdout , stderr = capsys .readouterr ()
6368 assert RE_TIME_MESSAGE .match (stdout )
6469 assert stdout .count ("\n " ) == 1
@@ -69,7 +74,7 @@ def test_explicit_timer(capsys):
6974 """Test that timed section prints timing information"""
7075 t = Timer (text = TIME_MESSAGE )
7176 t .start ()
72- sum ( n ** 2 for n in range ( 1000 ) )
77+ waste_time ( )
7378 t .stop ()
7479 stdout , stderr = capsys .readouterr ()
7580 assert RE_TIME_MESSAGE .match (stdout )
@@ -96,14 +101,14 @@ def test_custom_logger():
96101 """Test that we can use a custom logger"""
97102 logger = CustomLogger ()
98103 with Timer (text = TIME_MESSAGE , logger = logger ):
99- sum ( n ** 2 for n in range ( 1000 ) )
104+ waste_time ( )
100105 assert RE_TIME_MESSAGE .match (logger .messages )
101106
102107
103108def test_timer_without_text (capsys ):
104109 """Test that timer with logger=None does not print anything"""
105110 with Timer (logger = None ):
106- sum ( n ** 2 for n in range ( 1000 ) )
111+ waste_time ( )
107112
108113 stdout , stderr = capsys .readouterr ()
109114 assert stdout == ""
@@ -112,8 +117,8 @@ def test_timer_without_text(capsys):
112117
113118def test_accumulated_decorator (capsys ):
114119 """Test that decorated timer can accumulate&
1E79
quot;""
115- accumulated_timewaste (1000 )
116- accumulated_timewaste (1000 )
120+ accumulated_timewaste ()
121+ accumulated_timewaste ()
117122
118123 stdout , stderr = capsys .readouterr ()
119124 lines = stdout .strip ().split ("\n " )
@@ -127,9 +132,9 @@ def test_accumulated_context_manager(capsys):
127132 """Test that context manager timer can accumulate"""
128133 t = Timer (name = "accumulator" , text = TIME_MESSAGE )
129134 with t :
130- sum ( n ** 2 for n in range ( 1000 ) )
135+ waste_time ( )
131136 with t :
132- sum ( n ** 2 for n in range ( 1000 ) )
137+ waste_time ( )
133138
134139 stdout , stderr = capsys .readouterr ()
135140 lines = stdout .strip ().split ("\n " )
@@ -144,10 +149,10 @@ def test_accumulated_explicit_timer(capsys):
144149 t = Timer (name = "accumulated_explicit_timer" , text = TIME_MESSAGE )
145150 total = 0
146151 t .start ()
147- sum ( n ** 2 for n in range ( 1000 ) )
152+ waste_time ( )
148153 total += t .stop ()
149154 t .start ()
150- sum ( n ** 2 for n in range ( 1000 ) )
155+ waste_time ( )
151156 total += t .stop ()
152157
153158 stdout , stderr = capsys .readouterr ()
@@ -179,3 +184,57 @@ def test_timer_sets_last():
179184 time .sleep (0.02 )
180185
181186 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