You are here

function performance_view_summary in Performance Logging and Monitoring 7

Same name and namespace in other branches
  1. 5 performance.module \performance_view_summary()
  2. 6.2 performance.module \performance_view_summary()
  3. 6 performance.module \performance_view_summary()
  4. 7.2 performance.module \performance_view_summary()
1 string reference to 'performance_view_summary'
performance_menu in ./performance.module
Implementation of hook_menu().

File

./performance.module, line 956
Logs detailed and/or summary page generation time and memory consumption for page requests. Copyright Khalid Baheyeldin 2008 of http://2bits.com

Code

function performance_view_summary() {
  drupal_set_title(t('Performance logs: Summary'));
  global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;

  // array of element-keyed number of rows per page
  $go = FALSE;
  $rows = $data_list = array();
  $source = '';

  // Get selected source.
  if (isset($_GET['source'])) {
    $source = check_plain($_GET['source']);
  }
  else {

    // Get data from first active source.
    foreach (performance_data_stores() as $store => $data) {
      if ($data['#enabled']) {
        $source = $store;
        break;
      }
    }
  }

  // Build table header.
  $header = array(
    array(
      'data' => t('Path'),
      'field' => 'path',
    ),
    array(
      'data' => t('Last access'),
      'field' => 'last_access',
    ),
    array(
      'data' => t('# accesses'),
      'field' => 'num_accesses',
    ),
    array(
      'data' => t('MB Memory (Max)'),
      'field' => 'bytes_max',
    ),
    array(
      'data' => t('MB Memory (Avg)'),
      'field' => 'bytes_avg',
    ),
    array(
      'data' => t('ms (Max)'),
      'field' => 'ms_max',
    ),
    array(
      'data' => t('ms (Avg)'),
      'field' => 'ms_avg',
    ),
  );
  if (variable_get(PERFORMANCE_QUERY_VAR, 0)) {
    $header[] = array(
      'data' => t('Query ms (Max)'),
      'field' => 'query_timer_max',
    );
    $header[] = array(
      'data' => t('Query ms (Avg)'),
      'field' => 'query_timer_avg',
    );
    $header[] = array(
      'data' => t('Query Count (Max)'),
      'field' => 'query_count_max',
    );
    $header[] = array(
      'data' => t('Query Count (Avg)'),
      'field' => 'query_count_avg',
    );
  }
  $pager_height = 50;

  // Fetch data. Exception here when logging to DB. Should get rid of this one
  // still. May not be possible though...
  if ($source == 'db') {
    $data_list = performance_db_get_data($header, $pager_height);
    $go = TRUE;
  }
  elseif (!empty($source)) {
    $data_list = call_user_func('performance_get_data_' . $source);
    $go = TRUE;
  }
  if (!$go) {
    return t('Summary performance log is not enabled. Go to the !link to enable it.', array(
      '!link' => l(t('settings page'), PERFORMANCE_SETTINGS),
    ));
  }
  $total_rows = $shown = $last_max = $total_bytes = $total_ms = $total_accesses = 0;
  $last_min = REQUEST_TIME;
  $threshold = variable_get('performance_threshold_accesses', 0);

  // TODO: make this work properly!
  // Set up pager since this is not done automatically when not using DB.
  if ($source != 'db' && $data_list) {
    $page = isset($_GET['page']) ? sprintf('%d', $_GET['page']) : 0;
    $pager_page_array = array(
      0 => $page,
    );
    $pager_total_items = array(
      0 => count($data_list),
    );
    $pager_limits = array(
      0 => $pager_height,
    );
    $pager_total = array(
      0 => ceil($pager_total_items[0] / $pager_limits[0]),
    );

    // Extract the data subset we need.
    $data_list = array_slice($data_list, $page * $pager_height, $pager_height);
  }

  // Format data into table.
  foreach ($data_list as $data) {

    // Cast to array because of the DB API now returning row objects by default.
    $data = is_object($data) ? (array) $data : $data;
    $total_rows++;
    $last_max = max($last_max, $data['last_access']);
    $last_min = min($last_min, $data['last_access']);

    // Calculate running averages.
    $total_bytes += $data['bytes_avg'];
    $total_ms += $data['ms_avg'];
    $total_accesses += $data['num_accesses'];
    $row_data = array();
    if ($data['num_accesses'] > $threshold) {
      $shown++;
      $row_data[] = l(check_plain($data['path']), $data['path']);
      $row_data[] = format_date($data['last_access'], 'small');
      $row_data[] = $data['num_accesses'];
      $row_data[] = number_format($data['bytes_max'] / 1024 / 1024, 2);
      $row_data[] = number_format($data['bytes_avg'] / 1024 / 1024, 2);
      $row_data[] = number_format($data['ms_max'], 1);
      $row_data[] = number_format($data['ms_avg'], 1);
      if (variable_get(PERFORMANCE_QUERY_VAR, 0)) {
        $row_data[] = number_format($data['query_timer_max'], 1);
        $row_data[] = number_format($data['query_timer_avg'], 1);
        $row_data[] = $data['query_count_max'];
        $row_data[] = $data['query_count_avg'];
      }
    }
    $rows[] = array(
      'data' => $row_data,
    );
  }
  $output = '';
  if ($threshold) {
    $output .= t('Showing !shown paths with more than !threshold accesses, out of !total total paths.', array(
      '!threshold' => $threshold,
      '!shown' => $shown,
      '!total' => $total_rows,
    )) . '<br/>';
  }
  else {
    $output .= t('Showing all !total paths.', array(
      '!total' => $total_rows,
    )) . '<br/>';
  }

  // Protect against divide by zero.
  if ($total_rows > 0) {
    $mb_avg = number_format($total_bytes / $total_rows / 1024 / 1024, 1);
    $ms_avg = number_format($total_ms / $total_rows, 2);
  }
  else {
    $mb_avg = 'n/a';
    $ms_avg = 'n/a';
  }
  $output .= t('Average memory per page: !mb_avg MB', array(
    '!mb_avg' => $mb_avg,
  )) . '<br/>';
  $output .= t('Average duration per page: !ms_avg ms', array(
    '!ms_avg' => $ms_avg,
  )) . '<br/>';
  $output .= t('Total number of page accesses: !accesses', array(
    '!accesses' => $total_accesses,
  )) . '<br/>';
  $output .= t('First access: !access.', array(
    '!access' => format_date($last_min, 'small'),
  )) . '<br/>';
  $output .= t('Last access: !access.', array(
    '!access' => format_date($last_max, 'small'),
  ));
  $output .= performance_sources_switcher($source);

  // Return a renderable array.
  return array(
    'general_info' => array(
      '#prefix' => '<p>',
      '#markup' => $output,
      '#suffix' => '</p><p>&nbsp;</p>',
    ),
    'query_data_summary' => array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#sticky' => TRUE,
      '#empty' => t('No statistics available yet.'),
    ),
    'pager' => array(
      '#theme' => 'pager',
      '#quantity' => $pager_height,
    ),
  );
}