inicio mail me! sindicaci;ón

Profiling Django

Sometimes you need to get down to the core and make your code faster. One technique to speed up things is performance analysis through profiling.

Python 2.5 has revamped its support for profiling with the addition of cProfile which is (the name says all) a C-based version of the profile module. It’s definitely better than hotshot (which has been somewhat fixed in the latest version).

Django has a custom handler which enables mod_python powered apps to be profiled (with hotshot) without monkey patching the codebase.

What about internal webserver anyway?

There’s a wiki page about that. You simply have to patch django/core/management.py with this patch and Django will generate a handful of .prof files in /tmp or whatever each time you invoke the webserver. Then you have to read the stats with hotshot.stats.load or you can use gather_profile_stats.py and collect them all in aggregated profile files.

Sadly I didn’t get cProfile to work with Django because it lacks of a method returning the result of the profiled callable like the hotshot’s runcall but I didn’t spent much time on that so I guess the hack is just around the corner.

That’s it.

HTH

Related posts

  • Django community please stand up (for DB2)
  • The first Django book “made in Italy” is out
  • Django has the quid
  • Praising the Django book
  • Django from another perspective
  • Gravatar

    Maxime Biais said,

    March 7, 2007 @ 3:28 pm

    Thanks for the patch ! so simple to write, but not so simple to find the good place to plug it…

    Gravatar

    Lawrence said,

    March 7, 2007 @ 3:32 pm

    The patch is not really mine, I just updated the one you can find on the wiki page. Anyway.. you’re welcome :-)

    Gravatar

    Chris said,

    March 3, 2008 @ 4:36 am

    There is no runcall at the module level, but if you instantiate a Profile object you get access to the runcall method. … import cProfile prof = cProfile.Profile() return prof.runcall(handler, environ, start_response) …

    RSS feed for comments on this post