Step 1 : Install XDebugxdebug is an open source debugger available for PHP. xdebug can be used to display more information in error traces. It can also be used to collect detailed code coverage & profiling information.
Installation
You need to install following packages to prepare environment for installation of pecl module xdebug.
sudo apt-get -y install php-pear php5-dev build-essential
Now we install xdebug using pecl.
sudo pecl install xdebug
Above command will download, compile & install xdebug on your system.
Configuration
Open usr/local/lib/php.ini and append following line.
zend_extension=/usr/lib/php5/20060613/xdebug.so
Be careful about that number part in the path as it could be different for you. Now restart apache by issuing following command.
sudo apache2ctl restart
Now try to view the output of your phpinfo() function. If you find xdebug word in that then it means you have successfully installed xdebug.
Stacktrace
xdebug.collect_params accepts values from 1 to 4. Where 1 refers to less verbosity & 4 refers to maximum verbosity while displaying stack traces. The easiest way of setting this option is using function ini_set in PHP.
ini_set("xdebug.collect_params", )
Get to know more stack trace options of xdebug.
var_dump
Once after installing xdebug when ever you call var_dump you are actually calling xdebug version of var_dump instead of built in one. By default xdebug version of var_dump gives you friendly info on the variables you are trying to inspect.
See more options on controlling display of var_dump information.
Debugging
xdebug uses DBGp protocol to communicate with external debuggers. Vim already has a plugin to do that. So you can debug php scripts from within vim.
Download php debugger plugin for vim.
Step 2: Enable Profiling
You need to add following line to /usr/local/lib/php.ini to enable profiling of php scripts.
xdebug.profiler_enable=1
Now restart the apache server by issuing following command.
sudo apache2ctl restart
When ever you access a php page through apache, xdebug will create a file something like cachegrind.out.15093. xdebug by default uses /tmp directory to dump files which contain profiling information. You can change this target directory by using xdebug option xdebug.profiler_output_dir and you can change result file name by using the option xdebug.profiler_output_name.
See more xdebug profiling options.
Some times you don't want to profile all requests. xdebug provides a selective mechanism to trigger profiling of specific requests. In order to enable this option you have to add following configuration option to
php.ini.
xdebug.profiler_enable_trigger=On
Now you can trigger profiling of specific requests by adding XDEBUG_PROFILE to the query part of the GET request.
Analyzing Profiling results
We need to use kcachegrind to analyse profile file results. We can install kcachegrind by issuing following command.
sudo apt-get -y install kcachegrind
Now open your profle result file in kcachegrind and you can visuall inspect which part of your script is eating cpu resources. Callmap & Callgrap provide easy to understand visualizations to find bottlenecks in your script.