proved. But you already did the best you could so, now what? This is where
profiling comes in.
is the act of taking accurate time and/or memory measurements for every action
your code performs. This is then explored to determine where the bottlenecks lie.
There are two tools for profiling that are commonly used:
1. The tried-and-tested Xdebug
extension written by Derick Rethans, with
or QCachegrind
to review the results.
2. Newcomer XHProf
, from the folks at Facebook, with the XHGui web front end
written by Paul Reinheimer.
Xdebug is a fantastic tool that provides the most insight into your code. It does,
however, come with too much overhead, so is typically best avoided in a production
environment; furthermore, KCachegrind/QCachegrind work poorly on Mac OS X
or Windows. Theres a web front end called webcachegrind, but it fails to provide
anywhere near the functionality of the desktop tools, nor XHGui. Additionally,
comparing two unique profiles can be a tricky task.
On the other hand, XHProf is a tool developed for use in production environments.
Facebook has noted that it profiles hits randomly in production to assess performance
on an ongoing basis. With the addition of XHGui, you can very easily compare
multiple runs, even several months apart.
Installing XHProf
XHProf is available as a PECL extension; however, the latest package (at least) wont
install with the standard pecl install xhprof. Instead, we can install it by hand.
First, fetch the package (you can download this in your browser, too, if youd like!)
and unpack it:
$ wget
$ tar zxvf xhprof-0.9.2.tgz
Next, change to the extension subdirectory; this is where well compile the exten-
$ cd xhprof-0.9.2/extension
To compile a shared extension (either one thats included with the main PHP distri-
bution or one from PECL), you must first run the phpize command. This sets up
the extension for compilation against your current PHP version.
Then youll run ./configure, make, and make install, just like with any normal
source compilation:
$ ./configure --enable-xhprof
$ make
$ make install
Now enable the extension in your php.ini file:
Once this is done, youll want to restart your web server.
Now that we have the extension installed, lets use it. For this, we return to the
unpacked code directory, and this time pull out the xhprof_html and xhprof_lib
directories. Move both directories to your DocumentRoot.
Next, we need to create two files to wrap our code. Well use PHPs auto_pre-
pend_file and auto_append_file to automatically wrap our code with these files.
The first file well call header.php:
PHP Master: Write Cutting-edge Code228
// Only run if the xhprof extension is enabled
if (extension_loaded('xhprof')) {
// Include the xhprof classes
include_once '/path/to/xhprof_lib/utils/xhprof_lib.php';
include_once '/path/to/xhprof_lib/utils/xhprof_runs.php';
// Start the profiler capturing CPU and Memory data.
Well call the second file footer.php:
if (extension_loaded('xhprof')) {
$ns = 'myapp'; // namespace for your application
// Turn off the profiler
$xhprof_data = xhprof_disable();
// Instantiate the class to save our run
$xhprof_runs = new XHProfRuns_Default();
// Save the run
$run_id = $xhprof_runs->save_run($xhprof_data, $ns);
// url to the XHProf UI libraries
$url = '';
$url .= '?run=%s&source=%s';
// Replace the placeholders
$url = sprintf($url, $run_id, $ns);
// Display the URL
echo "<a href='$url' target='_new'>Profiler Output</a>";
Finally, add the following to your php.ini:
auto_prepend_file = /path/to/xhprof_lib/header.php
auto_append_file = /path/to/xhprof_lib/footer.php
Or, add this to your .htaccess file:
php_value auto_prepend_file /path/to/xhprof_lib/header.php
php_value auto_append_file /path/to/xhrprof_lib/footer.php
Once youve done this (and if necessary, restarted your web server), youll see a
link at the bottom of every page to the xhprof.profile output. Clicking this link
will reveal a page similar to Figure 6.10.
Figure 6.10. The XHProf user interface
This page gives an overview of the profile, including the amount of wall time (actual
time) and memory usage, as well as the total number of functions called. This is
followed by a list of the top 100 function calls; by default, theyre in the order they
are called.
Each row includes the following:
Function Name: the name of the function
Calls: how many times the function was called
Incl. Wall Time: the amount of wall time that passed from when the function was
called to when it completed, including any subfunctions called
Excl. Wall Time: the wall time used, excluding subfunctions
Incl. CPU: the amount of CPU time used, including any subfunctions called
PHP Master: Write Cutting-edge Code230

Get PHP Master now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.