blob: 625fb8da90b589ec495713e487273423eaebaf22 [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)
76
77 def test_timer_invalid_setup(self):
78 self.assertRaises(ValueError, timeit.Timer, setup=None)
79
80 fake_setup = "import timeit; timeit._fake_timer.setup()"
81 fake_stmt = "import timeit; timeit._fake_timer.inc()"
82
83 def fake_callable_setup(self):
84 self.fake_timer.setup()
85
86 def fake_callable_stmt(self):
87 self.fake_timer.inc()
88
89 def timeit(self, stmt, setup, number=None):
90 self.fake_timer = FakeTimer()
91 t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
92 kwargs = {}
93 if number is None:
94 number = DEFAULT_NUMBER
95 else:
96 kwargs['number'] = number
97 delta_time = t.timeit(**kwargs)
98 self.assertEqual(self.fake_timer.setup_calls, 1)
99 self.assertEqual(self.fake_timer.count, number)
100 self.assertEqual(delta_time, number)
101
102 # Takes too long to run in debug build.
103 #def test_timeit_default_iters(self):
104 # self.timeit(self.fake_stmt, self.fake_setup)
105
106 def test_timeit_zero_iters(self):
107 self.timeit(self.fake_stmt, self.fake_setup, number=0)
108
109 def test_timeit_few_iters(self):
110 self.timeit(self.fake_stmt, self.fake_setup, number=3)
111
112 def test_timeit_callable_stmt(self):
113 self.timeit(self.fake_callable_stmt, self.fake_setup, number=3)
114
115 def test_timeit_callable_stmt_and_setup(self):
116 self.timeit(self.fake_callable_stmt,
117 self.fake_callable_setup, number=3)
118
119 # Takes too long to run in debug build.
120 #def test_timeit_function(self):
121 # delta_time = timeit.timeit(self.fake_stmt, self.fake_setup,
122 # timer=FakeTimer())
123 # self.assertEqual(delta_time, DEFAULT_NUMBER)
124
125 def test_timeit_function_zero_iters(self):
126 delta_time = timeit.timeit(self.fake_stmt, self.fake_setup, number=0,
127 timer=FakeTimer())
128 self.assertEqual(delta_time, 0)
129
130 def repeat(self, stmt, setup, repeat=None, number=None):
131 self.fake_timer = FakeTimer()
132 t = timeit.Timer(stmt=stmt, setup=setup, timer=self.fake_timer)
133 kwargs = {}
134 if repeat is None:
135 repeat = DEFAULT_REPEAT
136 else:
137 kwargs['repeat'] = repeat
138 if number is None:
139 number = DEFAULT_NUMBER
140 else:
141 kwargs['number'] = number
142 delta_times = t.repeat(**kwargs)
143 self.assertEqual(self.fake_timer.setup_calls, repeat)
144 self.assertEqual(self.fake_timer.count, repeat * number)
145 self.assertEqual(delta_times, repeat * [float(number)])
146
147 # Takes too long to run in debug build.
148 #def test_repeat_default(self):
149 # self.repeat(self.fake_stmt, self.fake_setup)
150
151 def test_repeat_zero_reps(self):
152 self.repeat(self.fake_stmt, self.fake_setup, repeat=0)
153
154 def test_repeat_zero_iters(self):
155 self.repeat(self.fake_stmt, self.fake_setup, number=0)
156
157 def test_repeat_few_reps_and_iters(self):
158 self.repeat(self.fake_stmt, self.fake_setup, repeat=3, number=5)
159
160 def test_repeat_callable_stmt(self):
161 self.repeat(self.fake_callable_stmt, self.fake_setup,
162 repeat=3, number=5)
163
164 def test_repeat_callable_stmt_and_setup(self):
165 self.repeat(self.fake_callable_stmt, self.fake_callable_setup,
166 repeat=3, number=5)
167
168 # Takes too long to run in debug build.
169 #def test_repeat_function(self):
170 # delta_times = timeit.repeat(self.fake_stmt, self.fake_setup,
171 # timer=FakeTimer())
172 # self.assertEqual(delta_times, DEFAULT_REPEAT * [float(DEFAULT_NUMBER)])
173
174 def test_repeat_function_zero_reps(self):
175 delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, repeat=0,
176 timer=FakeTimer())
177 self.assertEqual(delta_times, [])
178
179 def test_repeat_function_zero_iters(self):
180 delta_times = timeit.repeat(self.fake_stmt, self.fake_setup, number=0,
181 timer=FakeTimer())
182 self.assertEqual(delta_times, DEFAULT_REPEAT * [0.0])
183
184 def assert_exc_string(self, exc_string, expected_exc_name):
185 exc_lines = exc_string.splitlines()
186 self.assertGreater(len(exc_lines), 2)
187 self.assertTrue(exc_lines[0].startswith('Traceback'))
188 self.assertTrue(exc_lines[-1].startswith(expected_exc_name))
189
190 def test_print_exc(self):
191 s = io.StringIO()
192 t = timeit.Timer("1/0")
193 try:
194 t.timeit()
195 except:
196 t.print_exc(s)
197 self.assert_exc_string(s.getvalue(), 'ZeroDivisionError')
198
199 MAIN_DEFAULT_OUTPUT = "10 loops, best of 3: 1 sec per loop\n"
200
201 def run_main(self, seconds_per_increment=1.0, switches=None, timer=None):
202 if timer is None:
203 timer = FakeTimer(seconds_per_increment=seconds_per_increment)
204 if switches is None:
205 args = []
206 else:
207 args = switches[:]
208 args.append(self.fake_stmt)
209 # timeit.main() modifies sys.path, so save and restore it.
210 orig_sys_path = sys.path[:]
211 with captured_stdout() as s:
212 timeit.main(args=args, _wrap_timer=timer.wrap_timer)
213 sys.path[:] = orig_sys_path[:]
214 return s.getvalue()
215
216 def test_main_bad_switch(self):
217 s = self.run_main(switches=['--bad-switch'])
218 self.assertEqual(s, dedent("""\
219 option --bad-switch not recognized
220 use -h/--help for command line help
221 """))
222
223 def test_main_seconds(self):
224 s = self.run_main(seconds_per_increment=5.5)
225 self.assertEqual(s, "10 loops, best of 3: 5.5 sec per loop\n")
226
227 def test_main_milliseconds(self):
228 s = self.run_main(seconds_per_increment=0.0055)
229 self.assertEqual(s, "100 loops, best of 3: 5.5 msec per loop\n")
230
231 def test_main_microseconds(self):
232 s = self.run_main(seconds_per_increment=0.0000025, switches=['-n100'])
233 self.assertEqual(s, "100 loops, best of 3: 2.5 usec per loop\n")
234
235 def test_main_fixed_iters(self):
236 s = self.run_main(seconds_per_increment=2.0, switches=['-n35'])
237 self.assertEqual(s, "35 loops, best of 3: 2 sec per loop\n")
238
239 def test_main_setup(self):
240 s = self.run_main(seconds_per_increment=2.0,
241 switches=['-n35', '-s', 'print("CustomSetup")'])
242 self.assertEqual(s, "CustomSetup\n" * 3 +
243 "35 loops, best of 3: 2 sec per loop\n")
244
245 def test_main_fixed_reps(self):
246 s = self.run_main(seconds_per_increment=60.0, switches=['-r9'])
247 self.assertEqual(s, "10 loops, best of 9: 60 sec per loop\n")
248
249 def test_main_negative_reps(self):
250 s = self.run_main(seconds_per_increment=60.0, switches=['-r-5'])
251 self.assertEqual(s, "10 loops, best of 1: 60 sec per loop\n")
252
Benjamin Peterson7a9953e2012-10-12 11:44:19 -0400253 @unittest.skipIf(sys.flags.optimize >= 2, "need __doc__")
R David Murraya88da672011-03-16 17:32:27 -0400254 def test_main_help(self):
255 s = self.run_main(switches=['-h'])
256 # Note: It's not clear that the trailing space was intended as part of
257 # the help text, but since it's there, check for it.
258 self.assertEqual(s, timeit.__doc__ + ' ')
259
260 def test_main_using_time(self):
261 fake_timer = FakeTimer()
262 s = self.run_main(switches=['-t'], timer=fake_timer)
263 self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT)
264 self.assertIs(fake_timer.saved_timer, time.time)
265
266 def test_main_using_clock(self):
267 fake_timer = FakeTimer()
268 s = self.run_main(switches=['-c'], timer=fake_timer)
269 self.assertEqual(s, self.MAIN_DEFAULT_OUTPUT)
270 self.assertIs(fake_timer.saved_timer, time.clock)
271
272 def test_main_verbose(self):
273 s = self.run_main(switches=['-v'])
274 self.assertEqual(s, dedent("""\
275 10 loops -> 10 secs
276 raw times: 10 10 10
277 10 loops, best of 3: 1 sec per loop
278 """))
279
280 def test_main_very_verbose(self):
281 s = self.run_main(seconds_per_increment=0.000050, switches=['-vv'])
282 self.assertEqual(s, dedent("""\
283 10 loops -> 0.0005 secs
284 100 loops -> 0.005 secs
285 1000 loops -> 0.05 secs
286 10000 loops -> 0.5 secs
287 raw times: 0.5 0.5 0.5
288 10000 loops, best of 3: 50 usec per loop
289 """))
290
291 def test_main_exception(self):
292 with captured_stderr() as error_stringio:
293 s = self.run_main(switches=['1/0'])
294 self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
295
296 def test_main_exception_fixed_reps(self):
297 with captured_stderr() as error_stringio:
298 s = self.run_main(switches=['-n1', '1/0'])
299 self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
300
301
302def test_main():
303 run_unittest(TestTimeit)
304
305if __name__ == '__main__':
306 test_main()