XHProf Howto (on Debian with vhost)

XHPROF is a very helpful profile tool including html based reports which makes it easy to analyze and optimize a php application/webpage.

Installation

Since I was not able to use the PECL installation on Debian:

apt-get install php5-common
pecl config-set preferred_state beta
pecl install xhprof

… I installed it manually.

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test

Installation

Since I was not able to use the PECL installation on Debian:

apt-get install php5-common
pecl config-set preferred_state beta
pecl install xhprof

… I installed it manually as root:

cd ~
wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test

 

Enabling XHProf in PHP

To Enable XHPROF open your apache php.ini

vi /etc/php5/apache2/php.ini

And add this lines:

[xhprof]
extension=xhprof.so
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
xhprof.output_dir=/tmp/xhprof

 

The output dir is used by xhprof to store it’s profiling raw data when analyzing the code. If the folder already exists make sure it is owned by www-data (or the apache process owner).

mkdir /tmp/xhprof
chown www-data:www-data /tmp/xhprof

 

Data reports vhost

xhprof generates readable reports in HTML – to enable those we have to make sure apache can read and write the xhprof html structure (which is part of the previously downloaded archive).
To make it more easy I decided to create a name based virtual host where reports can be viewed.

As root:

mv ~/xhprof-0.9.2/ /var/www/ # move the extracted xhprof folder to /var/www
chown -R www-data:www-data /var/www/xhprof-0.9.2

Create the virtual hosts file:

vi /etc/apache2/sites-available/xhprof

Assuming you have a running Name Based Virtualhost setup add this to the created file:

<VirtualHost *:80>
        DocumentRoot "/var/www/xhprof-0.9.2/"
        ServerName xhprof.<yourDomain>:80
        ServerAdmin your@email.com
        CustomLog /var/log/apache/apache.log combined
</VirtualHost>

Now enable the virtualhost – this creats a symlink from /etc/apache2/sites/available/xhprof to /etc/apache2/sites/enabled

a2enable xhprof

 

Finally restart apache

/etc/init.d/apache2 restart

Including xhprof in our application

There are several ways to do this – it is also possible to only profile parts of an application. It is more wise to profile the complete code. So a simple test could be:

# Init XHPROF
if (extension_loaded('xhprof')) {
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    $XHPROF_ROOT = '/var/www/xhprof-0.9.2';
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
}

$i = 0;
while ($i < 10000) {
    $i++;
}

# De-Init XHPROF, generate report and link
if (extension_loaded('xhprof')) {
    $data = xhprof_disable();
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($data, "xhprof");
    $profiler_url = sprintf('http://xhprof.<YOURDOMAIN>/xhprof_html/index.php?run=%s', $run_id);
    echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
}

 

As you can see there are just a few steps to use xhprof:

  1. See if it is enabled (extension loaded)
  2. xhprof_enable
  3. Inlcude some libs
  4. Run some code which should be profiled
  5. xhprof_disable
  6. Save the profile data (save_run)
  7. Provide the URL where the data can be watched as HTML

Optional – other ways to include xhprof

Now it is not always that easy – some applications do not have a defined start or stop or perform several exit in different codepaths. It is necessary to think about the application structure. A valid way is to include the init part in the constructor of a class and the de-init in the destructor. My self-written MVC for example is based on a main class which is called in the beginning and no matter where and how it exits it also passes the constructor.

Another approach is to enable it for EVERY php application running on this server by using auto-append and prepend in your php.ini:

; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file = /var/www/xhprof-0.9.2/header.php

; Automatically add files after PHP document.
; http://php.net/auto-append-file
auto_append_file = /var/www/xhprof-0.9.2/footer.php

 

Then put the init-code into the header and the de-init code into the footer. This is a valid solution for development machines. But be aware – every php homepage/application will show the profile link below.

Auto-Append and Auto-Prepend can also be enabled per-vhost/index by using htaccess:

XHPROF is a very helpful profile tool including html based reports which makes it easy to analyze and optimize a php application/webpage.

Installation

Since I was not able to use the PECL installation on Debian:

apt-get install php5-common
pecl config-set preferred_state beta
pecl install xhprof

