iguana: The Tilley Hemp Hat (Default)
Iguana ([personal profile] iguana) wrote2012-01-23 02:58 pm

Using repoze.profile in a Pylons project

What with Google Code Search having been closed down, and the Pylons documentation largely broken since that project merged into Pyramid, it took a few leaps of faith on my part to figure out how to set up profiling on a project using the repoze.profile middleware.

project/config/middleware.py:
from repoze.profile.profiler import AccumulatingProfileMiddleware

# ...

def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
    # Configure the Pylons environment
    load_environment(global_conf, app_conf)

    # The Pylons WSGI app
    app = PylonsApp()

    #Profile the app
    app = AccumulatingProfileMiddleware(
        app,
        log_filename='profiling.log',
        cachegrind_filename='cachegrind.out',
        discard_first_request=True,
        flush_at_shutdown=True,
        path='/__profile__'
    )

    # Routing/Session/Cache Middleware
    app = RoutesMiddleware(app, config['routes.map'])
    app = SessionMiddleware(app, config)
    app = CacheMiddleware(app, config)

    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
    # ...


Then go to http://localhost:5000/__profile__ for the stats. It looks like this might be rather helpful as of writing, but we shall see now I have it set up.

Edit: You'll need python2.6+ for the pstats module, which caused me some trouble (yes, I know it's 2012), but seems to have paid off. I've already got some nice juicy stats that have exposed at least one function that's taking longer than ideal.