changeset 2419:123974b5c8b5

added cProfile support to standalone, thanks to rayvd
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 18 Jul 2007 09:15:28 +0200
parents 78d745b57730
children d268d5467cfe
files MoinMoin/server/server_standalone.py moin.py
diffstat 2 files changed, 30 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/server/server_standalone.py	Sun Jul 15 13:18:58 2007 +0200
+++ b/MoinMoin/server/server_standalone.py	Wed Jul 18 09:15:28 2007 +0200
@@ -77,6 +77,9 @@
         if config.hotshotProfile:
             config.hotshotProfile.close()
 
+        if config.cProfile and config.cProfileProfile:
+            config.cProfile.dump_stats(config.cProfileProfile)
+
         # Set abort flag, then make request to wake the server
         self._abort = 1
         try:
@@ -561,6 +564,18 @@
     hotshotProfile = None
     pycallgraph_output = None
 
+def cProfileDecorator(func, profile):
+    """ Return a profiled function """
+    profile.moin_requests_done = 0
+    def profiledFunction(*args, **kw):
+        profile.moin_requests_done += 1
+        if profile.moin_requests_done == 1:
+            # Don't profile first request, it's not interesting
+            return func(*args, **kw)
+        return profile.runcall(func, *args, **kw)
+
+    return profiledFunction
+
 def run(configClass):
     """ Create and run a moin server
 
@@ -575,6 +590,9 @@
 
     config = configClass()
 
+    if config.hotshotProfile and config.cProfileProfile:
+        raise RuntimeError("You cannot run two profilers simultaneously.")
+
     # Install hotshot profiled serve_moin method. To compare with other
     # servers, we profile the part that create and run the request.
     if config.hotshotProfile:
@@ -583,6 +601,14 @@
         MoinRequestHandler.serve_moin = hotshotProfileDecorator(
             MoinRequestHandler.serve_moin, config.hotshotProfile)
 
+    if config.cProfileProfile:
+        import cProfile
+        # Create a new cProfile.Profile object using config.cProfileProfile
+        # as the path for the output file.
+        config.cProfile = cProfile.Profile()
+        MoinRequestHandler.serve_moin = cProfileDecorator(
+            MoinRequestHandler.serve_moin, config.cProfile)
+
     # Install a memory profiled serve_moin method
     if config.memoryProfile:
         config.memoryProfile.sample()
--- a/moin.py	Sun Jul 15 13:18:58 2007 +0200
+++ b/moin.py	Wed Jul 18 09:15:28 2007 +0200
@@ -127,6 +127,10 @@
     # Not compatible with threads - use with SimpleServer only.
     ## hotshotProfile = name + '.prof'
 
+    # cProfile profile (default commented)
+    # Use with SimpleServer only.
+    ## cProfileProfile = name + '.prof'
+
     # Using pycallgraph to make nice graphics of how moin works internally
     # hint: using zgrviewer to view .dot is much more effective than using
     #       some .png viewer