Wednesday, August 19, 2015

PHP Profiling Using Xdebug and Kcachegrind

Xdebug's Profiler is a powerful tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost. The profiler in Xdebug 2 outputs profiling information in the form of a cachegrind compatible file. This allows you to use the KCacheGrind tool (Linux,) to analyse your profiling data. 
Xdebug profiler is incorporated in the Xdebug tool. Therefore you first need to download, install, and enable Xdebug itself and after that enable the profiling functionality within it.

Install x-debug through apache


sudo apt-get install php5-xdebug

Enabling Xdebug integration with the PHP engine

Once xdebug is installed, you can enable the xdebug(profiler) to work with PHP by adding following lines at the end of php.ini file.








[xdebug] 
zend_extension="/usr/lib/php5/20121212/xdebug.so"  ;path to xdebug.so
xdebug.profiler_append = 0
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_name = cachegrind.out.%t-%s
xdebug.profiler_output_dir = "/home/bipen/lf/fhf_files/codeprofiling"
You can run this  command yo find the path to xdebug.so

find / -name 'xdebug.so' 2> /dev/null
Replace this path in "zend_extension= ...."

Xdebug Settings
  1. xdebug.profiler_append:
    Type: integer, Default value: 0
    Setting it to 1 will not overwrite the exisitng profiler file when a new request is made but append the new profiling in same file.
     
  2. xdebug.profiler_enable:
    Type: integer, Default value: 0
    Setting it to 1 will enable xdebug profiler which creates a file in the "profiler_output_dir" directory but If you want to selectively enable the profiler, set "profiler_enable_trigger" as 1
     
  3. xdebug.profiler_enable_trigger
     Type: integer, Default value: 0When this setting is set to 1, you can trigger the generation of profiler files by using the XDEBUG_PROFILE GET/POST parameter, or set a cookie with the name XDEBUG_PROFILE. If you want to profile a particular url you can append "?XDEBUG_PROFILE=1"  at the end.
    eg: http://somedomain.com/test?XDEBUG_PROFILE=1  (make sure xdebug.profiler_enable is set to 0)

  4. xdebug.profiler_output_name
    Type: string, Default value: cachegrind.out.%p This setting determines the name of the file that is used to dump traces into. The setting specifies the format with format specifiers, very similar to sprintf() and strftime(). There are several format specifiers that can be used to format the file name.
     
  5. xdebug.profiler_output_dir
    Type: string, Default value: /tmp The directory where the profiler output will be written to, make sure that the user who the PHP will be running as has write permissions to that directory. 

Once all of this settings is set,  run any project in your browser and this should generate a profiler file in above mentioned directory. Keep it mind, "profiler_enable"  set to 1 will profile all of the PHP process everytime it  runs  so you might end up having a lots and lots of profiler files in your system.

Installing KCACHEGRIND

KCachegrind is a tool used for profile data visualization. Visit this link for more details (http://kcachegrind.sourceforge.net/html/Documentation.html)

sudo apt-get install kcachegrind

References: