XHProf PHP Profiling

Today I set up my development environment so I can use XHProf to profile PHP scripts when needed, and it was pretty easy.

For starters, I use Ubuntu as the operating system for my desktop environment. So while the information below may be helpful, I will not cover any other OS in my descriptions.

XHProf is a PECL package, and can be easily installed by using standard PECL commands.  However, XHProf is still beta and the default settings of PECL will only install stable packages.  Don’t fear, there is a way within PECL to handle this by appending “-beta” to the end of the module name: (also note how I am using sudo to act as the admin user on Ubuntu)

sudo pecl install xhprof-beta

After issuing the command PECL will do its job of installing the PHP module but will not add it to the php.ini, so that must be done manually.  The default location of the php.ini in Ubuntu is at ‘/etc/php5/apache2/php.ini’, so we edit it there:

sudo vi /etc/php5/apache2/php.ini

We add a single line to the end of the php.ini to activate the module:


Then we restart Apache for the new setting to take affect:

sudo service apache2 restart

At this point we now have the XHProf module in PHP for Apache related calls to PHP.  If we add ‘phpinfo();’ to a PHP file and view it in a browser we see XHProf is now available for use.

NOTE: This does not make XHProf available for CLI activity. (command line run PHP scripts)  We also need to add the extension to the ‘/etc/php5/cli/php.ini’ file as well to make it available via command line PHP.

Now that XHProf is ready to be used I added it to the PHP script I wanted to profile.  Basically this is just a command to kick off the profiling at the beginning of the script, and some commands to save the results at the end of the script.

In the beginning add the following to start recording at the beginning, and adds CPU and memory info to the output:


Then at the end add the following to halt profiling and then using XHProf utilities create the output:

$xhprof_data = xhprof_disable();
$appNamespace = 'some_namespace_here';
include_once '/usr/share/php/xhprof_lib/utils/xhprof_lib.php';
include_once '/usr/share/php/xhprof_lib/utils/xhprof_runs.php';

$xhprof_runs = new XHProfRuns_Default();
$xhprof_runs->save_run($xhprof_data, $appNamespace);

NOTE: XHProf will dump the results in the ‘/tmp’ location on the system named something like {run_id}.{app_namespace}.xhprof (Ex.- 51f384b8cb9f2.some_namespace_here.xhprof), but the contents are not truly human readable.  I recommend using the tools provided by XHProf to help make them viewable in HTML.

How I made XHProf files pretty

I am Lazy a HUGE fan of doing things simple, so to make the XHProf output readable and easier to use I simply created 3 files in my document root of Apache like so:


include_once '/usr/share/php/xhprof_html/index.php';


include_once '/usr/share/php/xhprof_html/callgraph.php';


include_once '/usr/share/php/xhprof_html/typeahead.php';

No sense in reinventing the wheel.  These 3 files simply included the files already existing in the XHProf module, which call the output files directly from the ‘/tmp’ location on my system.

By calling to http://localhost/index.php I am now greeted with a list of links representing the different files output from XHProf to the ‘/tmp’ location as I executed the PHP scripts calling to XHProf. (shown below)


Now when I click on the individual links I can view the output from XHProf in a friendly HTML format, like below:


So, there we go.  Enjoy!

4 responses to “XHProf PHP Profiling”