How to speed up your app using XHProf profiling

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Sat Mar 29, 2014 6:51 pm

How to speed up your app using XHProf profiling

Postby aussieguy123 » Sat Mar 29, 2014 6:57 pm

XHProf is a PECL extension for PHP. To install it you first need to install PEAR. For Ubuntu, you run this command to install pear:
vagrant@vagrant-ubuntu-raring-64:~$ sudo apt-get install php-pear

You also need to install the php5-dev package so that you have access to the phpize comand:
vagrant@vagrant-ubuntu-raring-64:~$ sudo apt-get install php5-dev

Then, you use pecl to install the extension
vagrant@vagrant-ubuntu-raring-64:~$ sudo pecl install xhprof-beta

Watch the output of this command and you should see a line like this
265117 108 -rwxr-xr-x 1 root root 109247 Mar 26 22:22 /tmp/pear/temp/pear-build-roottyfEC6/install-xhprof-0.9.4/usr/lib/php5/20100525/

This line indicates the location of the extension file that you need to include in php.ini, just remove the /tmp/pear/temp/pear-build-roottyfEC6/install-xhprof-x.x.x part.
vagrant@vagrant-ubuntu-raring-64:~$ ls /usr/lib/php5/20100525/

Add extension=*filename of file* to your php.ini file

To verify that the installation worked, call phpinfo(); from a file on your web server. If it worked, there will be a section for XHProf.

Next, create a virtual host for you to view the HTML output of your xhprof runs. Below is one for Apache but you could make something similar for nginx and other web servers.
<VirtualHost *:80>
ServerName xhprof

DocumentRoot /usr/share/php/xhprof_html/

Options FollowSymLinks
AllowOverride None

Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Edit your /etc/hosts file and make xhprof point to, or the IP address of the system your web server is accessible from. xhprof

Then, restart your web server.
To start profiling, insert the following code where you want the profiling to begin:

If your app is namespaced, use the following code where you want the profiling to stop:
$xhprof_data = xhprof_disable();
$appNamespace = 'your_namespace';
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);

If your app is not namespaced, use this code to stop the profiler:
$xhprof_data = xhprof_disable();
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();

After this, you should see a list of xhprof runs if you visit xhprof in your browser.

Click on one of the runs and you will see a run down of each function in your app and its performance statistics.

And that's it. Now, you can identify and fix performance bottlenecks in your code.

A version of this guide, with screenshots is available here: ... prof?id=11

Return to “PHP coding => General”

Who is online

Users browsing this forum: Baidu [Spider], matt86 and 10 guests