Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 1 | """Benchmark some basic import use-cases. |
| 2 | |
| 3 | The assumption is made that this benchmark is run in a fresh interpreter and |
| 4 | thus has no external changes made to import-related attributes in sys. |
| 5 | |
| 6 | """ |
Brett Cannon | d382bfc | 2012-07-20 14:54:53 -0400 | [diff] [blame^] | 7 | from test.test_importlib import util |
| 8 | from test.test_importlib.source import util as source_util |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 9 | import decimal |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 10 | import imp |
| 11 | import importlib |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 12 | import importlib.machinery |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 13 | import json |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 14 | import os |
| 15 | import py_compile |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 16 | import sys |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 17 | import tabnanny |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 18 | import timeit |
| 19 | |
| 20 | |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 21 | def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3): |
| 22 | """Bench the given statement as many times as necessary until total |
| 23 | executions take one second.""" |
| 24 | stmt = "__import__({!r})".format(name) |
| 25 | timer = timeit.Timer(stmt) |
| 26 | for x in range(repeat): |
| 27 | total_time = 0 |
| 28 | count = 0 |
| 29 | while total_time < seconds: |
| 30 | try: |
| 31 | total_time += timer.timeit(1) |
| 32 | finally: |
| 33 | cleanup() |
| 34 | count += 1 |
| 35 | else: |
| 36 | # One execution too far |
| 37 | if total_time > seconds: |
| 38 | count -= 1 |
Brett Cannon | 7b9bcb8 | 2010-07-15 06:24:04 +0000 | [diff] [blame] | 39 | yield count // seconds |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 40 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 41 | def from_cache(seconds, repeat): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 42 | """sys.modules""" |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 43 | name = '<benchmark import>' |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 44 | module = imp.new_module(name) |
| 45 | module.__file__ = '<test>' |
| 46 | module.__package__ = '' |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 47 | with util.uncache(name): |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 48 | sys.modules[name] = module |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 49 | for result in bench(name, repeat=repeat, seconds=seconds): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 50 | yield result |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 51 | |
| 52 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 53 | def builtin_mod(seconds, repeat): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 54 | """Built-in module""" |
| 55 | name = 'errno' |
| 56 | if name in sys.modules: |
| 57 | del sys.modules[name] |
Brett Cannon | 7b9bcb8 | 2010-07-15 06:24:04 +0000 | [diff] [blame] | 58 | # Relying on built-in importer being implicit. |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 59 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 60 | seconds=seconds): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 61 | yield result |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 62 | |
| 63 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 64 | def source_wo_bytecode(seconds, repeat): |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 65 | """Source w/o bytecode: small""" |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 66 | sys.dont_write_bytecode = True |
| 67 | try: |
| 68 | name = '__importlib_test_benchmark__' |
| 69 | # Clears out sys.modules and puts an entry at the front of sys.path. |
| 70 | with source_util.create_modules(name) as mapping: |
| 71 | assert not os.path.exists(imp.cache_from_source(mapping[name])) |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 72 | sys.meta_path.append(importlib.machinery.PathFinder) |
| 73 | loader = (importlib.machinery.SourceFileLoader, |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 74 | importlib.machinery.SOURCE_SUFFIXES, True) |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 75 | sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader)) |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 76 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 77 | seconds=seconds): |
| 78 | yield result |
| 79 | finally: |
| 80 | sys.dont_write_bytecode = False |
| 81 | |
| 82 | |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 83 | def _wo_bytecode(module): |
| 84 | name = module.__name__ |
| 85 | def benchmark_wo_bytecode(seconds, repeat): |
| 86 | """Source w/o bytecode: {}""" |
| 87 | bytecode_path = imp.cache_from_source(module.__file__) |
| 88 | if os.path.exists(bytecode_path): |
| 89 | os.unlink(bytecode_path) |
| 90 | sys.dont_write_bytecode = True |
| 91 | try: |
| 92 | for result in bench(name, lambda: sys.modules.pop(name), |
| 93 | repeat=repeat, seconds=seconds): |
| 94 | yield result |
| 95 | finally: |
| 96 | sys.dont_write_bytecode = False |
| 97 | |
| 98 | benchmark_wo_bytecode.__doc__ = benchmark_wo_bytecode.__doc__.format(name) |
| 99 | return benchmark_wo_bytecode |
| 100 | |
| 101 | tabnanny_wo_bytecode = _wo_bytecode(tabnanny) |
| 102 | decimal_wo_bytecode = _wo_bytecode(decimal) |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 103 | |
| 104 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 105 | def source_writing_bytecode(seconds, repeat): |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 106 | """Source writing bytecode: small""" |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 107 | assert not sys.dont_write_bytecode |
| 108 | name = '__importlib_test_benchmark__' |
| 109 | with source_util.create_modules(name) as mapping: |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 110 | sys.meta_path.append(importlib.machinery.PathFinder) |
| 111 | loader = (importlib.machinery.SourceFileLoader, |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 112 | importlib.machinery.SOURCE_SUFFIXES, True) |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 113 | sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader)) |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 114 | def cleanup(): |
| 115 | sys.modules.pop(name) |
| 116 | os.unlink(imp.cache_from_source(mapping[name])) |
| 117 | for result in bench(name, cleanup, repeat=repeat, seconds=seconds): |
| 118 | assert not os.path.exists(imp.cache_from_source(mapping[name])) |
| 119 | yield result |
| 120 | |
| 121 | |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 122 | def _writing_bytecode(module): |
| 123 | name = module.__name__ |
| 124 | def writing_bytecode_benchmark(seconds, repeat): |
| 125 | """Source writing bytecode: {}""" |
| 126 | assert not sys.dont_write_bytecode |
| 127 | def cleanup(): |
| 128 | sys.modules.pop(name) |
| 129 | os.unlink(imp.cache_from_source(module.__file__)) |
| 130 | for result in bench(name, cleanup, repeat=repeat, seconds=seconds): |
| 131 | yield result |
| 132 | |
| 133 | writing_bytecode_benchmark.__doc__ = ( |
| 134 | writing_bytecode_benchmark.__doc__.format(name)) |
| 135 | return writing_bytecode_benchmark |
| 136 | |
| 137 | tabnanny_writing_bytecode = _writing_bytecode(tabnanny) |
| 138 | decimal_writing_bytecode = _writing_bytecode(decimal) |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 139 | |
| 140 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 141 | def source_using_bytecode(seconds, repeat): |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 142 | """Source w/ bytecode: small""" |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 143 | name = '__importlib_test_benchmark__' |
| 144 | with source_util.create_modules(name) as mapping: |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 145 | sys.meta_path.append(importlib.machinery.PathFinder) |
| 146 | loader = (importlib.machinery.SourceFileLoader, |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 147 | importlib.machinery.SOURCE_SUFFIXES, True) |
Brett Cannon | 810c64d | 2012-05-11 11:12:00 -0400 | [diff] [blame] | 148 | sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader)) |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 149 | py_compile.compile(mapping[name]) |
| 150 | assert os.path.exists(imp.cache_from_source(mapping[name])) |
| 151 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 152 | seconds=seconds): |
| 153 | yield result |
| 154 | |
| 155 | |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 156 | def _using_bytecode(module): |
| 157 | name = module.__name__ |
| 158 | def using_bytecode_benchmark(seconds, repeat): |
| 159 | """Source w/ bytecode: {}""" |
| 160 | py_compile.compile(module.__file__) |
| 161 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 162 | seconds=seconds): |
| 163 | yield result |
| 164 | |
| 165 | using_bytecode_benchmark.__doc__ = ( |
| 166 | using_bytecode_benchmark.__doc__.format(name)) |
| 167 | return using_bytecode_benchmark |
| 168 | |
| 169 | tabnanny_using_bytecode = _using_bytecode(tabnanny) |
| 170 | decimal_using_bytecode = _using_bytecode(decimal) |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 171 | |
| 172 | |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 173 | def main(import_, options): |
| 174 | if options.source_file: |
| 175 | with options.source_file: |
| 176 | prev_results = json.load(options.source_file) |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 177 | else: |
| 178 | prev_results = {} |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 179 | __builtins__.__import__ = import_ |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 180 | benchmarks = (from_cache, builtin_mod, |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 181 | source_writing_bytecode, |
Brett Cannon | cae1068 | 2012-02-07 09:40:33 -0500 | [diff] [blame] | 182 | source_wo_bytecode, source_using_bytecode, |
Brett Cannon | 466e6a9 | 2012-02-07 09:19:12 -0500 | [diff] [blame] | 183 | tabnanny_writing_bytecode, |
Brett Cannon | cae1068 | 2012-02-07 09:40:33 -0500 | [diff] [blame] | 184 | tabnanny_wo_bytecode, tabnanny_using_bytecode, |
| 185 | decimal_writing_bytecode, |
| 186 | decimal_wo_bytecode, decimal_using_bytecode, |
| 187 | ) |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 188 | if options.benchmark: |
Brett Cannon | e3a9ae5 | 2012-01-30 19:27:51 -0500 | [diff] [blame] | 189 | for b in benchmarks: |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 190 | if b.__doc__ == options.benchmark: |
Brett Cannon | e3a9ae5 | 2012-01-30 19:27:51 -0500 | [diff] [blame] | 191 | benchmarks = [b] |
| 192 | break |
| 193 | else: |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 194 | print('Unknown benchmark: {!r}'.format(options.benchmark, |
| 195 | file=sys.stderr)) |
Brett Cannon | e3a9ae5 | 2012-01-30 19:27:51 -0500 | [diff] [blame] | 196 | sys.exit(1) |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 197 | seconds = 1 |
| 198 | seconds_plural = 's' if seconds > 1 else '' |
| 199 | repeat = 3 |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 200 | header = ('Measuring imports/second over {} second{}, best out of {}\n' |
Brett Cannon | cae1068 | 2012-02-07 09:40:33 -0500 | [diff] [blame] | 201 | 'Entire benchmark run should take about {} seconds\n' |
| 202 | 'Using {!r} as __import__\n') |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 203 | print(header.format(seconds, seconds_plural, repeat, |
Brett Cannon | cae1068 | 2012-02-07 09:40:33 -0500 | [diff] [blame] | 204 | len(benchmarks) * seconds * repeat, __import__)) |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 205 | new_results = {} |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 206 | for benchmark in benchmarks: |
| 207 | print(benchmark.__doc__, "[", end=' ') |
| 208 | sys.stdout.flush() |
| 209 | results = [] |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame] | 210 | for result in benchmark(seconds=seconds, repeat=repeat): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 211 | results.append(result) |
| 212 | print(result, end=' ') |
| 213 | sys.stdout.flush() |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 214 | assert not sys.dont_write_bytecode |
Brett Cannon | cbe1a4e | 2010-07-16 19:26:23 +0000 | [diff] [blame] | 215 | print("]", "best is", format(max(results), ',d')) |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 216 | new_results[benchmark.__doc__] = results |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 217 | if prev_results: |
| 218 | print('\n\nComparing new vs. old\n') |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 219 | for benchmark in benchmarks: |
| 220 | benchmark_name = benchmark.__doc__ |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 221 | old_result = max(prev_results[benchmark_name]) |
| 222 | new_result = max(new_results[benchmark_name]) |
| 223 | result = '{:,d} vs. {:,d} ({:%})'.format(new_result, |
| 224 | old_result, |
| 225 | new_result/old_result) |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 226 | print(benchmark_name, ':', result) |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 227 | if options.dest_file: |
| 228 | with options.dest_file: |
| 229 | json.dump(new_results, options.dest_file, indent=2) |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 230 | |
| 231 | |
| 232 | if __name__ == '__main__': |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 233 | import argparse |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 234 | |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 235 | parser = argparse.ArgumentParser() |
| 236 | parser.add_argument('-b', '--builtin', dest='builtin', action='store_true', |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 237 | default=False, help="use the built-in __import__") |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 238 | parser.add_argument('-r', '--read', dest='source_file', |
| 239 | type=argparse.FileType('r'), |
| 240 | help='file to read benchmark data from to compare ' |
| 241 | 'against') |
| 242 | parser.add_argument('-w', '--write', dest='dest_file', |
| 243 | type=argparse.FileType('w'), |
| 244 | help='file to write benchmark data to') |
Brett Cannon | e3a9ae5 | 2012-01-30 19:27:51 -0500 | [diff] [blame] | 245 | parser.add_argument('--benchmark', dest='benchmark', |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 246 | help='specific benchmark to run') |
Brett Cannon | 190f33c | 2012-01-30 19:12:29 -0500 | [diff] [blame] | 247 | options = parser.parse_args() |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 248 | import_ = __import__ |
| 249 | if not options.builtin: |
| 250 | import_ = importlib.__import__ |
| 251 | |
Brett Cannon | dfc3270 | 2012-02-23 19:34:35 -0500 | [diff] [blame] | 252 | main(import_, options) |