|
1 #!/usr/bin/env python2.5 |
|
2 """ |
|
3 This is a benchmarking script to test the speed of 2to3's pattern matching |
|
4 system. It's equivalent to "refactor.py -f all" for every Python module |
|
5 in sys.modules, but without engaging the actual transformations. |
|
6 """ |
|
7 |
|
8 __author__ = "Collin Winter <collinw at gmail.com>" |
|
9 |
|
10 # Python imports |
|
11 import os.path |
|
12 import sys |
|
13 from time import time |
|
14 |
|
15 # Test imports |
|
16 from .support import adjust_path |
|
17 adjust_path() |
|
18 |
|
19 # Local imports |
|
20 from .. import refactor |
|
21 |
|
22 ### Mock code for refactor.py and the fixers |
|
23 ############################################################################### |
|
24 class Options: |
|
25 def __init__(self, **kwargs): |
|
26 for k, v in kwargs.items(): |
|
27 setattr(self, k, v) |
|
28 |
|
29 self.verbose = False |
|
30 |
|
31 def dummy_transform(*args, **kwargs): |
|
32 pass |
|
33 |
|
34 ### Collect list of modules to match against |
|
35 ############################################################################### |
|
36 files = [] |
|
37 for mod in sys.modules.values(): |
|
38 if mod is None or not hasattr(mod, '__file__'): |
|
39 continue |
|
40 f = mod.__file__ |
|
41 if f.endswith('.pyc'): |
|
42 f = f[:-1] |
|
43 if f.endswith('.py'): |
|
44 files.append(f) |
|
45 |
|
46 ### Set up refactor and run the benchmark |
|
47 ############################################################################### |
|
48 options = Options(fix=["all"], print_function=False, doctests_only=False) |
|
49 refactor = refactor.RefactoringTool(options) |
|
50 for fixer in refactor.fixers: |
|
51 # We don't want them to actually fix the tree, just match against it. |
|
52 fixer.transform = dummy_transform |
|
53 |
|
54 t = time() |
|
55 for f in files: |
|
56 print "Matching", f |
|
57 refactor.refactor_file(f) |
|
58 print "%d seconds to match %d files" % (time() - t, len(sys.modules)) |