Rev 4: Benchmark count of functions in a file. in file:///home/robertc/source/baz/plugins/piss/trunk/

Robert Collins robertc at robertcollins.net
Thu May 3 03:37:35 BST 2007


At file:///home/robertc/source/baz/plugins/piss/trunk/

------------------------------------------------------------
revno: 4
revision-id: robertc at robertcollins.net-20070503023734-uqcm2psx0tod27pd
parent: robertc at robertcollins.net-20070501113231-tdboinla71qnr459
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Thu 2007-05-03 12:37:34 +1000
message:
  Benchmark count of functions in a file.
modified:
  benchmarks/bench_import.py     bench_import.py-20070501062416-23ztx65da4ul4oxv-7
  creator.py                     creator.py-20070501064357-lwoaimjvnf8252jd-1
  tests/test_creator.py          test_creator.py-20070501064357-lwoaimjvnf8252jd-4
=== modified file 'benchmarks/bench_import.py'
--- a/benchmarks/bench_import.py	2007-05-01 11:32:31 +0000
+++ b/benchmarks/bench_import.py	2007-05-03 02:37:34 +0000
@@ -31,12 +31,27 @@
         TestCaseWithTransport.setUp(self)
         self.creator = _mod_creator.Creator()
 
+    def time_importing_module(self, num_functions):
+        self.add_cwd_to_path()
+        # import once to generate a .pyc - the common case.
+        self.creator.build_module('tiny_piss')
+        self.import_and_forget('tiny_piss')
+        self.time(self.import_and_forget, 'tiny_piss')
+
     def test_import_tiny_module(self):
-        self.add_cwd_to_path()
-        # import once to generate a .pyc - the common case.
-        self.creator.build_module('tiny_piss')
-        self.import_and_forget('tiny_piss')
-        self.time(self.import_and_forget, 'tiny_piss')
+        self.time_importing_module(0)
+
+    def test_import_10_functions(self):
+        self.time_importing_module(10)
+
+    def test_import_100_functions(self):
+        self.time_importing_module(100)
+
+    def test_import_1000_functions(self):
+        self.time_importing_module(1000)
+
+    def test_import_10000_functions(self):
+        self.time_importing_module(1000)
 
     def test_import_tiny_package(self):
         self.add_cwd_to_path()

=== modified file 'creator.py'
--- a/creator.py	2007-05-01 11:32:31 +0000
+++ b/creator.py	2007-05-03 02:37:34 +0000
@@ -23,12 +23,18 @@
 class Creator(object):
     """A creator of python modules/packages."""
 
-    def build_module(self, name):
-        """Build a module in cwd called name."""
+    def build_module(self, name, num_functions=0):
+        """Build a module in cwd called name.
+        
+        :param num_functions: the number of functions to create. Each function
+            gets a name like fun_X where X is the serial of the function.
+        """
         path = name + '.py'
         output = file(path, 'wt+')
         try:
             try:
+                for fn_index in range(num_functions):
+                    output.write('def fun_%d():pass\n' % fn_index)
                 output.write('')
             finally:
                 output.close()

=== modified file 'tests/test_creator.py'
--- a/tests/test_creator.py	2007-05-01 11:32:31 +0000
+++ b/tests/test_creator.py	2007-05-03 02:37:34 +0000
@@ -18,6 +18,8 @@
 
 """Tests for the python module creator."""
 
+import os
+import sys
 
 from bzrlib.plugins.piss import creator as _mod_creator
 from bzrlib.tests import TestCaseWithTransport
@@ -25,6 +27,18 @@
 
 class TestCreator(TestCaseWithTransport):
 
+    def add_cwd_to_path(self):
+        # FIXME : duplicated in the bench module.
+        sys.path.append(os.getcwd())
+        self.addCleanup(lambda: sys.path.pop(-1))
+
+    def import_and_forget(self, module_name):
+        # FIXME : duplicated in the bench module.
+        try:
+            exec 'import %s' % module_name
+        finally:
+            return sys.modules.pop(module_name)
+
     def test_construct(self):
         _mod_creator.Creator()
 
@@ -34,6 +48,16 @@
         creator.build_module('foo')
         self.assertEqual('', self.get_transport().get_bytes('foo.py'))
 
+    def test_build_module_function_count(self):
+        # building a module with 100 functions should have functions
+        # fun_0..fun_99
+        creator = _mod_creator.Creator()
+        creator.build_module('foo', num_functions=100)
+        self.add_cwd_to_path()
+        mod_foo = self.import_and_forget('foo')
+        for fn_index in range(100):
+            self.assertTrue(callable(getattr(mod_foo, 'fun_%d' % fn_index)))
+
     def test_build_package(self):
         # building a package called 'foo' should make a foo/__init__.py
         creator = _mod_creator.Creator()



More information about the bazaar-commits mailing list