drupal 7 xhprof php profiling results example

Sometimes we need to provide php profiling to find bottlenecks inside our site. Xhprof extension is one of the best solutions. Here is example of found small issue and given graphs before and after chnages.

Graph before changes

xhprof profiling result before changes

After changes (I just added module_load_include into condition to call it not so often).

after changes xhprof result

 

/**
 * Implements imagestyle_preprocess
 * before
 */
function yoursite_preprocess_image_style(&$variables) {
  module_load_include('inc', 'ctools', 'includes/cleanstring');
  $options = array(
    'separator' => ' ',
    'replacements' => array('\'' => ''),
  );
  $except_styles = array(
    '650x360_rotator',
    'large',
  );
  if (!in_array($variables['style_name'], $except_styles)) {
    $variables['alt'] = $variables['alt'] ? ctools_cleanstring($variables['alt'] . ' thumbnail', $options) : '';
    $variables['title'] = $variables['title'] ? ctools_cleanstring($variables['title'] . ' thumbnail', $options) : '';
  }
}

/**
 * Implements imagestyle_preprocess
 * after
 */
function yoursite_preprocess_image_style(&$variables) {
  $options = array(
    'separator' => ' ',
    'replacements' => array('\'' => ''),
  );
  $except_styles = array(
    '650x360_rotator',
    'large',
  );
  if (!in_array($variables['style_name'], $except_styles)) {
    / / Put include into condition.
    module_load_include('inc', 'ctools', 'includes/cleanstring');
    $variables['alt'] = $variables['alt'] ? ctools_cleanstring($variables['alt'] . ' thumbnail', $options) : '';
    $variables['title'] = $variables['title'] ? ctools_cleanstring($variables['title'] . ' thumbnail', $options) : '';
  }
}

So as you can see 76 calls was changed to 31. So we definitely need to use php profiling. For windows user can be used xdebug, but I don't like it. I was windows user and I changed os to linux mint to use xhprof, so believe me this extension is perfect for php profiling. (I just added module_load_include into condition to call it not so often).