xdebug profiler for PHP and kcachegrind to optimize site speed

To enable xdebug profiling You just need to enable xdebug extension (add zend_extension=”/usr/lib64/php/modules/xdebug.so” for example if You are on 64bit Centos Linux distribution) and edit Your PHP configuration by creating separate xdebug.ini file or add these lines directly in php.ini:

xdebug.profiler_enable = 1 # by default profiler is not enabled
xdebug.profiler_output_dir = “/var/log/xdebug” #this is the directory where xdebug profiler generated files will be located
xdebug.profiler_output_name = “callgrind.%R::%u” #to work with kcachegrind output files must have callgrind. prefix (by default xdebug uses cachegrind.out. prefix) change this so You don’t need to manually rename files to callgrind. prefix.

Please don’t enable xdebug profiler ir production environment because in few seconds it can generate GIGABYTES of data and it will most likely crash Your webserver.

Here is an example what we can get from kcachegrind (data is from one of my first php projects):

Click to enlarge:
kcachegrind output example from php xdebug

kcachegrind sorts everything by importance so on top You can see PHP actions which use the most resources so You don’t need to guess where to optimize.

For example, I had ancient multi-language library in this project where I created 900 method calls for each of 900 translations. With one little fix now I make only 1 method call from multi-language library.

Unfortunately I couldn’t enable Profiler on XAMPP 1.7 and I tested this feature on production server for a few seconds.

There is more stuff about xdebug to write about so I am quite sure there will be Part II of this post 🙂

Advertisements