… I installed it manually.

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test

 

Enabling XHProf in PHP

To Enable XHPROF open your apache php.ini

vi /etc/php5/apache2/php.ini

And add this lines:

XHPROF is a very helpful profile tool including html based reports which makes it easy to analyze and optimize a php application/webpage.

Installation

Since I was not able to use the PECL installation on Debian:

apt-get install php5-common
pecl config-set preferred_state beta
pecl install xhprof

… I installed it manually as root:

cd ~
wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test

 

Enabling XHProf in PHP

To Enable XHPROF open your apache php.ini

vi /etc/php5/apache2/php.ini

And add this lines:

[xhprof]
extension=xhprof.so
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
xhprof.output_dir=/tmp/xhprof

The output dir is used by xhprof to store it’s profiling raw data when analyzing the code. If the folder already exists make sure it is owned by www-data (or the apache process owner).

mkdir /tmp/xhprof
chown www-data:www-data /tmp/xhprof

 

Data reports vhost

xhprof generates readable reports in HTML – to enable those we have to make sure apache can read and write the xhprof html structure (which is part of the previously downloaded archive).
To make it more easy I decided to create a name based virtual host where reports can be viewed.

As root:

mv ~/xhprof-0.9.2/ /var/www/ # move the extracted xhprof folder to /var/www
chown -R www-data:www-data /var/www/xhprof-0.9.2

Create the virtual hosts file:

vi /etc/apache2/sites-available/xhprof

Assuming you have a running Name Based Virtualhost setup add this to the created file:

<VirtualHost *:80>
        DocumentRoot "/var/www/xhprof-0.9.2/"
        ServerName xhprof.<yourDomain>:80
        ServerAdmin your@email.com
        CustomLog /var/log/apache/apache.log combined
</VirtualHost>

Now enable the virtualhost – this creats a symlink from /etc/apache2/sites/available/xhprof to /etc/apache2/sites/enabled

a2enable xhprof

 

Finally restart apache

/etc/init.d/apache2 restart

Inclusing xhprof in our application

There are several ways to do this – it is also possible to only profile parts of an application. It is more wise to profile the complete code. So a simple test could be:

# Init XHPROF
if (extension_loaded('xhprof')) {
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    $XHPROF_ROOT = '/var/www/xhprof-0.9.2';
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
}

$i = 0;
while ($i < 10000) {
    $i++;
}

# De-Init XHPROF, generate report and link
if (extension_loaded('xhprof')) {
    $data = xhprof_disable();
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($data, "xhprof");
    $profiler_url = sprintf('http://xhprof.<YOURDOMAIN>/xhprof_html/index.php?run=%s', $run_id);
    echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
}

 

As you can see there are just a few steps to use xhprof:

  1. See if it is enabled (extension loaded)
  2. xhprof_enable
  3. Inlcude some libs
  4. Run some code which should be profiled
  5. xhprof_disable
  6. Save the profile data (save_run)
  7. Provide the URL where the data can be watched as HTML

Optional – other ways to include xhprof

Now it is not always that easy – some applications do not have a defined start or stop or perform several exit in different codepaths. It is necessary to think about the application structure. A valid way is to include the init part in the constructor of a class and the de-init in the destructor. My self-written MVC for example is based on a main class which is called in the beginning and no matter where and how it exits it also passes the constructor.

Another approach is to enable it for EVERY php application running on this server by using auto-append and prepend in your php.ini:

; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file = /var/www/xhprof-0.9.2/header.php

; Automatically add files after PHP document.
; http://php.net/auto-append-file
auto_append_file = /var/www/xhprof-0.9.2/footer.php

 

Then put the init-code into the header and the de-init code into the footer. This is a valid solution for development machines. But be aware – every php homepage/application will show the profile link below.

Auto-Append and Auto-Prepend can also be enabled per-vhost/index by using htaccess:

php_value auto_prepend_file /var/www/xhprof/header.php
php_value auto_append_file /var/www/xhprof/footer.php

Resources

http://php.net/manual/en/book.xhprof.php
http://techportal.inviqa.com/2009/12/01/profiling-with-xhprof/

 

Leave a Reply