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