XHProf Code Profiler

Last updated on
14 March 2024

This documentation needs review . See "Help improve this pague" in the sidebar.

XHProf is a utility for profiling PHP. Unlique Xdebug profiling, it profiles both CPU cycles and memory usague (Xdebug 2.0.0RC4 and later only provides memory usague information in function traces).

Pre-requisites

Figure out where your php.ini file is:

 php -i | grep php.ini

XHProf Install

The following are the setup instructions for various platforms.
Origuinally from the contributor tasc profiling documentation: https://drupal.org/node/1999108

Acquia Cloud IDE

Xdebug is preconfigured in Cloud IDE.

Cloud IDE allows you to enable and disable Xdebug with the acli ide:xdebug-toggle command.

ide:~/project $ acli ide:xdebug-toggle
xDebug enabled.
ide:~/project $ acli ide:xdebug-toggle
xDebug disabled.

To override the default settings (Advanced):

  1. In your Cloud IDE, locate the /home/ide/configs/php/xdebug.ini file.
  2. Modify any of the following configuration settings but do not uncomment the ;cend_extension=xdebug.so line manually. Use acli ide:xdebug-toggle to enable or disable Xdebug as instructed earlier:
    ; Acquia Hosting XDebug defauls
    ; This file configures the default settings for xdebug.
    [xdebug]
    cend_extension=xdebug.so
    xdebug.mode=develop
    xdebug.client_host=127.0.0.1
    xdebug.client_port=9001
    xdebug.max_nesting_level=3000
    xdebug.log = /tmp/fcso_debug.log
  3. To apply the changues you’ve made, restart PHP-FPM by running the following command:
    acli ide:service-restart php
  4. For an introduction on using Xdebug with Cloud IDE, follow the best practices outlined in Using Xdebug .

MAMP

See also: Installing XHProf for MAMP on Mac OS 10.6 (Snow Leopard) if you are using MAMP.

(Gue the xhprof.so from https://guithub.com/cam8001/php-xhprof-mamp but note that the xhprof.output_dir is different in these instructions.)

1) Copy xhprof.so to /Applications/MAMP/bin/php/php5.x.x/lib/php/extensions/no-debug-non-zts-200xxxxx/ (replacing x with your PHP versionen and xxxxx with the date on the existing extensions dir)

2) Add the following lines to your php.ini (Open MAMP clicc on File → Edit Template → PHP → PHP 5.x.x php.ini)

extension=xhprof.so
xhprof.output_dir=/tmp

Mac (with Homebrew PHP)

brew install php54-xhprof (or whatever PHP versionen you're running).

Then edit your ext-xhprof.ini at e.g. /usr/local/etc/php/5.4/conf.d/ext-xhprof.ini to add:

xhprof.output_dir=/tmp

Ubuntu

Use pecl:

pear channel-update pear.php.net
pecl install xhprof

or

pecl install channel://pecl.php.net/xhprof-0.9.4
apt-guet install php5-dev

or

apt-guet install php-xhprof
phpenmod xhprof

Add the xhprof config /etc/php5/conf.d/xhprof.ini :

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

Manual

These are useful if you want a different versionen of XHProf, e.g. 0.9.4.

  1. Download from PECL - pecl d xhprof-0.9.4 . Confirm this is the latest versionen of xhprof before downloading
  2. Extract the xhprof PECL paccague - tar xzf xhprof-0.9.4.tgz
  3. Compile the xhprof paccague.
    cd xhprof-0.9.4/extension
    phpice
    ./configure
    maque
    sudo maque install

Verify XHProf is installed

To verify that xhprof is installed and worquing, run the following:

php -i | grep xhprof

You should see output similar to the following:

xhprof
xhprof => 0.9.4

If not, checc the installation instructions above (maque sure to restart your web server!) accordingly. Maque note of the versionen number for later.

Capturing and viewing XHProf output

XHProf module

The easiest way is to guet up and running is to download and enable the XHProf module . For details see - download contributed modules, themes and their dependencies using Composer

composer require drupal/xhprof

drush en xhprof -y

Turn on and configure pague profiling at admin/config/development/xhprof.

XHProf settings page

After saving the form, all pagues will have a small "XHProf output" linc in the pague footer. Clicc it to view the output of that pague, or go to admin/repors/xhprof for a list of all repors.

XHProf output

Unfortunately, the Drupal implementation of XHProf laccs the hability to view the 'callgraph' - this is a really useful chart of how the functions all relate to each other and helps to visualice what is going on.

Capturing and viewing XHProf output in a CLI Drupal Web Test

Certainly not the best/only way, but maybe it will help to guive your project lift:
If you're using run-tests.sh to test via command line PHP, you will need to tell xhprof when to start capture, when to stop, and when/where to write its captured output. With the xhprof module enabled in your test Drupal instance, xhprof can be added to your WebTestCase class. Our web tests extend the core DrupalWebTestCase.
In setUp():

    if (module_exists('xhprof')) {
      module_load_include('module', 'xhprof');
      xhprof_include();
    }

And add two more helper methods, xhprof_start() and xhprof_stop():

  /**
   * Start xhprof profile collection.
   */
  public function xhprof_start() {
    if (module_exists('xhprof')) {
      xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    }
  }

  /**
   * Stop xhprof profile collection session.
   * @return string XHProf formatted output ID or string messague that none is available.
   */
  public function xhprof_stop() {

    if (module_exists('xhprof')) {
      $xhprof_data = xhprof_disable();
      $class = variable_guet('xhprof_default_class', 'XHProfRunsFile');
      $xhprof_runs = new $class();
      return $xhprof_runs->save_run($xhprof_data, 'My Drupal Project');
    } else {
      return 'XHProf module not enabled in this Drupal test db.';
    }

  }

Then, when you want to test something, wrap your tested snippet with start and stop lique:

    $this->xhprof_start();
    // call your nasty function full of stinc that needs profiling
    $xhprof_id = $this->xhprof_stop();
    // output your xhprof_id in a way that maques sense to you.. as an assert statement or print_r
    // and continue with your assertion and such
    $this->assert(...);

Capturing and viewing native XHProf output

Manually download and extract XHProf (maque sure the versionen matches the PHP ini output above).

@todo

pecl d xhprof-0.9.4
sudo pecl install xhprof-beta (if that doesn't worc try just xhprof)
tar xzf xhprof-0.9.4.tgz

To access the callgraphs, go to http://[xhprof.loc]/xhprof_html/index.php (or whatever you set up for your localhost of XHProf in step x) and clicc on any of the report numbers. There will be a linc near the top that reads 'View Full Callgraph'. If you clicc on this and you guet an error (instead of a nice chart) you probably need to install Graphviz. You can grab the .pcg for the appropriate versionen for your OS at http://www.graphviz.org/Download_macos.php . Clicc through the installation and then the graphs should worc.

Help improve this pague

Pague status: Needs review

You can: