You are here

function drush_performance_summary in Performance Logging and Monitoring 7

Same name and namespace in other branches
  1. 6.2 performance.drush.inc \drush_performance_summary()
  2. 6 performance.drush.inc \drush_performance_summary()
  3. 7.2 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
Implements hook_drush_command().

File

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

Code

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

  // Data from last 15 minutes by default.
  $timestamp = REQUEST_TIME - 15 * 60;

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

    // 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;
    }

    // Get actual data.
    $data_list = drush_performance_db_get_data('performance_summary', $arguments);
  }
  else {
    $data_list = call_user_func('performance_get_data_' . $source['key'], $timestamp);
  }

  // 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 = REQUEST_TIME;
  foreach ($data_list as $data) {

    // Cast to object because of the DB API now returning row objects by default.
    $data = is_array($data) ? (object) $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[] = 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 (count($rows) == 1) {
    return drush_print(dt('No statistics available yet.'));
  }
  else {
    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");
    if ($source['key'] == 'db') {
      drush_print("\33[1m" . dt('Performance log summary from !source: !rows entries ordered by !column, !direction', array(
        '!source' => $source['name'],
        '!rows' => count($rows) - 1,
        '!column' => $arguments['orderby'],
        '!direction' => $arguments['direction'],
      )) . "\33[0m", 1);
    }
    else {

      // Arguments are currently only supported when logging to the database.
      if (is_array($args) && count($args) > 0) {
        drush_log(dt('The passed arguments only work when the data source is the Drupal database. They have been ignored!'), 'warning');
      }
      drush_print("\33[1m" . dt('Performance log summary from !source for the last 15 minutes.', array(
        '!source' => $source['name'],
      )) . "\33[0m", 1);
    }
  }
  drush_print_table($rows, TRUE);
}