blob: 1c4b8fcfca599127524645aa4c592fa74b6df6d1 [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001#!/usr/bin/env python2.5
2"""
3This is a benchmarking script to test the speed of 2to3's pattern matching
4system. It's equivalent to "refactor.py -f all" for every Python module
5in sys.modules, but without engaging the actual transformations.
6"""
7
8__author__ = "Collin Winter <collinw at gmail.com>"
9
10# Python imports
11import os.path
12import sys
13from time import time
14
15# Test imports
16from support import adjust_path
17adjust_path()
18
19# Local imports
20from .. import refactor
21
22### Mock code for refactor.py and the fixers
23###############################################################################
24class Options:
25 def __init__(self, **kwargs):
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000026 for k, v in list(kwargs.items()):
Martin v. Löwisef04c442008-03-19 05:04:44 +000027 setattr(self, k, v)
28
29 self.verbose = False
Martin v. Löwisf733c602008-03-19 05:26:18 +000030
Martin v. Löwisef04c442008-03-19 05:04:44 +000031def dummy_transform(*args, **kwargs):
32 pass
Martin v. Löwisf733c602008-03-19 05:26:18 +000033
Martin v. Löwisef04c442008-03-19 05:04:44 +000034### Collect list of modules to match against
35###############################################################################
36files = []
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000037for mod in list(sys.modules.values()):
Martin v. Löwisef04c442008-03-19 05:04:44 +000038 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###############################################################################
48options = Options(fix=["all"], print_function=False, doctests_only=False)
49refactor = refactor.RefactoringTool(options)
50for 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
54t = time()
55for f in files:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000056 print("Matching", f)
Martin v. Löwisef04c442008-03-19 05:04:44 +000057 refactor.refactor_file(f)
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +000058print("%d seconds to match %d files" % (time() - t, len(sys.modules)))