You are here

function drush_performance_summary in Performance Logging and Monitoring 6.2

Same name and namespace in other branches
  1. 6 performance.drush.inc \drush_performance_summary()
  2. 7.2 performance.drush.inc \drush_performance_summary()
  3. 7 performance.drush.inc \drush_performance_summary()

Summary page callback. Differs a little from the version in the module because drush_print_table() works differently.

1 string reference to 'drush_performance_summary'
performance_drush_command in ./performance.drush.inc
Implementation of hook_drush_command().

File

./performance.drush.inc, line 57
Drush integration for the Performance module.

Code

function drush_performance_summary() {
  $args = func_get_args();

  // Collect arguments.
  $orderby = 'last_access';
  $columns = array(
    'path',
    'last_access',
    'bytes_max',
    'bytes_avg',
    'ms_max',
    'ms_avg',
    'num_accesses',
  );
  if (variable_get(PERFORMANCE_QUERY_VAR, 0)) {
    $columns[] = 'query_count_max';
    $columns[] = 'query_count_avg';
    $columns[] = 'query_timer_max';
    $columns[] = 'query_timer_avg';
  }
  $arguments = drush_performance_parse_args($args, $orderby, $columns);

  // Error thrown, abort.
  if (!is_array($arguments)) {
    return $arguments;
  }

  // Go back no more than 1 hour.
  $arguments['timestamp'] = time() - 60 * 60;

  // Get actual data.
  $data_list = performance_traverse_cache('drush_performance_get_summary', $arguments);
  $data_list = performance_sort_summary($data_list, $arguments['direction'], $arguments['orderby']);
  if (empty($data_list) && !variable_get('performance_summary', 0)) {
    return drush_set_error(dt('Summary performance log is not enabled. Go to the settings page to enable it.'));
  }
  elseif (empty($data_list)) {
    drush_print("\33[1m" . dt('No log messages available.') . "\33[0m\n", 1);
    drush_print(dt('Possible causes:'), 1);
    drush_print('- ' . dt('no data stored yet'), 2);
    drush_print('- ' . dt('all data stored is older than one hour'), 2);
    return drush_print('- $base_url ' . dt('not properly set: run drush with the --uri parameter'), 2);
  }
  elseif (!variable_get('performance_summary', 0)) {
    drush_print("\33[1;33m" . dt('Summary performance log is not enabled! Showing stored logs.') . "\33[0m\n", 1);
  }

  // Build table header.
  $header = array(
    dt('Path'),
    dt('Last access'),
    dt('# accesses'),
    dt('MB Memory (Max)'),
    dt('MB Memory (Avg)'),
    dt('ms (Max)'),
    dt('ms (Avg)'),
  );
  if (variable_get(PERFORMANCE_QUERY_VAR, 0)) {
    $header[] = dt('Query ms (Max)');
    $header[] = dt('Query ms (Avg)');
    $header[] = dt('Query Count (Max)');
    $header[] = dt('Query Count (Avg)');
  }
  $rows[] = $header;

  // Format data into table.
  $threshold = variable_get('performance_threshold_accesses', 0);
  $total_rows = $shown = $last_max = $total_bytes = $total_ms = $total_accesses = 0;
  $last_min = time();
  foreach ($data_list as $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_sum'] / $data['num_accesses'];
    $total_ms += $data['ms_sum'] / $data['num_accesses'];
    $total_accesses += $data['num_accesses'];
    $row_data = array();
    if ($data['num_accesses'] > $threshold) {
      $shown++;
      $row_data[] = check_plain($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[] = $row_data;
  }
  if (!$rows) {
    $rows[] = dt('No statistics available yet.');
  }
  if ($threshold) {
    drush_print("\n" . dt('Showing !shown paths with more than !threshold accesses, out of !total total paths.', array(
      '!threshold' => $threshold,
      '!shown' => $shown,
      '!total' => $total_rows,
    )));
  }
  else {
    drush_print("\n" . dt('Showing all !total paths.', array(
      '!total' => $total_rows,
    )));
  }

  // 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';
  }
  drush_print(dt('Average memory per page: !mb_avg MB', array(
    '!mb_avg' => $mb_avg,
  )));
  drush_print(dt('Average duration per page: !ms_avg ms', array(
    '!ms_avg' => $ms_avg,
  )));
  drush_print(dt('Total number of page accesses: !accesses', array(
    '!accesses' => $total_accesses,
  )));
  drush_print(dt('First access: !access.', array(
    '!access' => format_date($last_min, 'small'),
  )));
  drush_print(dt('Last access: !access.', array(
    '!access' => format_date($last_max, 'small'),
  )) . "\n");
  drush_print("\33[1m" . dt('Performance log summary: !rows entries ordered by !column, !direction', array(
    '!rows' => count($rows) - 1,
    '!column' => $arguments['orderby'],
    '!direction' => $arguments['direction'],
  )) . "\33[0m", 1);

  // TODO: add 'running averages' here, like in the Drupal backend.
  drush_print_table($rows, TRUE);
}