PHP Performance Tuning with XDebug

XDebug is the swiss army knife of PHP performance tuning and enables diagnostics, debugging and performance profiling for PHP applications.

By Tim TrottPHP Tutorials • November 15, 2012
PHP Performance Tuning with XDebug

PHP Performance Tuning is an excellent way to waste hours and get milliseconds back. XDebug is the Swiss army knife of PHP performance tuning and enables enhanced diagnostics, debugging and performance profiling for all PHP applications. This article will show you how to set up and start profiling your scripts with XDebug.

Installing Xdebug Extension

Xdebug is a PHP performance tuning extension installed at the server level. It is quick and easy to install in Ubuntu and will help with PHP performance tuning.

Use the following command to install Xdebug:

C#
sudo aptitude install php5-xdebug

Next, we must edit the PHP configuration file to use the new extension. Using your favourite text editor, we will add a few lines to the php.ini file.

C#
sudo pico /etc/php5/apache2/php.ini

You then need to locate the lines adding extensions to PHP and add the line:

C#
zend_extension="/usr/lib/php5/20100525/xdebug.so"

Please note that your version number may differ, so check the filename first.

Save the file and restart Apache to load the new module.

C#
sudo /etc/init.d/apache2 restart

You can now verify the successful installation of Xdebug by calling PHP on the command line:

C#
php -v

If successful, you should see something like the following:

C#
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:01:00)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.0.5, Copyright (c) 2002-2008, by Derick Rethans

You can now access the PHP performance tuning tools and enhanced debug messages without any application changes.

Example Outputs for PHP Performance Tuning

var_dump

var_dump before XDebug
var_dump before XDebug
var_dump with Xdebug Installed
var_dump with Xdebug Installed

Error Messages

Before XDebug:

PHP Error Message Before XDebug
PHP Error Message Before XDebug

With XDebug:

Error message after XDebug Installed
Error message after XDebug Installed

As you can see, XDebug not only provides the debug information in a much clearer way but also provides additional information that is very useful for debugging applications.

PHP Performance Tuning and Profiling

One of XDebug's strong points is its ability to save profiling information to a log file, which can then be analysed to find areas where application performance should be improved. To enable performance data logging, we need to edit the php.ini configuration file again and add a few lines.

C#
sudo pico /etc/php5/apache2/php.ini

In a suitable place, it does not matter where you insert the following lines:

C#
xdebug.profiler_enable=1
xdebug.profiler_append=On
xdebug.profiler_output_dir="/var/www/logs/dev/trace";
xdebug.profiler_output_name="cachegrind.out.%t-%s"

The first line will enable PHP performance tuning logging data, set to 0 to disable. The second line tells Xdebug to append to log files on each page refresh. If you do not set this, Xdebug will overwrite the log each time a page is requested. The following two lines set the output directory and the filename. Ensure you check that Apache (or another web server) has to write access to the directory.

Once Apache has been restarted, each time a page is loaded from the server, Xdebug will generate a log file containing all the performance profiling information. Be careful, as XDebug saves a lot of information to disk, and you may find your hard drive rapidly filling up.

Analysing PHP Performance Tuning Data

The log files themselves are useless; you will need to use an analysis tool such as CacheGrind to get any meaningful information out of them. CacheGrind is available for both Windows and Linux. In this example, I will use the Windows version WinCacheGrind.

Once downloaded, there is no need to install anything; just run the executable to run the program. You can then load a log file using the file menu.

WinCacheGrind XDebug Performance Analysis
WinCacheGrind XDebug Performance Analysis
WinCacheGrind XDebug Performance Analysis
WinCacheGrind XDebug Performance Analysis

On the first screen, you can see an overview of the contents of the log, including the total times to load the page. You can drill down into each function to look at the performance timings of functions called from within.

On the second screen, you can see the overall performance, and if you sort the list by Avg, Self, you can see the functions that take the longest to complete. You can drill down into the function to see what is causing them to run longer and identify areas for improvement. You can also see what functions are most often called, another improvement source. Just because a function only takes 1 ms to run, if it is being called 1,000 times in a page load, it all adds up to a second.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.