There often comes a time when you wonder why your page is occasionally experiencing a PHP Out Of Memory error with Drupal. Without profiling tools, you have no idea why Drupal is consuming the memory it is. Luckily the team at Facebook have created the tool XHProf.
How to install XHProf with apache
This can be done entirely with PECL:
Create the directory for the data to live in, I choose to use /tmp but you can choose another location for more permanent storage
If you use apache, then setting this up is trivial. First create the settings file (included automatically)
Then let apache know that all URLs beginning with “xhprof_html” are indeed XHProf URLs.
Drupal 7 integration with Devel
The devel module makes the integration with XHProf as simple as enabling the module and setting some variables.
And there you have it, now all your pages will be profiled as they are generated, and Devel will embed a link at the bottom of the page in order to view the metrics, and callgraph trace.
Here are some images snapped from a recent project on a complex page generation:
The metrics page lists all functions, and can be sorted numerous ways:
The callgraph is useful to find the critical path in terms of page load: