[Zope3-checkins] SVN: zope.testing/trunk/src/zope/testing/ fixed bug in profiling with layers in subprocesses (with tests)

Benji York benji at zope.com
Wed Aug 24 16:23:19 EDT 2005


Log message for revision 38078:
  fixed bug in profiling with layers in subprocesses (with tests)
  

Changed:
  U   zope.testing/trunk/src/zope/testing/profiling.txt
  U   zope.testing/trunk/src/zope/testing/testrunner.py

-=-
Modified: zope.testing/trunk/src/zope/testing/profiling.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/profiling.txt	2005-08-24 19:39:43 UTC (rev 38077)
+++ zope.testing/trunk/src/zope/testing/profiling.txt	2005-08-24 20:23:18 UTC (rev 38078)
@@ -13,7 +13,7 @@
     ...     '--tests-pattern', '^sampletestsf?$',
     ...     ]
 
-    >>> sys.argv = 'test --profile'.split()
+    >>> sys.argv = [testrunner_script, '--profile']
 
 When the tests are run, we get profiling output.
 
@@ -27,8 +27,12 @@
     ...
     Total: ... tests, 0 failures, 0 errors
     ...
-       Ordered by: cumulative time, call count
-       List reduced from ... to 50 due to restriction <50>
-    <BLANKLINE>
-       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
-    ...
+       ncalls  tottime  percall  cumtime  percall filename:lineno(function)...
+
+Profiling also works across layers.
+
+    >>> sys.argv = [testrunner_script, '-ssample2', '--profile', '--tests-pattern', 'sampletests_ntd']
+    >>> testrunner.run(defaults)
+    Running...
+      Tear down ... not supported...
+       ncalls  tottime  percall  cumtime  percall filename:lineno(function)...

Modified: zope.testing/trunk/src/zope/testing/testrunner.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.py	2005-08-24 19:39:43 UTC (rev 38077)
+++ zope.testing/trunk/src/zope/testing/testrunner.py	2005-08-24 20:23:18 UTC (rev 38078)
@@ -119,42 +119,46 @@
     else:
         tracer = None
 
-    try:
-        if options.profile:
-            prof_prefix = 'tests_profile'
-            prof_suffix = '.prof'
-            prof_glob = prof_prefix + '*' + prof_suffix
-            dummy, file_path = tempfile.mkstemp(prof_suffix, prof_prefix, '.')
-            prof = hotshot.Profile(file_path)
-            prof.start()
+    if options.profile:
+        prof_prefix = 'tests_profile'
+        prof_suffix = '.prof'
+        prof_glob = prof_prefix + '*' + prof_suffix
 
-        try:
-            try:
-                failed = run_with_options(options)
-            except EndRun:
-                failed = True
-        finally:
-            if tracer:
-                tracer.stop()
-            if options.profile:                         
-                prof.stop()
-                prof.close()
-
-        if options.profile:
-            stats = None
+        # if we are going to be profiling, and this isn't a subprocess,
+        # clean up any stale results files
+        if not options.resume_layer:
             for file_name in glob.glob(prof_glob):
-                loaded = hotshot.stats.load(file_name)
-                if stats is None:
-                    stats = loaded
-                else:
-                    stats.add(loaded)
-            stats.sort_stats('cumulative', 'calls')
-            stats.print_stats(50)
-    finally:
-        if options.profile:
-            for file_name in glob.glob(prof_glob):
                 os.unlink(file_name)
+        
+        # set up the output file
+        dummy, file_path = tempfile.mkstemp(prof_suffix, prof_prefix, '.')
+        prof = hotshot.Profile(file_path)
+        prof.start()
 
+    try:
+        try:
+            failed = run_with_options(options)
+        except EndRun:
+            failed = True
+    finally:
+        if tracer:
+            tracer.stop()
+        if options.profile:                         
+            prof.stop()
+            prof.close()
+
+    if options.profile and not options.resume_layer:
+        stats = None
+        for file_name in glob.glob(prof_glob):
+            loaded = hotshot.stats.load(file_name)
+            if stats is None:
+                stats = loaded
+            else:
+                stats.add(loaded)
+        stats.sort_stats('cumulative', 'calls')
+#        stats.sort_stats('time', 'calls')
+        stats.print_stats(50)
+
     if tracer:
         coverdir = os.path.join(os.getcwd(), options.coverage)
         r = tracer.results()
@@ -365,6 +369,10 @@
             for a in args[1:]
             ])
 
+    # this is because of a bug in Python (http://www.python.org/sf/900092)
+    if options.profile and sys.version_info[:3] <= (2,4,1):
+        args.insert(1, '-O')
+
     subin, subout, suberr = os.popen3(args)
     for l in subout:
         sys.stdout.write(l)



More information about the Zope3-Checkins mailing list