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