Using xDebug with PHP
This article discusses installing and using xDebug for debugging and profiling PHP scripts we also touch on using kcachgrind to help interpret the results of our profiling.Recently we profiled the most commonly used sections of our website to see if we could improve upon our wait time and generally improve our code base.
xDebug |
Setting Up xDebug
Our first task we need to setup our alpha test bed to use xDebug we like to use pear when installing packages like this as it sets the system up for us. Simply running the following command as root will install all that is needed.
root@chic:~# pecl install xdebug
Once you have issued this command you need to configure PHP to use the extension
/etc/php5/apache2/conf.d/xdebug.ini
You need to add the following to the above file remembering to change the value of profiler_output_dir and trace_output_dir to reflect your own directory structure. Also your module location could vary if so use find or locate to find xdebug.so
zend_extension=/usr/lib/php5/20090626/xdebug.so
[xdebug]
xdebug.profiler_enable=on
xdebug.profiler_output_dir=/home/chic/cachegrind
xdebug.trace_output_dir=/home/chic/traces
xdebug.auto_trace=on
xdebug.profiler_output_name=callgrind.out.%t-%s
xdebug.collect_assignments=on
xdebug.collect_return=on
xdebug.collect_vars=on
[xdebug]
xdebug.profiler_enable=on
xdebug.profiler_output_dir=/home/chic/cachegrind
xdebug.trace_output_dir=/home/chic/traces
xdebug.auto_trace=on
xdebug.profiler_output_name=callgrind.out.%t-%s
xdebug.collect_assignments=on
xdebug.collect_return=on
xdebug.collect_vars=on
Once you have done this you need to reload Apache so the module is loaded and the configuration changes are read.
/etc/init.d/apache2 reload
You will want to create the directories for profile output files and give it the appropriate file permissions you may want to use something other than 777 but our alpha machines are single user so we can be a bit more relaxed.
mkdir /home/chic/cachegrind && chmod 777 /home/chic/cachegrind
We now need to do the same but for the stack trace file output.
mkdir /home/chris/traces && chmod 777 /home/chris/traces
Now the system should be ready to start generating debugging data although you should remember that quite a few files can be generated while debugging especially if you are throwing a lot of requests at your server. You should also never install xDebug on your production server as it is bad form.
Setting Up Kcachegrind
The last thing we need to install is kcachegrind which is the program we will use to interpret and visualise the generated profiler reports.
root@chic:~# apt-get install kcachegrind
You should now be able to run kcachegrind either using the shell or maybe your distribution has added a nice menu item for it. On Mint Linux it is located under programming in the main menu.
Start Profiling
To start the profiling of your scripts simply accesses the web address of the page you want to profile or run it via the command line if it is a cli application.This will create a new file under the directory you specified in the configuration file for us its called callgrind.out.%t-%s where %t is the time stamp and %s is the script name.
At this point you will need to open kcachegrind to help interpret the results from the profiling we like to use the Callee tab in the right window panel which allows you to visualise the amount of time each action is taking.
Using this helps us to check which parts of our script are taking up the biggest percentage in total execution time.
We used the information we gained from using this tool to improve one of our scripts by removing an unneeded function call which we more than likely would have missed if we did not profile the calling script.
By using this approach we managed to knock 40% of the execution time of our script which is a huge saving in execution time.
Debugging
We use xDebug on our development workstations to improve the output when something goes wrong it also adds some more information tovar_dump()
such as the type of the variable etc..xDebug should not be used on live servers for the reasons mentioned above and we would also advise not to use it on production machines.
Conclusion
Hopefully this article has given you an insight into debugging and profiling PHP scripts using xDebug and kcachegrind. Using these tools can help decide which parts of your script need the most attention.Being able to visualise the execution of a script is a good way to see what exactly is going on during run time.
No comments:
Post a Comment