I was working on a project which was randomly failing to load certain views outside the development environment. It turns out that the system was running out of memory. After searching some of the error messages output by the script, I stumbled upon the xhprof PHP module. It was originally created by Facebook, and released under an open source license.
xhprof provides profiling information, down to the function call. This includes execution time, CPU and memory usage for each operation. The module allows you to find and optimise bottlenecks in your application. The library includes a GUI output, you just use the classes provided to create the reports.
I installed using PECL on a Debian Squeeze development server, and the first things you need are php5-dev and make;
apt-get install php5-dev make
However, when you try to run the PECL install, we fail because the package is beta. We therefore need to set PECL to install beta packages. Configure PECL to install beta packages:
pecl config-set preferred_state beta
pecl install xhprof
PECL will correctly fetch the source files, but fail to start compiling.
11 source files, building
Cannot find config.m4.
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module
ERROR: 'phpize' failed
This error is caused by a bug in the PEAR library installed on the system. The bug was fixed in PEAR 1.9.3, but as of the time of writing PEAR 1.9.4 is the latest stable version. Updating PEAR fixes the issue and allows the module to be compiled correctly.
pear install PEAR-1.9.4
After installing 1.9.4 you can then compile and install xhprof on Debian Squeeze. To add the module to PHP, you need to add it to your configuration.
The last thing to do is check the module is correctly loaded, check your phpinfo() output for an xhprof section. Then you can start profiling your projects.
For more information on setting up and using xhprof, head over to this ibuildings techportal article.