Installing XHProf in MAMP for Drupal 6

XHProf is a hierarchical profiler for PHP. It reports function-level call counts and inclusive and exclusive metrics such as wall (elapsed) time, CPU time and memory usage. A function's profile can be broken down by callers or callees. The raw data collection component is implemented in C as a PHP Zend extension called xhprof. XHProf has a simple HTML based user interface (written in PHP). The browser based UI for viewing profiler results makes it easy to view results or to share results with peers. A callgraph image view is also supported.

So you're sold, but getting it installed in MAMP is kind of a pain.

A little about my setup system first. I'm running Mac OS X 10.6.4 with MAMP 1.9.2 with php 5.2.13 and you'll need Xcode.

First step is downloading the MAMP Server components and libraries. Heads up though, this is a 125.5 MB download. You could also download php-5.2.13 from php.net, it's only 12 MB.

$ cd /Applications/MAMP/bin/php5.2
$ mkdir includes
$ cd !$
$ wget http://us.php.net/get/php-5.2.13.tar.gz/from/this/mirror
$ tar zxvf php-5.2.13.tar.gz
$ mv php-5.2.13 php
$ MACOSX_DEPLOYMENT_TARGET=10.6 CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch i386 -arch x86_64 -bind_at_load"
$ export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
$ ./configure CFLAGS="-arch i386" --with-config-file-path=/Applications/MAMP/bin/php5.2/bin/php-config

Great, you're halfway there.

Next step is downloading XHProf 0.9.2 from PECL.

$ cd ~/Desktop
$ wget http://pecl.php.net/get/xhprof-0.9.2.tgz
$ tar zxvf xhprof-0.9.2.tgz
$ cd xhprof-0.9.2/extensions
$ /Applications/MAMP/bin/php5/bin/phpize
$ ./configure CFLAGS="-arch i386" --with-php-config="/Applications/MAMP/bin/php5.2/bin/php-config"
$ make
$ make install

Now, assuming everything has gone alright, you should have a xhprof.so file in ~/Desktop/xhprof-0.9.2/extensions/modules.

$ cd modules
$ mv xhprof.so /Applications/MAMP/bin/php5.2/lib/php/extensions/no-debug-non-zts-20060613/

Next, open up MAMP PRO. Hit Command-3 and add in information for the xhprof extension.

[xhprof]
extension=xhprof.so
xhprof.output_dir="/var/tmp/xhprof"

Restart MAMP and visit http://localhost/MAMP/phpinfo.php and see if xhprof is there.

We're not quite done yet. We'll need to copy ~/Desktop/xhprof-0.9.2/{xhprof_lib,xhprof_html} to some vhost in MAMP. It's really up to you. Mine are in my Drupal doc root, so if you move it the code below will need to be changed.

FINALLY, open up Drupal's index.php and make it look like below.

<?php
if (extension_loaded('xhprof')) {
    include_once
dirname(__FILE__) . '/xhprof_lib/utils/xhprof_lib.php';
    include_once
dirname(__FILE__) . '/xhprof_lib/utils/xhprof_runs.php';
   
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}

// $Id: index.php,v 1.94 2007/12/26 08:46:48 dries Exp $
....
drupal_page_footer();
if (
extension_loaded('xhprof')) {
   
$profiler_namespace = 'd6'// namespace for your application
   
$xhprof_data = xhprof_disable();
   
$xhprof_runs = new XHProfRuns_Default();
   
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
   
// url to the XHProf UI libraries (change the host name and path)
   
$profiler_url = sprintf('http://localhost/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
    print
'<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
}
?>

Now just visit your Drupal and you should see a link for Profiler output on the bottom of the page and you're done!

Tags: