Django profiling with hotshot and kcachegrind

Testing django applications

hotshot is a python profiler and Django has a modified mod_python handler that uses hotshot and generates logs. Those logs will help you find slow pieces of your django applications.


Your application needs to run under apache / mod_python. Here is a simple config:
Alias /media/ "/YOUR_PATH/django/contrib/admin/media/"
Alias /site_media/ "/YOUR_PATH/site_media/"
<Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.profiler-hotshot
    PythonPath "['/YOUR_PATH/MY/APP/'] + sys.path"
    PythonDebug On
<Location "/site_media">
SetHandler none
You don't use:
PythonHandler django.core.handlers.modpython
PythonHandler django.core.handlers.profiler-hotshot

By default logs are saved to /var/log/cmsprofile and the folder must exists and apache hast to have write permissions on it. Now when you call a page of your django project a log file will be generated there (hidden file - starts with a dot). To make those files useful we will use KCachegrind.

Using Kcachegrind

kcachegrind is a KDE application for profilers logs visualisation. To use Kcachegrind we need to convert hotshot logs using one of kcachegrind helpers:
hotshot2calltree > cachegrind.out.01
hotshot2calltree will generate a log file that can be used in kcachegrind:

hotshot for scripts

It's very simple, like this:
import hotshot
prof = hotshot.Profile("")
#your python code here
*prof file needs to be converted in the same way.
blog comments powered by Disqus