2009-09-03 17:45:21 -03:00
|
|
|
"""Benchmark some basic import use-cases."""
|
|
|
|
# XXX
|
2010-07-15 03:24:04 -03:00
|
|
|
# - from source
|
|
|
|
# + sys.dont_write_bytecode = True
|
|
|
|
# + sys.dont_write_bytecode = False
|
|
|
|
# - from bytecode
|
|
|
|
# - extensions
|
2009-03-30 12:53:01 -03:00
|
|
|
from . import util
|
|
|
|
from .source import util as source_util
|
|
|
|
import imp
|
|
|
|
import importlib
|
|
|
|
import sys
|
|
|
|
import timeit
|
|
|
|
|
|
|
|
|
2009-09-03 17:45:21 -03:00
|
|
|
def bench(name, cleanup=lambda: None, *, seconds=1, repeat=3):
|
|
|
|
"""Bench the given statement as many times as necessary until total
|
|
|
|
executions take one second."""
|
|
|
|
stmt = "__import__({!r})".format(name)
|
|
|
|
timer = timeit.Timer(stmt)
|
|
|
|
for x in range(repeat):
|
|
|
|
total_time = 0
|
|
|
|
count = 0
|
|
|
|
while total_time < seconds:
|
|
|
|
try:
|
|
|
|
total_time += timer.timeit(1)
|
|
|
|
finally:
|
|
|
|
cleanup()
|
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
# One execution too far
|
|
|
|
if total_time > seconds:
|
|
|
|
count -= 1
|
2010-07-15 03:24:04 -03:00
|
|
|
yield count // seconds
|
2009-09-03 17:45:21 -03:00
|
|
|
|
|
|
|
def from_cache(repeat):
|
|
|
|
"""sys.modules"""
|
2009-03-30 12:53:01 -03:00
|
|
|
name = '<benchmark import>'
|
2009-09-03 17:45:21 -03:00
|
|
|
module = imp.new_module(name)
|
|
|
|
module.__file__ = '<test>'
|
|
|
|
module.__package__ = ''
|
2009-03-30 12:53:01 -03:00
|
|
|
with util.uncache(name):
|
|
|
|
sys.modules[name] = module
|
2009-09-03 17:45:21 -03:00
|
|
|
for result in bench(name, repeat=repeat):
|
|
|
|
yield result
|
2009-03-30 12:53:01 -03:00
|
|
|
|
|
|
|
|
2009-09-03 17:45:21 -03:00
|
|
|
def builtin_mod(repeat):
|
|
|
|
"""Built-in module"""
|
|
|
|
name = 'errno'
|
|
|
|
if name in sys.modules:
|
|
|
|
del sys.modules[name]
|
2010-07-15 03:24:04 -03:00
|
|
|
# Relying on built-in importer being implicit.
|
2009-09-03 17:45:21 -03:00
|
|
|
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat):
|
|
|
|
yield result
|
2009-03-30 12:53:01 -03:00
|
|
|
|
|
|
|
|
2010-07-15 03:24:04 -03:00
|
|
|
def main(import_, *, repeat=3):
|
2009-09-03 17:45:21 -03:00
|
|
|
__builtins__.__import__ = import_
|
|
|
|
benchmarks = from_cache, builtin_mod
|
2010-07-15 03:24:04 -03:00
|
|
|
print("Measuring imports/second\n")
|
2009-09-03 17:45:21 -03:00
|
|
|
for benchmark in benchmarks:
|
|
|
|
print(benchmark.__doc__, "[", end=' ')
|
|
|
|
sys.stdout.flush()
|
|
|
|
results = []
|
|
|
|
for result in benchmark(repeat):
|
|
|
|
results.append(result)
|
|
|
|
print(result, end=' ')
|
|
|
|
sys.stdout.flush()
|
|
|
|
print("]", "best is", max(results))
|
2009-03-30 12:53:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import optparse
|
|
|
|
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-b', '--builtin', dest='builtin', action='store_true',
|
|
|
|
default=False, help="use the built-in __import__")
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
if args:
|
2010-07-15 03:24:04 -03:00
|
|
|
raise RuntimeError("unrecognized args: {}".format(args))
|
2009-03-30 12:53:01 -03:00
|
|
|
import_ = __import__
|
|
|
|
if not options.builtin:
|
|
|
|
import_ = importlib.__import__
|
|
|
|
|
|
|
|
main(import_)
|