blob: cd2a8c0ef0e7cbf382fa394efef29017e04ae7d4 [file] [log] [blame]
Brett Cannon23cf5742009-09-03 20:45:21 +00001"""Benchmark some basic import use-cases."""
2# XXX
Brett Cannon7b9bcb82010-07-15 06:24:04 +00003# - from source
4# + sys.dont_write_bytecode = True
5# + sys.dont_write_bytecode = False
6# - from bytecode
7# - extensions
Brett Cannon6ae7a7d2009-03-30 15:53:01 +00008from . import util
9from .source import util as source_util
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000010import imp
11import importlib
12import sys
13import timeit
14
15
Brett Cannon23cf5742009-09-03 20:45:21 +000016def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3):
17 """Bench the given statement as many times as necessary until total
18 executions take one second."""
19 stmt = "__import__({!r})".format(name)
20 timer = timeit.Timer(stmt)
21 for x in range(repeat):
22 total_time = 0
23 count = 0
24 while total_time < seconds:
25 try:
26 total_time += timer.timeit(1)
27 finally:
28 cleanup()
29 count += 1
30 else:
31 # One execution too far
32 if total_time > seconds:
33 count -= 1
Brett Cannon7b9bcb82010-07-15 06:24:04 +000034 yield count // seconds
Brett Cannon23cf5742009-09-03 20:45:21 +000035
36def from_cache(repeat):
37 """sys.modules"""
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000038 name = '<benchmark import>'
Brett Cannon23cf5742009-09-03 20:45:21 +000039 module = imp.new_module(name)
40 module.__file__ = '<test>'
41 module.__package__ = ''
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000042 with util.uncache(name):
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000043 sys.modules[name] = module
Brett Cannon23cf5742009-09-03 20:45:21 +000044 for result in bench(name, repeat=repeat):
45 yield result
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000046
47
Brett Cannon23cf5742009-09-03 20:45:21 +000048def builtin_mod(repeat):
49 """Built-in module"""
50 name = 'errno'
51 if name in sys.modules:
52 del sys.modules[name]
Brett Cannon7b9bcb82010-07-15 06:24:04 +000053 # Relying on built-in importer being implicit.
Brett Cannon23cf5742009-09-03 20:45:21 +000054 for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat):
55 yield result
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000056
57
Brett Cannon7b9bcb82010-07-15 06:24:04 +000058def main(import_, *, repeat=3):
Brett Cannon23cf5742009-09-03 20:45:21 +000059 __builtins__.__import__ = import_
60 benchmarks = from_cache, builtin_mod
Brett Cannon7b9bcb82010-07-15 06:24:04 +000061 print("Measuring imports/second\n")
Brett Cannon23cf5742009-09-03 20:45:21 +000062 for benchmark in benchmarks:
63 print(benchmark.__doc__, "[", end=' ')
64 sys.stdout.flush()
65 results = []
66 for result in benchmark(repeat):
67 results.append(result)
68 print(result, end=' ')
69 sys.stdout.flush()
70 print("]", "best is", max(results))
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000071
72
73if __name__ == '__main__':
74 import optparse
75
76 parser = optparse.OptionParser()
77 parser.add_option('-b', '--builtin', dest='builtin', action='store_true',
78 default=False, help="use the built-in __import__")
79 options, args = parser.parse_args()
80 if args:
Brett Cannon7b9bcb82010-07-15 06:24:04 +000081 raise RuntimeError("unrecognized args: {}".format(args))
Brett Cannon6ae7a7d2009-03-30 15:53:01 +000082 import_ = __import__
83 if not options.builtin:
84 import_ = importlib.__import__
85
86 main(import_)