Using PHP XHPROF with Drupal

***Update***

If you're using Drupal 7, there is a lot simple approach now : http://drupal.org/project/XHProf

What is XHProf...?

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.

(From http://mirror.facebook.net/facebook/xhprof/doc.htm)

Here's how to add the very useful XHPROF - profiler to your DEVELOPMENT instance of your Drupal site

1. Install the XHPROF on your dev instance in php by downloading it.

http://pecl.php.net/package/xhprof

And installing it manually...

2. Add to the .htaccess file at the bottom =>

php_value auto_prepend_file /var/www/xhprof/header.php
php_value auto_append_file /var/www/xhprof/footer.php

3. Add these files to where you want on your test setup, and so that it mirrors the above two lines from your .htaccess file =>

footer.php:

<?php

if (extension_loaded('xhprof')) {
       $profiler_namespace = "turner";
       $xhprof_data = xhprof_disable();
       $xhprof_runs = new XHProfRuns_Default();
       $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
       $profiler_url =
sprintf('http://192.168.0.1/xhprof/xhprof_html/index.php?run=%s&source=%s',
$run_id, $profiler_namespace);
       printf('<br><br>&nbsp;&nbsp;&nbsp;<a href="%s"
target="_blank">Profiler output</a><br>'
, $profiler_url);
} else {
       echo "<!-- profiler not enabled -->";
}

header.php:

<?php

if (extension_loaded('xhprof')) {
       include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_lib.php";
       include_once "/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php";
       xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}

*** please note : http://192.168.0.1/xhprof/xhprof_html/index.php - is relative to where you have put your xhprof folders.

4. Add the following to your php.ini =>

extension=xhprof.so
xhprof.output_dir="/tmp"

5. And restart apache2

And now at the bottom of your pages you should have "Profiler output" at the bottom of your pages - click to see the data.




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.
Bg