Xdebug is a great tool for debugging our PHP code, but another thing it does very well is help us find bottlenecks in our applications. For instructions on installing xDebug you can see my article Debugging PHP code with xDebug.

Enabling the profiler

We can tell xDebug to profile our code by adding this in our php.ini file:

1
2
xdebug.profiler_enable = 1
xdebug.profiler_output_dir = '/path/to/folder/'

xdebug.profiler_output_dir is the folder where xDebug will dump the profile information. Make sure this folder is writable by apache or you won’t see any file generated in that folder.

Once you do this configuration all requests made to your server will generate a profiling file in the specified folder. This is generally a very bad idea because those files can often get very large and will soon fill your hard drive. Because of this we should set profiler_enable to 0 and enable_trigger to 1 on php.ini:

1
2
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1

This tells xDebug to only output the profiling file when an HTTP parameter with the name XDEBUG_PROFILE is present. So something like this would activate the profiler for an specific request:

1
http://ncona.com/?XDEBUG_PROFILE=1

Parsing the profile information

If you open the profile file generated by xDebug it won’t make much sense to you. This is because that file is not made to be read by humans. The file follows a format called cachegrind that can be read by (in our case) KCacheGrind.

You can install KCacheGrind in Ubuntu based systems with this command:

1
sudo apt-get install kcachegrind

Once KCacheGrind is installed you can use it to open the files generated by xDebug. KcacheGrind will give you a lot of information about the performance of your code in a very easy to read format.

[ debugging  php  profiling  programming  ]
Debugging assembly with GDB
Introduction to GDB
SSL termination on load-balanced wordpress
Setting up Google Analytics for Android
Using Android lint to check for errors in your code