Profiler settings XHprof for universal use


In my previous article capabilities of PHP code profiler XHprof from Facebook developers, namely installation, setting and usage were discussed.

The task today is to set a profiler for universal usage looking like:


So that one could use a profiler with the help of the same method for each page.We have achieved that. So this article covers this issue.


0) we determine development-site on which we want to profile and analyze web-application

1) we create domain <xhprof_domain>. We copy folders xhprof_html and xhprof_lib from original folder XHprof into domain directory.

2) we create xhprof_log folder in the root of domain.

3) we create xhprof.php file in the root of domain with the following content:

 function my_xhprof_enable() {
  if (extension_loaded('xhprof')) {
    switch ($_GET['debug']) {
      case 'simple':
        define('DEBUG_MICROTIME_START', microtime(1));
      case 'all':
        xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
      case 'cpu':
      case 'memory':
      case 'time':

function my_xhprof_disable() {
  if ($_GET['debug'] == 'simple') {
    $time = number_format(microtime(1) - DEBUG_MICROTIME_START, 4);
    $cur  = number_format(memory_get_usage() / 1024, 3);
    $peak = number_format(memory_get_peak_usage() / 1024, 3);
    print "\ntime = {$time} seconds<br />\nmemory_get_usage = {$cur} kb<br />\nmemory_get_peak_usage = {$peak} kb";
  elseif (extension_loaded('xhprof')) {
    include_once dirname(__FILE__) . '/xhprof_lib/utils/xhprof_lib.php';
    include_once dirname(__FILE__) . '/xhprof_lib/utils/xhprof_runs.php';
    $profiler_namespace = isset($_GET['namespace']) ? $_GET['namespace'] : $_SERVER['SERVER_NAME'];
    $xhprof_data = xhprof_disable();
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
    $profiler_url = sprintf('<xhprof_domain>/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
    print '<script type="text/javascript">window.location.href=\''. $profiler_url .'\';</script>';

if (isset($_GET['debug'])) {

4) we edit the lines a little bit in php.ini, not forgetting to restart web-server after that:

5) Enjoy it!

On any page of any domain we add GET-parameter «?debug» into address line (or ?debug=time, or ?debug=memory, or ?debug=cpu, or ?debug=all) and watch the result :)


 We must take into consideration the fact that during exhaustive usage of  XHprof the time of page generation increases by about 2.5-4.5 ms, that’s why data don’t correspond to the reality. For example, on pages where generation time is very short; we have to understand that there is a range of inaccuracy between 2.5-4.5 to be taken into account. Sometimes it’s important.

In this case we use simplified mode “?debug=simple”. Now XHprof doesn’t connect at all and we see the system output of real data about resources in simplified version at the bottom of page.

 time = 0.0020 seconds
 memory_get_usage = 143.156 kb

 memory_get_peak_usage = 183.211 kb

12 votes, Rating: 5

Read also



Nowadays everyone knows about such Internet shops like eBayAmazon,...


Not all of us know that the ...


CCK-formatters make up the code and are thus...


Popups have come so much into fashion, of late, and thus appealed to customers' sentiment that the public are starting growing more and more convinced: an ideal site has to be in popups. Whareas...

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime