Profiling your App with XDebug and KCacheGrind

If profiling of applications is something you already heard of but never played with, here is what I learned from starting to dig into this topic:

(1) Install XDebug (if dou do not already have it on your Dev-System). If not, please refert to the XDebug-Docs. The procedure is very well documented there. I used the pecl installer.

(2) I added this content to my php.ini files (for Apache2 and CLI). In the “Switch profiling”- and “Switch tracing”-sections you can comment the lines “xdebug.profiler_enable = 1/0″ and “xdebug.auto_trace = 1/0″ in or out. If you leave profiling and/or tracing on, it will significantly slow down all PHP execution on your server and generate many large cachegrind-files. It is not wise to leave it on if you do not need to profile or trace!

If I decide to do profiling, I switch it on, do my script executions to be profiled, which produces the required cachegrind-files and then I switch it off again. Remember you have to restart Apache2 after each change to your php.ini in order to take effect.

Set the path to your directory where you like to get the cachegrind-files written to. I set it to ‘/var/www/__profiling_data’.

(3) Now you need a viewer. I started under Windows with the free Tool WinCacheGrind. But to be honest, I did expect something cooler… As I tried KCacheGrind, which is a free Tool for Linux I was pleased to see informative screens like the following:

KCacheGrind Screen 1 KCacheGrind Screen 2

For those of you who do not know how to use Linux Apps… On Debian do “apt-get install kcachegrind” and once it is installed, type “kcachegrind &” on a console after logging in in the GUI-interface of Linux.

I was surprised at which points my scripts spent their time. Check the XDebug-Docs for more information on what you acually can read from the profiledata.

Happy profiling!