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 Trott | PHP Tutorials | November 15, 2012
807 words, estimated reading time 3 minutes.

PHP Performance Tuning is a good way to waste hours of time to 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 get 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 certainly help in 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. We are going to add a few lines to the php.ini file using your favourite text editor.

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, your version number may be different, 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 now have access to the PHP performance tuning tools and enhanced debug messages without any changes to your applications.

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 once again edit the php.ini configuration file and add a few lines.

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

In a suitable place, it does not matter where 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 next two lines set the output directory and the filename. Make sure that 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 am going to 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.

In 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, again another source of improvement. 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.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.