Commit 466e6a90 authored by Brett Cannon's avatar Brett Cannon

Have importlib.test.benchmark test with tabnanny as a medium-sized test.

parent 9878b63c
...@@ -13,6 +13,7 @@ import json ...@@ -13,6 +13,7 @@ import json
import os import os
import py_compile import py_compile
import sys import sys
import tabnanny
import timeit import timeit
...@@ -60,7 +61,7 @@ def builtin_mod(seconds, repeat): ...@@ -60,7 +61,7 @@ def builtin_mod(seconds, repeat):
def source_wo_bytecode(seconds, repeat): def source_wo_bytecode(seconds, repeat):
"""Source w/o bytecode: simple""" """Source w/o bytecode: small"""
sys.dont_write_bytecode = True sys.dont_write_bytecode = True
try: try:
name = '__importlib_test_benchmark__' name = '__importlib_test_benchmark__'
...@@ -74,23 +75,30 @@ def source_wo_bytecode(seconds, repeat): ...@@ -74,23 +75,30 @@ def source_wo_bytecode(seconds, repeat):
sys.dont_write_bytecode = False sys.dont_write_bytecode = False
def decimal_wo_bytecode(seconds, repeat): def _wo_bytecode(module):
"""Source w/o bytecode: decimal""" name = module.__name__
name = 'decimal' def benchmark_wo_bytecode(seconds, repeat):
decimal_bytecode = imp.cache_from_source(decimal.__file__) """Source w/o bytecode: {}"""
if os.path.exists(decimal_bytecode): bytecode_path = imp.cache_from_source(module.__file__)
os.unlink(decimal_bytecode) if os.path.exists(bytecode_path):
sys.dont_write_bytecode = True os.unlink(bytecode_path)
try: sys.dont_write_bytecode = True
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, try:
seconds=seconds): for result in bench(name, lambda: sys.modules.pop(name),
yield result repeat=repeat, seconds=seconds):
finally: yield result
sys.dont_write_bytecode = False finally:
sys.dont_write_bytecode = False
benchmark_wo_bytecode.__doc__ = benchmark_wo_bytecode.__doc__.format(name)
return benchmark_wo_bytecode
tabnanny_wo_bytecode = _wo_bytecode(tabnanny)
decimal_wo_bytecode = _wo_bytecode(decimal)
def source_writing_bytecode(seconds, repeat): def source_writing_bytecode(seconds, repeat):
"""Source writing bytecode: simple""" """Source writing bytecode: small"""
assert not sys.dont_write_bytecode assert not sys.dont_write_bytecode
name = '__importlib_test_benchmark__' name = '__importlib_test_benchmark__'
with source_util.create_modules(name) as mapping: with source_util.create_modules(name) as mapping:
...@@ -102,19 +110,27 @@ def source_writing_bytecode(seconds, repeat): ...@@ -102,19 +110,27 @@ def source_writing_bytecode(seconds, repeat):
yield result yield result
def decimal_writing_bytecode(seconds, repeat): def _writing_bytecode(module):
"""Source writing bytecode: decimal""" name = module.__name__
assert not sys.dont_write_bytecode def writing_bytecode_benchmark(seconds, repeat):
name = 'decimal' """Source writing bytecode: {}"""
def cleanup(): assert not sys.dont_write_bytecode
sys.modules.pop(name) def cleanup():
os.unlink(imp.cache_from_source(decimal.__file__)) sys.modules.pop(name)
for result in bench(name, cleanup, repeat=repeat, seconds=seconds): os.unlink(imp.cache_from_source(module.__file__))
yield result for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
yield result
writing_bytecode_benchmark.__doc__ = (
writing_bytecode_benchmark.__doc__.format(name))
return writing_bytecode_benchmark
tabnanny_writing_bytecode = _writing_bytecode(tabnanny)
decimal_writing_bytecode = _writing_bytecode(decimal)
def source_using_bytecode(seconds, repeat): def source_using_bytecode(seconds, repeat):
"""Bytecode w/ source: simple""" """Source w/ bytecode: small"""
name = '__importlib_test_benchmark__' name = '__importlib_test_benchmark__'
with source_util.create_modules(name) as mapping: with source_util.create_modules(name) as mapping:
py_compile.compile(mapping[name]) py_compile.compile(mapping[name])
...@@ -124,13 +140,21 @@ def source_using_bytecode(seconds, repeat): ...@@ -124,13 +140,21 @@ def source_using_bytecode(seconds, repeat):
yield result yield result
def decimal_using_bytecode(seconds, repeat): def _using_bytecode(module):
"""Bytecode w/ source: decimal""" name = module.__name__
name = 'decimal' def using_bytecode_benchmark(seconds, repeat):
py_compile.compile(decimal.__file__) """Source w/ bytecode: {}"""
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat, py_compile.compile(module.__file__)
seconds=seconds): for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
yield result seconds=seconds):
yield result
using_bytecode_benchmark.__doc__ = (
using_bytecode_benchmark.__doc__.format(name))
return using_bytecode_benchmark
tabnanny_using_bytecode = _using_bytecode(tabnanny)
decimal_using_bytecode = _using_bytecode(decimal)
def main(import_, filename=None, benchmark=None): def main(import_, filename=None, benchmark=None):
...@@ -143,6 +167,8 @@ def main(import_, filename=None, benchmark=None): ...@@ -143,6 +167,8 @@ def main(import_, filename=None, benchmark=None):
benchmarks = (from_cache, builtin_mod, benchmarks = (from_cache, builtin_mod,
source_using_bytecode, source_wo_bytecode, source_using_bytecode, source_wo_bytecode,
source_writing_bytecode, source_writing_bytecode,
tabnanny_using_bytecode, tabnanny_wo_bytecode,
tabnanny_writing_bytecode,
decimal_using_bytecode, decimal_writing_bytecode, decimal_using_bytecode, decimal_writing_bytecode,
decimal_wo_bytecode,) decimal_wo_bytecode,)
if benchmark: if benchmark:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment