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 | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 12 | import os |
| 13 | import py_compile |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 14 | import sys |
| 15 | import timeit |
| 16 | |
| 17 | |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 18 | def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3): |
| 19 | """Bench the given statement as many times as necessary until total |
| 20 | executions take one second.""" |
| 21 | stmt = "__import__({!r})".format(name) |
| 22 | timer = timeit.Timer(stmt) |
| 23 | for x in range(repeat): |
| 24 | total_time = 0 |
| 25 | count = 0 |
| 26 | while total_time < seconds: |
| 27 | try: |
| 28 | total_time += timer.timeit(1) |
| 29 | finally: |
| 30 | cleanup() |
| 31 | count += 1 |
| 32 | else: |
| 33 | # One execution too far |
| 34 | if total_time > seconds: |
| 35 | count -= 1 |
Brett Cannon | 7b9bcb8 | 2010-07-15 06:24:04 +0000 | [diff] [blame] | 36 | yield count // seconds |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 37 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 38 | def from_cache(seconds, repeat): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 39 | """sys.modules""" |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 40 | name = '<benchmark import>' |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 41 | module = imp.new_module(name) |
| 42 | module.__file__ = '<test>' |
| 43 | module.__package__ = '' |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 44 | with util.uncache(name): |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 45 | sys.modules[name] = module |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 46 | for result in bench(name, repeat=repeat, seconds=seconds): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 47 | yield result |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 48 | |
| 49 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 50 | def builtin_mod(seconds, repeat): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 51 | """Built-in module""" |
| 52 | name = 'errno' |
| 53 | if name in sys.modules: |
| 54 | del sys.modules[name] |
Brett Cannon | 7b9bcb8 | 2010-07-15 06:24:04 +0000 | [diff] [blame] | 55 | # Relying on built-in importer being implicit. |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 56 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 57 | seconds=seconds): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 58 | yield result |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 59 | |
| 60 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 61 | def source_wo_bytecode(seconds, repeat): |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 62 | """Source w/o bytecode: simple""" |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 63 | sys.dont_write_bytecode = True |
| 64 | try: |
| 65 | name = '__importlib_test_benchmark__' |
| 66 | # Clears out sys.modules and puts an entry at the front of sys.path. |
| 67 | with source_util.create_modules(name) as mapping: |
| 68 | assert not os.path.exists(imp.cache_from_source(mapping[name])) |
| 69 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 70 | seconds=seconds): |
| 71 | yield result |
| 72 | finally: |
| 73 | sys.dont_write_bytecode = False |
| 74 | |
| 75 | |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 76 | def decimal_wo_bytecode(seconds, repeat): |
| 77 | """Source w/o bytecode: decimal""" |
| 78 | name = 'decimal' |
| 79 | decimal_bytecode = imp.cache_from_source(decimal.__file__) |
| 80 | if os.path.exists(decimal_bytecode): |
| 81 | os.unlink(decimal_bytecode) |
| 82 | sys.dont_write_bytecode = True |
| 83 | try: |
| 84 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 85 | seconds=seconds): |
| 86 | yield result |
| 87 | finally: |
| 88 | sys.dont_write_bytecode = False |
| 89 | |
| 90 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 91 | def source_writing_bytecode(seconds, repeat): |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 92 | """Source writing bytecode: simple""" |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 93 | assert not sys.dont_write_bytecode |
| 94 | name = '__importlib_test_benchmark__' |
| 95 | with source_util.create_modules(name) as mapping: |
| 96 | def cleanup(): |
| 97 | sys.modules.pop(name) |
| 98 | os.unlink(imp.cache_from_source(mapping[name])) |
| 99 | for result in bench(name, cleanup, repeat=repeat, seconds=seconds): |
| 100 | assert not os.path.exists(imp.cache_from_source(mapping[name])) |
| 101 | yield result |
| 102 | |
| 103 | |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 104 | def decimal_writing_bytecode(seconds, repeat): |
| 105 | """Source writing bytecode: decimal""" |
| 106 | assert not sys.dont_write_bytecode |
| 107 | name = 'decimal' |
| 108 | def cleanup(): |
| 109 | sys.modules.pop(name) |
| 110 | os.unlink(imp.cache_from_source(decimal.__file__)) |
| 111 | for result in bench(name, cleanup, repeat=repeat, seconds=seconds): |
| 112 | yield result |
| 113 | |
| 114 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 115 | def source_using_bytecode(seconds, repeat): |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 116 | """Bytecode w/ source: simple""" |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 117 | name = '__importlib_test_benchmark__' |
| 118 | with source_util.create_modules(name) as mapping: |
| 119 | py_compile.compile(mapping[name]) |
| 120 | assert os.path.exists(imp.cache_from_source(mapping[name])) |
| 121 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 122 | seconds=seconds): |
| 123 | yield result |
| 124 | |
| 125 | |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 126 | def decimal_using_bytecode(seconds, repeat): |
| 127 | """Bytecode w/ source: decimal""" |
| 128 | name = 'decimal' |
| 129 | py_compile.compile(decimal.__file__) |
| 130 | for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, |
| 131 | seconds=seconds): |
| 132 | yield result |
| 133 | |
| 134 | |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 135 | def main(import_): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 136 | __builtins__.__import__ = import_ |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 137 | benchmarks = (from_cache, builtin_mod, |
| 138 | source_using_bytecode, source_wo_bytecode, |
| 139 | source_writing_bytecode, |
| 140 | decimal_using_bytecode, decimal_writing_bytecode, |
| 141 | decimal_wo_bytecode,) |
| 142 | seconds = 1 |
| 143 | seconds_plural = 's' if seconds > 1 else '' |
| 144 | repeat = 3 |
| 145 | header = "Measuring imports/second over {} second{}, best out of {}\n" |
| 146 | print(header.format(seconds, seconds_plural, repeat)) |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 147 | for benchmark in benchmarks: |
| 148 | print(benchmark.__doc__, "[", end=' ') |
| 149 | sys.stdout.flush() |
| 150 | results = [] |
Brett Cannon | 5db0c94 | 2010-07-22 07:40:56 +0000 | [diff] [blame^] | 151 | for result in benchmark(seconds=seconds, repeat=repeat): |
Brett Cannon | 23cf574 | 2009-09-03 20:45:21 +0000 | [diff] [blame] | 152 | results.append(result) |
| 153 | print(result, end=' ') |
| 154 | sys.stdout.flush() |
Brett Cannon | 3b0a19e | 2010-07-16 19:04:29 +0000 | [diff] [blame] | 155 | assert not sys.dont_write_bytecode |
Brett Cannon | cbe1a4e | 2010-07-16 19:26:23 +0000 | [diff] [blame] | 156 | print("]", "best is", format(max(results), ',d')) |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | import optparse |
| 161 | |
| 162 | parser = optparse.OptionParser() |
| 163 | parser.add_option('-b', '--builtin', dest='builtin', action='store_true', |
| 164 | default=False, help="use the built-in __import__") |
| 165 | options, args = parser.parse_args() |
| 166 | if args: |
Brett Cannon | 7b9bcb8 | 2010-07-15 06:24:04 +0000 | [diff] [blame] | 167 | raise RuntimeError("unrecognized args: {}".format(args)) |
Brett Cannon | 6ae7a7d | 2009-03-30 15:53:01 +0000 | [diff] [blame] | 168 | import_ = __import__ |
| 169 | if not options.builtin: |
| 170 | import_ = importlib.__import__ |
| 171 | |
| 172 | main(import_) |