Using XHProf to evaluate code performance

XHProf is a hierarchical profiler for PHP. Using XHProf, you can evaluate your website's code performance and identify functions that may be running slowly or using an excessive portion of your server resources. On Acquia Cloud, you should use XHProf only on your Dev and Stage environments. Be sure to disable XHProf when you're not actively evaluating your website, because the tool uses additional server resources and can adversely affect your site's performance.

For more information about XHProf, see:

If you are using Mac OS X, you may find XHProf with Acquia Dev Desktop in Mac OS X useful.

Enabling XHProf for your website

To enable XHProf for your website:

  1. Install and enable the XHProf module from Drupal.org.
  2. Go to the Configuration > XHProf settings (admin/config/development/xhprof) page in your Drupal website.
  3. Select the Enable profiling of page views and drush requests check box.
  4. Click Save configuration.

Using XHProf to profile your website

When using XHProf, it's best to view your website as an authenticated user, rather than as an administrator. This gives you the most valid and useful results. If you choose to do this, remember to add the permission for "Access XHProf data" to the Authenticated User role in your development environment.

You will see that after you have installed and enabled the XHProf module as well as visited some of the URLs, the output of the XHProf module is available in the Drupal XHProf reports page (Reports > XHProf runs at admin/reports/xhprof).

Note that the destination of the raw XHProf output is defined by the xhprof.output_dir PHP configuration. The default value for sites in Acquia Cloud is /mnt/tmp/site.env/. This value works for all single web environments like dev and staging. However, if you need to use XHProf on a production environment, you should change the value of xhprof.output_dir using an ini_set function in your settings.php file:

ini_set('xhprof.output_dir', '/mnt/gfs/' . $_ENV['AH_SITE_NAME'] . '/xhprof');

Remember that enabling XHProf can lead to excessive file generation, so it is best to periodically clean out the output directory and/or disable the module except when actively profiling.

You can also access the profiling output of the XHProf module from each page. This is possible once the previously mentioned permission is given to the Authenticated User role. When the XHProf module is enabled and active, XHProf will then generate a link that you can find in the bottom-left corner of each non-admin page. This link goes directly to the Drupal XHProf reports page ( Reports > XHProf runs at admin/reports/xhprof).

XHProf output link

The Drupal XHProf report displays a listing of each function called by the page. You can sort the report by columns to identify the most resource-consuming functions. For example, you can sort functions in the table view by clicking Excl. Wall Time. This sorts PHP functions by which ones are using the most CPU time in descending order. Alternatively you can sort by Excl. MemUse (bytes) to see which functions are consuming the most memory.

Clicking a function name in the Drupal XHProf report shows you the function's parent functions.

Once you have identified any functions that may be using an inordinate amount of resources, you can investigate possible solutions that are focused on those functions. For example, if the function is called in a Drupal contributed or core module, you could search drupal.org for module patches that address performance problems related to the identified functions. You might also consider whether the entities calling the functions can be cached more effectively, so that the function doesn't need to be called as frequently.

Turning off profiling

Since XHProf can have a negative impact on your site's performance, it's important to stop it from profiling when you are not using it. You don't need to uninstall the XHProf module or even disable it. Instead, to turn off profiling:

  1. Go to the Configuration > Development > XHProf (admin/config/development/xhprof) page in your Drupal website.
  2. Deselect the Enable profiling of page views and drush requests check box.
  3. Click Save configuration.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Contact supportStill need assistance? Contact Acquia Support