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
6
extension written by Derick Rethans, with
KCachegrind
7
or QCachegrind
8
to review the results.
2. Newcomer XHProf
9
, 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:
6
http://xdebug.org/
7
http://kcachegrind.sourceforge.net/html/Home.html
8
http://kcachegrind.sourceforge.net/html/Home.html
9
http://pecl.php.net/package/xhprof
227Performance
$ wget http://pecl.php.net/get/xhprof-0.9.2.tgz
$ tar zxvf xhprof-0.9.2.tgz
Next, change to the extension subdirectory; this is where well compile the exten-
sion:
$ 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:
[xhprof]
extension=xhprof.so
xhprof.output_dir="/tmp/xhprof"
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
chapter_06/header.php
// 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.
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
Well call the second file footer.php:
chapter_06/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 = 'http://example.org/xhprof_html/index.php';
$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:
229Performance
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.