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<br>$ mkdir includes<br>$ cd !$<br>$ wget http://us.php.net/get/php-5.2.13.tar.gz/from/this/mirror<br>$ tar zxvf php-5.2.13.tar.gz<br>$ mv php-5.2.13 php<br>$ 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"<br>$ export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET<br>$ ./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<br>$ wget http://pecl.php.net/get/xhprof-0.9.2.tgz<br>$ tar zxvf xhprof-0.9.2.tgz<br>$ cd xhprof-0.9.2/extensions<br>$ /Applications/MAMP/bin/php5/bin/phpize<br>$ ./configure CFLAGS="-arch i386" --with-php-config="/Applications/MAMP/bin/php5.2/bin/php-config"<br>$ make<br>$ 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<br>$ 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]<br>extension=xhprof.so<br>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.
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">if (</span><span style="color: #0000BB">extension_loaded</span><span style="color: #007700">(</span><span style="color: #DD0000">'xhprof'</span><span style="color: #007700">)) {<br> include_once </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">) . </span><span style="color: #DD0000">'/xhprof_lib/utils/xhprof_lib.php'</span><span style="color: #007700">;<br> include_once </span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">) . </span><span style="color: #DD0000">'/xhprof_lib/utils/xhprof_runs.php'</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">xhprof_enable</span><span style="color: #007700">(</span><span style="color: #0000BB">XHPROF_FLAGS_CPU </span><span style="color: #007700">+ </span><span style="color: #0000BB">XHPROF_FLAGS_MEMORY</span><span style="color: #007700">);<br>}<br><br></span><span style="color: #FF8000">// $Id: index.php,v 1.94 2007/12/26 08:46:48 dries Exp $<br></span><span style="color: #007700">....<br></span><span style="color: #0000BB">drupal_page_footer</span><span style="color: #007700">();<br>if (</span><span style="color: #0000BB">extension_loaded</span><span style="color: #007700">(</span><span style="color: #DD0000">'xhprof'</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$profiler_namespace </span><span style="color: #007700">= </span><span style="color: #DD0000">'d6'</span><span style="color: #007700">; </span><span style="color: #FF8000">// namespace for your application<br> </span><span style="color: #0000BB">$xhprof_data </span><span style="color: #007700">= </span><span style="color: #0000BB">xhprof_disable</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$xhprof_runs </span><span style="color: #007700">= new </span><span style="color: #0000BB">XHProfRuns_Default</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$run_id </span><span style="color: #007700">= </span><span style="color: #0000BB">$xhprof_runs</span><span style="color: #007700">-></span><span style="color: #0000BB">save_run</span><span style="color: #007700">(</span><span style="color: #0000BB">$xhprof_data</span><span style="color: #007700">, </span><span style="color: #0000BB">$profiler_namespace</span><span style="color: #007700">);<br> </span><span style="color: #FF8000">// url to the XHProf UI libraries (change the host name and path)<br> </span><span style="color: #0000BB">$profiler_url </span><span style="color: #007700">= </span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://localhost/xhprof_html/index.php?run=%s&source=%s'</span><span style="color: #007700">, </span><span style="color: #0000BB">$run_id</span><span style="color: #007700">, </span><span style="color: #0000BB">$profiler_namespace</span><span style="color: #007700">);<br> print </span><span style="color: #DD0000">'<a href="'</span><span style="color: #007700">. </span><span style="color: #0000BB">$profiler_url </span><span style="color: #007700">.</span><span style="color: #DD0000">'" target="_blank">Profiler output</a>'</span><span style="color: #007700">;<br>}<br></span><span style="color: #0000BB">?></span></span>
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: