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