[Python-checkins] r53093 - sandbox/trunk/import_in_py/importbench.py
brett.cannon
python-checkins at python.org
Wed Dec 20 02:46:51 CET 2006
Author: brett.cannon
Date: Wed Dec 20 02:46:50 2006
New Revision: 53093
Modified:
sandbox/trunk/import_in_py/importbench.py
Log:
Test the importation of a package.
Modified: sandbox/trunk/import_in_py/importbench.py
==============================================================================
--- sandbox/trunk/import_in_py/importbench.py (original)
+++ sandbox/trunk/import_in_py/importbench.py Wed Dec 20 02:46:50 2006
@@ -20,6 +20,7 @@
"""
from py_compile import compile as compile_to_pyc
import os
+from shutil import rmtree
import StringIO
import sys
import tempfile
@@ -72,14 +73,18 @@
self.directory = tempfile.gettempdir()
self.py_path = os.path.join(self.directory, self.module_name + '.py')
self.pyc_path = self.py_path + ('c' if __debug__ else 'o')
- with open(self.py_path, 'w') as py_file:
- py_file.write("# Temporary file for benchmarking import.")
- if self.pyc:
- compile_to_pyc(self.py_path, doraise=True)
- if not self.py:
- os.remove(self.py_path)
- sys.path.append(self.directory)
- return self
+ try:
+ with open(self.py_path, 'w') as py_file:
+ py_file.write("# Temporary file for benchmarking import.")
+ if self.pyc:
+ compile_to_pyc(self.py_path, doraise=True)
+ if not self.py:
+ os.remove(self.py_path)
+ sys.path.append(self.directory)
+ return self
+ except Exception:
+ self.__exit__()
+ raise
def __exit__(self, *args):
"""Clean up created state."""
@@ -87,6 +92,40 @@
os.remove(self.py_path)
if os.path.exists(self.pyc_path):
os.remove(self.pyc_path)
+
+class PyPycPackage(object):
+
+ """Create .py files for testing the importing of packages."""
+
+ def __init__(self, pkg_name="testpkg", module_name="testmod"):
+ """Specify the names for the package and module within the package."""
+ self.pkg_name = pkg_name
+ self.module_name = module_name
+
+ def __enter__(self):
+ """Create the package and module."""
+ self.directory = tempfile.gettempdir()
+ self.pkg_path = os.path.join(self.directory, self.pkg_name)
+ os.mkdir(self.pkg_path)
+ try:
+ self.pkg_init_path = os.path.join(self.pkg_path, '__init__.py')
+ with open(self.pkg_init_path, 'w') as init_file:
+ init_file.write('# This file is used for benchmarking package '
+ 'imports')
+ self.full_module_name = self.pkg_name + '.' + self.module_name
+ self.module_path = os.path.join(self.pkg_path, self.module_name+'.py')
+ with open(self.module_path, 'w') as module_file:
+ module_file.write('# This file is used to benchmark importing '
+ 'modules in a package.')
+ sys.path.append(self.directory)
+ return self
+ except Exception:
+ self.__exit__()
+ raise
+
+ def __exit__(self, *args):
+ """Cleanup our mess."""
+ rmtree(self.pkg_path)
@save_import_state
@@ -171,7 +210,8 @@
@save_import_state
def bench_long_sys_path(repetitions, iterations, extra_path_entries=20):
- """See the impact of having a large number of entries on sys.path."""
+ """See the impact of having a large number of entries on sys.path that do
+ not have the module."""
def succeed_hook(path):
class FailImporter(object):
def find_module(name, path=None):
@@ -184,6 +224,16 @@
sys.path.insert(0, str(entry)+'dummy_entry')
timer = import_and_clear_timer(file_state.module_name)
return timer.repeat(repetitions, iterations)
+
+ at save_import_state
+def bench_package(repetitions, iterations):
+ """Benchmark the importing of a package."""
+ sys.path = []
+ sys.meta_path = []
+ with PyPycPackage() as pkg_details:
+ timer = import_and_clear_timer(pkg_details.pkg_name)
+ return timer.repeat(repetitions, iterations)
+
def display_results(fxn, name, spacing, repetitions, iterations):
More information about the Python-checkins
mailing list