blob: eebad185010639569442e2d1d19ea1a460861d33 [file] [log] [blame]
R David Murraya88da672011-03-16 17:32:27 -04001import timeit
2import unittest
3import sys
4import io
5import time
6from textwrap import dedent
7
8from test.support import run_unittest
9from test.support import captured_stdout
10from test.support import captured_stderr
11
12# timeit's default number of iterations.
13DEFAULT_NUMBER = 1000000
14
15# timeit's default number of repetitions.
16DEFAULT_REPEAT = 3
17
18# XXX: some tests are commented out that would improve the coverage but take a
19# long time to run because they test the default number of loops, which is
20# large. The tests could be enabled if there was a way to override the default
21# number of loops during testing, but this would require changing the signature
22# of some functions that use the default as a default argument.
23
24class FakeTimer:
25 BASE_TIME = 42.0
26 def __init__(self, seconds_per_increment=1.0):
27 self.count = 0
28 self.setup_calls = 0
29 self.seconds_per_increment=seconds_per_increment
30 timeit._fake_timer = self
31
32 def __call__(self):
33 return self.BASE_TIME + self.count * self.seconds_per_increment
34
35 def inc(self):
36 self.count += 1
37
38 def setup(self):
39 self.setup_calls += 1
40
41 def wrap_timer(self, timer):
42 """Records 'timer' and returns self as callable timer."""
43 self.saved_timer = timer
44 return self
45
46class TestTimeit(unittest.TestCase):
47
48 def tearDown(self):
49 try:
50 del timeit._fake_timer
51 except AttributeError:
52 pass
53
54 def test_reindent_empty(self):
55 self.assertEqual(timeit.reindent("", 0), "")
56 self.assertEqual(timeit.reindent("", 4), "")
57
58 def test_reindent_single(self):
59 self.assertEqual(timeit.reindent("pass", 0), "pass")
60 self.assertEqual(timeit.reindent("pass", 4), "pass")
61
62 def test_reindent_multi_empty(self):
63 self.assertEqual(timeit.reindent("\n\n", 0), "\n\n")
64 self.assertEqual(timeit.reindent("\n\n", 4), "\n \n ")
65
66 def test_reindent_multi(self):
67 self.assertEqual(timeit.reindent(
68 "print()\npass\nbreak", 0),
69 "print()\npass\nbreak")
70 self.assertEqual(timeit.reindent(
71 "print()\npass\nbreak", 4),
72 "print()\n pass\n break")
73
74 def test_timer_invalid_stmt(self):
75 self.assertRaises(ValueError, timeit.Timer, stmt=None)
Serhiy Storchaka2bef5852015-01-26 12:09:17 +020076 self.assertRaises(SyntaxError, timeit.Timer, stmt='return')
77 self.assertRaises(SyntaxError, timeit.Timer, stmt='yield')
78 self.assertRaises(SyntaxError, timeit.Timer, stmt='yield from ()')
79 self.assertRaises(SyntaxError, timeit.Timer, stmt='break')
80 self.assertRaises(SyntaxError, timeit.Timer, stmt='continue')
81 self.assertRaises(SyntaxError, timeit.Timer, stmt='from timeit import *')
R David Murraya88da672011-03-16 17:32:27 -040082
83 def test_timer_invalid_setup(self):
84 self.assertRaises(ValueError, timeit.Timer, setup=None)
Serhiy Storchaka2bef5852015-01-26 12:09:17 +020085 self.assertRaises(SyntaxError, timeit.Timer, setup='return')
86 self.assertRaises(SyntaxError, timeit.Timer, setup='yield')
87 self.assertRaises(SyntaxError, timeit.Timer, setup='yield from ()')
88 self.assertRaises(SyntaxError, timeit.Timer, setup='break')
89 self.assertRaises(SyntaxError, timeit.Timer, setup='continue')
90 self.assertRaises(SyntaxError, timeit.Timer, setup='from timeit import *')
R David Murraya88da672011-03-16 17:32:27 -040091
92 fake_setup = "import timeit; timeit._fake_timer.setup()"
93 fake_stmt = "import timeit; timeit._fake_timer.inc()"
94
95 def fake_callable_setup(self):
96 self.fake_timer.setup()
97
98 def fake_callable_stmt(self):
99 self.fake_timer.inc()
100
Antoine Pitrouef3b9ed2014-08-22 23:13:50 -0400101 def timeit(self, stmt, setup, number=None, globals=None):
R David Murraya88da672011-03-16 17:32:27 -0400102 self.fake_timer = FakeTimer()
Antoine Pitrouef3b9ed2014-08-22 23:13:50 -0400103 t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer,
104 globals=globals)
R David Murraya88da672011-03-16 17:32:27 -0400105 kwargs = {}
106 if number is None:
107 number = DEFAULT_NUMBER
108 else:
109 kwargs['number'] = number
110 delta_time = t.timeit(**kwargs)
111 self.assertEqual(self.fake_timer.setup_calls, 1)
112 self.assertEqual(self.fake_timer.count, number)
113 self.assertEqual(delta_time, number)
114
115 # Takes too long to run in debug build.
116 #def test_timeit_default_iters(self):
117 # self.timeit(self.fake_stmt, self.fake_setup)
118
119 def test_timeit_zero_iters(self):
120 self.timeit(self.fake_stmt, self.fake_setup, number=0)
121
122 def test_timeit_few_iters(self):
123 self.timeit(self.fake_stmt, self.fake_setup, number=3)
124
125 def test_timeit_callable_stmt(self):
126 self.timeit(self.fake_callable_stmt, self.fake_setup, number=3)
127
128 def test_timeit_callable_stmt_and_setup(self):
129 self.timeit(self.fake_callable_stmt,
130 self.fake_callable_setup, number=3)
131
132 # Takes too long to run in debug build.
133 #def test_timeit_function(self):
134 # delta_time = timeit.timeit(self.fake_stmt, self.fake_setup,
135 # timer=FakeTimer())
136 # self.assertEqual(delta_time, DEFAULT_NUMBER)
137
138 def test_timeit_function_zero_iters(self):
139 delta_time = timeit.timeit(self.fake_stmt, self.fake_setup, number=0,
140 timer=FakeTimer())
141 self.assertEqual(delta_time, 0)
142
Antoine Pitrouef3b9ed2014-08-22 23:13:50 -0400143 def test_timeit_globals_args(self):
144 global _global_timer
145 _global_timer = FakeTimer()
146 t = timeit.Timer(stmt='_global_timer.inc()', timer=_global_timer)
147 self.assertRaises(NameError, t.timeit, number=3)
148 timeit.timeit(stmt='_global_timer.inc()', timer=_global_timer,
149 globals=globals(), number=3)
150 local_timer = FakeTimer()
151 timeit.timeit(stmt='local_timer.inc()', timer=local_timer,
152 globals=locals(), number=3)
153
R David Murraya88da672011-03-16 17:32:27 -0400154 def repeat(self, stmt, setup, repeat=None, number=None):
155 self.fake_timer = FakeTimer()
156 t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
157 kwargs = {}
158 if repeat is None:
159 repeat = DEFAULT_REPEAT
160 else:
161 kwargs['repeat'] = repeat
162 if number is None:
163 number = DEFAULT_NUMBER
164 else:
165 kwargs['number'] = number
166 delta_times = t.repeat(**kwargs)
167 self.assertEqual(self.fake_timer.setup_calls, repeat)
168 self.assertEqual(self.fake_timer.count, repeat * number)
169 self.assertEqual(delta_times, repeat * [float(number)])
170
171 # Takes too long to run in debug build.
172 #def test_repeat_default(self):
173 # self.repeat(self.fake_stmt, self.fake_setup)
174
175 def test_repeat_zero_reps(self):
176 self.repeat(self.fake_stmt, self.fake_setup, repeat=0)
177
178 def test_repeat_zero_iters(self):
179 self.repeat(self.fake_stmt, self.fake_setup, number=0)
180
181 def test_repeat_few_reps_and_iters(self):
182 self.repeat(self.fake_stmt, self.fake_setup, repeat=3, number=5)
183
184 def test_repeat_callable_stmt(self):
185 self.repeat(self.fake_callable_stmt, self.fake_setup,
186 repeat=3, number=5)
187
188 def test_repeat_callable_stmt_and_setup(self):
189 self.repeat(self.fake_callable_stmt, self.fake_callable_setup,
190 repeat=3, number=5)
191
192 # Takes too long to run in debug build.
193 #def test_repeat_function(self):
194 # delta_times = timeit.repeat(self.fake_stmt, self.fake_setup,
195 # timer=FakeTimer())
196 # self.assertEqual(delta_times, DEFAULT_REPEAT * [float(DEFAULT_NUMBER)])
197
198 def test_repeat_function_zero_reps(self):
199 delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, repeat=0,
200 timer=FakeTimer())
201 self.assertEqual(delta_times, [])
202
203 def test_repeat_function_zero_iters(self):
204 delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, number=0,
205 timer=FakeTimer())
206 self.assertEqual(delta_times, DEFAULT_REPEAT * [0.0])
207
208 def assert_exc_string(self, exc_string, expected_exc_name):
209 exc_lines = exc_string.splitlines()
210 self.assertGreater(len(exc_lines), 2)
211 self.assertTrue(exc_lines[0].startswith('Traceback'))
212 self.assertTrue(exc_lines[-1].startswith(expected_exc_name))
213
214 def test_print_exc(self):
215 s = io.StringIO()
216 t = timeit.Timer("1/0")
217 try:
218 t.timeit()
219 except:
220 t.print_exc(s)
221 self.assert_exc_string(s.getvalue(), 'ZeroDivisionError')
222
223 MAIN_DEFAULT_OUTPUT = "10 loops, best of 3: 1 sec per loop\n"
224
225 def run_main(self, seconds_per_increment=1.0, switches=None, timer=None):
226 if timer is None:
227 timer = FakeTimer(seconds_per_increment=seconds_per_increment)
228 if switches is None:
229 args = []
230 else:
231 args = switches[:]
232 args.append(self.fake_stmt)
233 # timeit.main() modifies sys.path, so save and restore it.
234 orig_sys_path = sys.path[:]
235 with captured_stdout() as s:
236 timeit.main(args=args, _wrap_timer=timer.wrap_timer)
237 sys.path[:] = orig_sys_path[:]
238 return s.getvalue()
239
240 def test_main_bad_switch(self):
241 s = self.run_main(switches=['--bad-switch'])
242 self.assertEqual(s, dedent("""\
243 option --bad-switch not recognized
244 use -h/--help for command line help
245 """))
246
247 def test_main_seconds(self):
248 s = self.run_main(seconds_per_increment=5.5)
249 self.assertEqual(s, "10 loops, best of 3: 5.5 sec per loop\n")
250
251 def test_main_milliseconds(self):
252 s = self.run_main(seconds_per_increment=0.0055)
253 self.assertEqual(s, "100 loops, best of 3: 5.5 msec per loop\n")
254
255 def test_main_microseconds(self):
256 s = self.run_main(seconds_per_increment=0.0000025, switches=['-n100'])
257 self.assertEqual(s, "100 loops, best of 3: 2.5 usec per loop\n")
258
259 def test_main_fixed_iters(self):
260 s = self.run_main(seconds_per_increment=2.0, switches=['-n35'])
261 self.assertEqual(s, "35 loops, best of 3: 2 sec per loop\n")
262
263 def test_main_setup(self):
264 s = self.run_main(seconds_per_increment=2.0,
265 switches=['-n35', '-s', 'print("CustomSetup")'])
266 self.assertEqual(s, "CustomSetup\n" * 3 +
267 "35 loops, best of 3: 2 sec per loop\n")
268
269 def test_main_fixed_reps(self):
270 s = self.run_main(seconds_per_increment=60.0, switches=['-r9'])
271 self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n")
272
273 def test_main_negative_reps(self):
274 s = self.run_main(seconds_per_increment=60.0, switches=['-r-5'])
275 self.assertEqual(s, "10 loops, best of 1: 60 sec per loop\n")
276
Benjamin Petersona5c40902012-10-12 11:44:19 -0400277 @unittest.skipIf(sys.flags.optimize >= 2, "need __doc__")
R David Murraya88da672011-03-16 17:32:27 -0400278 def test_main_help(self):
279 s = self.run_main(switches=['-h'])
280 # Note: It's not clear that the trailing space was intended as part of
281 # the help text, but since it's there, check for it.
282 self.assertEqual(s, timeit.__doc__ + ' ')
283
284 def test_main_using_time(self):
285 fake_timer = FakeTimer()
286 s = self.run_main(switches=['-t'], timer=fake_timer)
287 self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT)
288 self.assertIs(fake_timer.saved_timer, time.time)
289
290 def test_main_using_clock(self):
291 fake_timer = FakeTimer()
292 s = self.run_main(switches=['-c'], timer=fake_timer)
293 self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT)
294 self.assertIs(fake_timer.saved_timer, time.clock)
295
296 def test_main_verbose(self):
297 s = self.run_main(switches=['-v'])
298 self.assertEqual(s, dedent("""\
299 10 loops -> 10 secs
300 raw times: 10 10 10
301 10 loops, best of 3: 1 sec per loop
302 """))
303
304 def test_main_very_verbose(self):
305 s = self.run_main(seconds_per_increment=0.000050, switches=['-vv'])
306 self.assertEqual(s, dedent("""\
307 10 loops -> 0.0005 secs
308 100 loops -> 0.005 secs
309 1000 loops -> 0.05 secs
310 10000 loops -> 0.5 secs
311 raw times: 0.5 0.5 0.5
312 10000 loops, best of 3: 50 usec per loop
313 """))
314
Robert Collins302dbc62015-03-18 09:54:50 +1300315 def test_main_with_time_unit(self):
316 unit_sec = self.run_main(seconds_per_increment=0.002,
317 switches=['-u', 'sec'])
318 self.assertEqual(unit_sec,
319 "1000 loops, best of 3: 0.002 sec per loop\n")
320 unit_msec = self.run_main(seconds_per_increment=0.002,
321 switches=['-u', 'msec'])
322 self.assertEqual(unit_msec,
323 "1000 loops, best of 3: 2 msec per loop\n")
324 unit_usec = self.run_main(seconds_per_increment=0.002,
325 switches=['-u', 'usec'])
326 self.assertEqual(unit_usec,
327 "1000 loops, best of 3: 2e+03 usec per loop\n")
328 # Test invalid unit input
329 with captured_stderr() as error_stringio:
330 invalid = self.run_main(seconds_per_increment=0.002,
331 switches=['-u', 'parsec'])
332 self.assertEqual(error_stringio.getvalue(),
333 "Unrecognized unit. Please select usec, msec, or sec.\n")
334
R David Murraya88da672011-03-16 17:32:27 -0400335 def test_main_exception(self):
336 with captured_stderr() as error_stringio:
337 s = self.run_main(switches=['1/0'])
338 self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
339
340 def test_main_exception_fixed_reps(self):
341 with captured_stderr() as error_stringio:
342 s = self.run_main(switches=['-n1', '1/0'])
343 self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
344
345
346def test_main():
347 run_unittest(TestTimeit)
348
349if __name__ == '__main__':
350 test_main()