PHP Performance Tuning with XDebug
PHP Performance Tuning is a good way to waste the 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.
Adverts Blocked This website is supported entirely by advertisements. Please disable AdBlocking software so that I can continue providing free content and services.
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:
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.
sudo pico /etc/php5/apache2/php.ini
You then need to locate the lines adding extensions to PHP, and add the line:
Please note, your version number may be different, so check the filename first.
Save the file and restart Apache to load the new module.
sudo /etc/init.d/apache2 restart
You can now verify successful installation of Xdebug by calling PHP on the command line:
If successful, you should see something like the following:
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.
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 the configuration file and add a few lines.
sudo pico /etc/php5/apache2/php.ini
In a suitable place, it does not matter where, insert the following lines:
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 performance logging, 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.
In the first screen, you can see an overview of the contents of the log, including 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 1ms to run, if it is being called 1,000 times in a page load it all adds up to a second.
Last updated on: Saturday 17th June 2017