[Zope3-checkins] SVN: zope.testing/trunk/src/zope/testing/testrunner Added gc control and statistics options.

Jim Fulton jim at zope.com
Fri Oct 21 18:55:07 EDT 2005


Log message for revision 39554:
  Added gc control and statistics options.
  

Changed:
  A   zope.testing/trunk/src/zope/testing/testrunner-ex/gc0.py
  A   zope.testing/trunk/src/zope/testing/testrunner-ex/gc1.py
  A   zope.testing/trunk/src/zope/testing/testrunner-ex/gcset.py
  A   zope.testing/trunk/src/zope/testing/testrunner-ex/gcstats.py
  A   zope.testing/trunk/src/zope/testing/testrunner-gc.txt
  U   zope.testing/trunk/src/zope/testing/testrunner.py
  U   zope.testing/trunk/src/zope/testing/testrunner.txt

-=-
Added: zope.testing/trunk/src/zope/testing/testrunner-ex/gc0.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex/gc0.py	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex/gc0.py	2005-10-21 22:55:07 UTC (rev 39554)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+import unittest
+from zope.testing import doctest
+
+def make_sure_gc_is_disabled():
+    """
+    >>> import gc
+    >>> gc.get_threshold()[0]
+    0
+    """
+
+def test_suite():
+    return doctest.DocTestSuite()
+


Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex/gc0.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zope.testing/trunk/src/zope/testing/testrunner-ex/gc1.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex/gc1.py	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex/gc1.py	2005-10-21 22:55:07 UTC (rev 39554)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+import unittest
+from zope.testing import doctest
+
+def make_sure_gc_threshold_is_one():
+    """
+    >>> import gc
+    >>> gc.get_threshold()[0]
+    1
+    """
+
+def test_suite():
+    return doctest.DocTestSuite()
+


Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex/gc1.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zope.testing/trunk/src/zope/testing/testrunner-ex/gcset.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex/gcset.py	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex/gcset.py	2005-10-21 22:55:07 UTC (rev 39554)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+import unittest
+from zope.testing import doctest
+
+def make_sure_gc_threshold_is_701_11_9():
+    """
+    >>> import gc
+    >>> gc.get_threshold()
+    (701, 11, 9)
+    """
+
+def test_suite():
+    return doctest.DocTestSuite()
+


Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex/gcset.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zope.testing/trunk/src/zope/testing/testrunner-ex/gcstats.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-ex/gcstats.py	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner-ex/gcstats.py	2005-10-21 22:55:07 UTC (rev 39554)
@@ -0,0 +1,26 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+import unittest
+from zope.testing import doctest
+
+def generate_some_gc_statistics():
+    """
+    >>> import gc
+    >>> l = []; l.append(l); del l
+    >>> _ = gc.collect()
+    """
+
+def test_suite():
+    return doctest.DocTestSuite()
+


Property changes on: zope.testing/trunk/src/zope/testing/testrunner-ex/gcstats.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zope.testing/trunk/src/zope/testing/testrunner-gc.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-gc.txt	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner-gc.txt	2005-10-21 22:55:07 UTC (rev 39554)
@@ -0,0 +1,85 @@
+Test Runner
+===========
+
+Garbage Collection Control
+--------------------------
+
+When having problems that seem to be caused my memory-management
+errors, it can be helpful to adjust Python's cyclic garbage collector
+or to get garbage colection statistics.  The --gc option can be used
+for this purpose.
+
+If you think you are getting a test failure due to a garbage
+collection problem, you can try disabling garbage collection by
+using the --gc option with a value of zero.
+
+    >>> import os.path, sys
+    >>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex')
+    >>> defaults = ['--path', directory_with_tests]
+
+    >>> from zope.testing import testrunner
+    
+    >>> sys.argv = 'test --tests-pattern ^gc0$ --gc 0 -vv'.split()
+    >>> _ = testrunner.run(defaults)
+    Running tests at level 1
+    Cyclic garbage collection is disabled.
+    Running unit tests:
+      Running:
+        make_sure_gc_is_disabled (gc0)
+      Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
+
+Alternatively, if you think you are having a garbage collection
+related problem, you can cause garbage collection to happen more often
+by providing a low threshold:
+    
+    >>> sys.argv = 'test --tests-pattern ^gc1$ --gc 1 -vv'.split()
+    >>> _ = testrunner.run(defaults)
+    Running tests at level 1
+    Cyclic garbage collection threshold set to: (1,)
+    Running unit tests:
+      Running:
+        make_sure_gc_threshold_is_one (gc1)
+      Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
+
+You can specify up to 3 --gc options to set each of the 3 gc threshold
+values:
+
+    
+    >>> sys.argv = ('test --tests-pattern ^gcset$ --gc 701 --gc 11 --gc 9 -vv'
+    ...             .split())
+    >>> _ = testrunner.run(defaults)
+    Running tests at level 1
+    Cyclic garbage collection threshold set to: (701, 11, 9)
+    Running unit tests:
+      Running:
+        make_sure_gc_threshold_is_701_11_9 (gcset)
+      Ran 1 tests with 0 failures and 0 errors in 0.001 seconds.
+
+Garbage Collection Statistics
+-----------------------------
+
+You can enable gc debugging statistics using the --gc-options (-G)
+option.  You should provide names of one or more of the flags
+described in the library documentation for the gc module.
+
+The output statistics are written to standard error.  
+
+    >>> from StringIO import StringIO
+    >>> err = StringIO()
+    >>> stderr = sys.stderr
+    >>> sys.stderr = err
+    >>> sys.argv = ('test --tests-pattern ^gcstats$ -G DEBUG_STATS'
+    ...             ' -G DEBUG_COLLECTABLE -vv'
+    ...             .split())
+    >>> _ = testrunner.run(defaults)
+    Running tests at level 1
+    Running unit tests:
+      Running:
+        generate_some_gc_statistics (gcstats)
+      Ran 1 tests with 0 failures and 0 errors in 0.006 seconds.
+
+    >>> sys.stderr = stderr
+
+    >>> print err.getvalue()        # doctest: +ELLIPSIS
+    gc: collecting generation ...
+        


