Profiling PHP Web-Applications

Large scale PHP applications, of which there are many, can sometimes have performance issues that are very difficult to diagnose. When using Radix, Zend, Cake, Yii or any other framework it is in your best interest to use the tools provided here to optimise your code.

There are many factors to performance across many facets of hardware and software; so performance metrics should be acquired and tested against the same environment. That is make a base-line measurement, adjust and then re-test on the same system.

We're going to cover three tools here, xdebug, webgrind and xhprof. The first will provide information about function calls, frequency and duration and optionally some function call graphs. The second is a tool for viewing the outputs collected by the xdebug profiler. Finally, xhprof is an alternate profiler which provides metrics on cpu, time and memory usage.


The xdebug tools is super awesome for two purposes - a) debugging and b) collecting performance statistics.

~ # emerge xdebug
~ # apt-get install php5-xdebug
~ # yum install php5-xdebug
~ # pecl install xdebug

Configure xdebug by adding the following to your php.ini, it does not go in a sub-section. This example only shows useful values for the profiler, not the debugger. Review the xdebug settings documentation for more details.
; or full path, if needed
; zend_extension=/usr/lib/php5/20090626/

xdebug.profiler_enable = 1
xdebug.profiler_output_dir = /tmp/phprof
xdebug.profiler_output_name = %S.xdebug
xdebug.trace_format = 2

Note that because of the %U value used in the profiler_output_name mod_unique_id must be enabled. This module is also not available inside of lighttpd.

Or, this can be enabled via Apache configurations.

  php_flag xdebug.default_enable off
  php_flag xdebug.profiler_enable off
  php_value xdebug.profiler_output_name "cachegrind.out.%R.%t"


Install xhprof using pecl.

~ # pecl install xhprof
Failed to download pecl/xhprof within preferred state "stable", latest release is version 0.9.2, stability "beta", use "channel://" to install
install failed

Which may required the --force option.

~ # pecl install --force  xhprof

Or, manually install xhprof from source.

~ # cd /opt
. # git clone
. # cd xhprof/extension
. # phpize
. # aclocal; libtoolize --force; autoreconf
. # ./configure && make && make install

Configure xhprof by addding this to your php.ini file

~ # cd /var/www/site/webroot
~ # ln -s /opt/xhprof/xhprof_html ./xhprof

In order to engage xhprof it's necessary to modify the code to trigger it's functionality and to save the outputs.

Code for xhprof enable and save

The xhprof extension must be manually triggerd in code, with something like this, to start profiling. For example, this could be line 1 of your front-main controller (sometimes called index.php).


Bottom Line Configuration

xdebug.profiler_enable = 1
xdebug.profiler_output_dir = /tmp/phprof
xdebug.profiler_output_name = %U.xdebug
xdebug.trace_format = 2
xhprof.output_dir = /tmp/xhprof


See the webgrind howto.