Django profiling with hotshot and kcachegrind

Check out the new site at https://rkblog.dev.

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.

Requirements

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
    SetEnv DJANGO_SETTINGS_MODULE settings
    PythonPath "['/YOUR_PATH/MY/APP/'] + sys.path"
    PythonDebug On
</Location>
<Location "/site_media">
SetHandler none
</Location>
You don't use:
PythonHandler django.core.handlers.modpython
Instead:
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 file.prof > cachegrind.out.01
hotshot2calltree will generate a log file that can be used in kcachegrind:
djangoprof
djangoprof2


hotshot for scripts

It's very simple, like this:
import hotshot
prof = hotshot.Profile("plik.prof")
prof.start()
#your python code here
prof.stop()
*prof file needs to be converted in the same way.
RkBlog

Programming in Python, 14 July 2008


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact