You are here

function memcache_storage_debug_shutdown in Memcache Storage 7

Print debug output of memcache statistics.

1 string reference to 'memcache_storage_debug_shutdown'
memcache_storage_init in ./memcache_storage.module
Implements hook_init().

File

./memcache_storage.module, line 182
Provides hook implementation for Memcache Storage module.

Code

function memcache_storage_debug_shutdown() {

  // Don't call theme() during shutdown if the registry has been rebuilt (such
  // as when enabling/disabling modules on admin/build/modules) as things break.
  // Instead, simply exit without displaying admin statistics for this page
  // load.  See http://drupal.org/node/616282 for discussion.
  if (!function_exists('theme_get_registry') || !theme_get_registry()) {
    return;
  }

  // Try not to break non-HTML pages.
  if (function_exists('drupal_get_http_header') && !strstr(drupal_get_http_header('Content-Type'), 'html')) {
    return;
  }
  $debug_output = drupal_static('memcache_storage_debug_output');

  // Don't output any debug if it is empty.
  if (empty($debug_output)) {
    return;
  }
  $common_stats = array();
  foreach ($debug_output as $key => &$row) {
    $action = $row['action'];
    if (empty($common_stats[$action])) {
      $common_stats[$action]['action'] = $action;
      $common_stats[$action]['time'] = 0;
      $common_stats[$action]['mem'] = 0;
      $common_stats[$action]['HIT'] = 0;
      $common_stats[$action]['MISS'] = 0;
    }
    $common_stats[$action][$row['result']]++;

    // Add to the link attributes that was unable to add during
    // statistics collection, because some functions may be not loaded.
    if (!empty($row['clear_link'])) {
      $row['clear_link']['#text'] = t('Delete');
      $row['clear_link']['#options']['query'] = array(
        'token' => drupal_get_token($row['token']),
      );
    }

    // Remove token because we don't want to show it in the debug output.
    unset($row['token']);

    // Render link for clearing cache item.
    $debug_output[$key]['clear_link'] = render($row['clear_link']);

    // Do not collect statistics if previos row is same as current (for getMultiple).
    if (isset($debug_output[$key - 1]) && $debug_output[$key - 1]['timer'] == $row['timer'] && $debug_output[$key - 1]['memory'] == $row['memory']) {
      continue;
    }
    $common_stats[$action]['time'] += $row['timer'];
    $common_stats[$action]['mem'] += $row['memory'];
  }
  foreach ($common_stats as &$stats) {
    $stats['mem'] = number_format($stats['mem'] / 1024, 2);
    $stats['HIT'] = $stats['HIT'] . ' / ' . number_format($stats['HIT'] / ($stats['HIT'] + $stats['MISS']) * 100, 1) . '%';
    $stats['MISS'] = $stats['MISS'] . ' / ' . number_format($stats['MISS'] / ($stats['HIT'] + $stats['MISS']) * 100, 1) . '%';
  }

  // Rebuild detailed debug output.
  $detailed_debug = array();
  foreach ($debug_output as $row) {
    $detailed_debug[] = array(
      'data' => $row,
      'class' => array(
        $row['result'] == 'MISS' ? 'miss' : 'hit',
      ),
    );
  }
  $stats_table = array(
    '#theme' => 'table',
    '#header' => array(
      t('Action'),
      t('Total time, ms'),
      t('Total memory used, MB'),
      t('Total hits / %'),
      t('Total misses / %'),
    ),
    '#rows' => $common_stats,
    '#attributes' => array(
      'class' => array(
        'memcache-storage-common-debug',
      ),
    ),
  );
  $debug_table = array(
    '#theme' => 'table',
    '#header' => array(
      t('Action'),
      t('Time, ms'),
      t('Used memory, KB'),
      t('Result'),
      t('Cache bin'),
      t('Cache ID'),
      t('Memcache key'),
      t('Cache action'),
    ),
    '#rows' => $detailed_debug,
    '#attributes' => array(
      'class' => array(
        'memcache-storage-detailed-debug',
      ),
    ),
  );
  print '<h2>' . t('Memcache Storage debug output') . '</h2>';
  print t('Request URL: !url', array(
    '!url' => '<strong>' . url($_GET['q'], array(
      'absolute' => TRUE,
    )) . '</strong>',
  ));
  print render($stats_table) . render($debug_table);
}