Property changes on: zope.testing/trunk/src/zope/testing/testrunner-gc.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: zope.testing/trunk/src/zope/testing/testrunner.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.py	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner.py	2005-10-21 22:55:07 UTC (rev 39554)
@@ -238,8 +238,26 @@
             print "Running tests at level %d" % options.at_level
 
 
-    # XXX add tracing later
+    old_threshold = gc.get_threshold()
+    if options.gc:
+        if len(options.gc) > 3:
+            print >> sys.stderr, "Too many --gc options"
+            sys.exit(1)
+        if options.gc[0]:
+            print ("Cyclic garbage collection threshold set to: %s" %
+                   `tuple(options.gc)`)
+        else:
+            print "Cyclic garbage collection is disabled."
+            
+        gc.set_threshold(*options.gc)
 
+    old_flags = gc.get_debug()
+    if options.gc_option:
+        new_flags = 0
+        for op in options.gc_option:
+            new_flags |= getattr(gc, op)
+        gc.set_debug(new_flags)
+
     # Add directories to the path
     for path in options.path:
         if path not in sys.path:
@@ -347,6 +365,11 @@
             for test in import_errors:
                 print "  " + test.module
 
+    if options.gc_option:
+        gc.set_debug(old_flags)
+        
+    if options.gc:
+        gc.set_threshold(*old_threshold)
 
     return bool(import_errors or failures or errors)
 
@@ -1106,25 +1129,30 @@
     )
 
 
-def gc_callback(option, opt, GC_THRESHOLD, *args):
-    import gc
-    if GC_THRESHOLD == 0:
-        gc.disable()
-        print "gc disabled"
-    else:
-        gc.set_threshold(GC_THRESHOLD)
-        print "gc threshold:", gc.get_threshold()
-
 analysis.add_option(
-    '--gc', action="callback", callback=gc_callback, dest='gc', type="int",
+    '--gc', '-g', action="append", dest='gc', type="int",
     help="""\
-Set the garbage collector generation0 threshold.  This can be used
+Set the garbage collector generation threshold.  This can be used
 to stress memory and gc correctness.  Some crashes are only
 reproducible when the threshold is set to 1 (agressive garbage
 collection).  Do "--gc 0" to disable garbage collection altogether.
+
+The --gc option can be used up to 3 times to specify up to 3 of the 3
+Python gc_threshold settings.
+
 """)
 
 analysis.add_option(
+    '--gc-option', '-G', action="append", dest='gc_option', type="choice",
+    choices=['DEBUG_STATS', 'DEBUG_COLLECTABLE', 'DEBUG_UNCOLLECTABLE',
+             'DEBUG_INSTANCES', 'DEBUG_OBJECTS', 'DEBUG_SAVEALL',
+             'DEBUG_LEAK'],
+    help="""\
+Set a Python gc-module debug flag.  This option can be used more than
+once to set multiple flags.
+""")
+
+analysis.add_option(
     '--repeat', action="store", type="int", dest='repeat',
     help="""\
 Repeat the testst the given number of times.  This option is used to
@@ -1414,13 +1442,15 @@
         test.globs['saved-sys-info'] = (
             sys.path[:],
             sys.argv[:],
-            sys.modules.copy()
+            sys.modules.copy(),
+            gc.get_threshold()
             )
         test.globs['this_directory'] = os.path.split(__file__)[0]
         test.globs['testrunner_script'] = __file__
 
     def tearDown(test):
         sys.path[:], sys.argv[:] = test.globs['saved-sys-info'][:2]
+        gc.set_threshold(*test.globs['saved-sys-info'][3])
         sys.modules.clear()
         sys.modules.update(test.globs['saved-sys-info'][2])
 
@@ -1438,6 +1468,7 @@
         'testrunner-verbose.txt',
         'testrunner-wo-source.txt',
         'testrunner-repeat.txt',
+        'testrunner-gc.txt',
         setUp=setUp, tearDown=tearDown,
         optionflags=doctest.ELLIPSIS+doctest.NORMALIZE_WHITESPACE,
         checker=checker)

Modified: zope.testing/trunk/src/zope/testing/testrunner.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.txt	2005-10-21 17:53:46 UTC (rev 39553)
+++ zope.testing/trunk/src/zope/testing/testrunner.txt	2005-10-21 22:55:07 UTC (rev 39554)
@@ -82,4 +82,5 @@
 - `Code Coverage <testrunner-coverage.txt>`_
 - `Running Without Source Code <testrunner-wo-source.txt>`_
 - `Repeating Tests <testrunner-looping.txt>`_
+- `Garbage Collection Control and Statistics <testrunner-gc.txt>`_
 - `Edge Cases <testrunner-edge-cases.txt>`_



More information about the Zope3-Checkins mailing list