CherryPy / lib /

"""Profiler tools for CherryPy.

CherryPy users

You can profile any of your pages as follows:

    from cherrypy.lib import profile
    class Root:
        p = profile.Profiler("/path/to/profile/dir")
        def index(self):
   = True
        def _index(self):
            return "Hello, world!"

You can also turn on profiling for all requests
using the make_app function as WSGI middleware.

CherryPy developers

This module can be used whenever you make changes to CherryPy,
to get a quick sanity-check on overall CP performance. Use the
"--profile" flag when running the test suite. Then, use the serve()
function to browse the results in a web browser. If you run this
module from the command line, it will call serve() for you.


# Make profiler output more readable by adding __init__ modules' parents.
def new_func_strip_path(func_name):
    filename, line, name = func_name
    if filename.endswith(""):
        return os.path.basename(filename[:-12]) + filename[-12:], line, name
    return os.path.basename(filename), line, name

    import profile
    import pstats
    pstats.func_strip_path = new_func_strip_path
except ImportError:
    profile = None
    pstats = None
    import warnings
    msg = ("Your installation of Python doesn't have a profile module. "
           "If you're on Debian, you can apt-get python2.4-profiler from "
           "non-free in a separate step. See"
           "ProfilingOnDebian for details.")

import os, os.path
import sys

    import cStringIO as StringIO
except ImportError:
    import StringIO

_count = 0

class Profiler(object):
    def __init__(self, path=None):
        if not path:
            path = os.path.join(os.path.dirname(__file__), "profile")
        self.path = path
        if not os.path.exists(path):
    def run(self, func, *args):
        """run(func, *args). Run func, dumping profile data into self.path."""
        global _count
        c = _count = _count + 1
        path = os.path.join(self.path, "" % c)
        prof = profile.Profile()
        result = prof.runcall(func, *args)
        return result
    def statfiles(self):
        """statfiles() -> list of available profiles."""
        return [f for f in os.listdir(self.path)
                if f.startswith("cp_") and f.endswith(".prof")]
    def stats(self, filename, sortby='cumulative'):
        """stats(index) -> output of print_stats() for the given profile."""
        s = pstats.Stats(os.path.join(self.path, filename))
        oldout = sys.stdout
            sys.stdout = sio = StringIO.StringIO()
            sys.stdout = oldout
        response = sio.getvalue()
        return response
    def index(self):
        return """<html>
        <head><title>CherryPy profile data</title></head>
        <frameset cols='200, 1*'>
            <frame src='menu' />
            <frame name='main' src='' />
        """ = True
    def menu(self):
        yield "<h2>Profiling runs</h2>"
        yield "<p>Click on one of the runs below to see profiling data.</p>"
        runs = self.statfiles()
        for i in runs:
            yield "<a href='report?filename=%s' target='main'>%s</a><br />" % (i, i) = True
    def report(self, filename):
        import cherrypy
        cherrypy.response.headers['Content-Type'] = 'text/plain'
        return self.stats(filename) = True

class ProfileAggregator(Profiler):
    def __init__(self, path=None):
        Profiler.__init__(self, path)
        global _count
        self.count = _count = _count + 1
        self.profiler = profile.Profile()
    def run(self, func, *args):
        path = os.path.join(self.path, "" % self.count)
        result = self.profiler.runcall(func, *args)
        return result

class make_app:
    def __init__(self, nextapp, path=None, aggregate=False):
        """Make a WSGI middleware app which wraps 'nextapp' with profiling."""
        self.nextapp = nextapp
        self.aggregate = aggregate
        if aggregate:
            self.profiler = ProfileAggregator(path)
            self.profiler = Profiler(path)
    def __call__(self, environ, start_response):
        def gather():
            result = []
            for line in self.nextapp(environ, start_response):
            return result

def serve(path=None, port=8080):
    import cherrypy
    cherrypy.config.update({'server.socket_port': int(port),
                            'server.thread_pool': 10,
                            'environment': "production",

if __name__ == "__main__":
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.