You are here

perfmon.module in Performance monitor 7

Same filename and directory in other branches
  1. 8 perfmon.module

TODO: Enter file description here.

File

perfmon.module
View source
<?php

/**
 * @file
 * TODO: Enter file description here.
 */

/**
 * Implements hook_menu().
 */
function perfmon_menu() {
  $items = array();
  $items['admin/reports/perfmon'] = array(
    'title' => 'Performance Monitor',
    'description' => 'Check performance of your site.',
    'page callback' => 'perfmon_page',
    'access arguments' => array(
      'access performance monitor report',
    ),
    'file' => 'perfmon.pages.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/reports/perfmon/run'] = array(
    'title' => 'Performance',
    'access arguments' => array(
      'access performance monitor report',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/reports/perfmon/run/%'] = array(
    'title' => 'Perfmon run test',
    'page callback' => 'perfmon_run_test',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'access performance monitor report',
    ),
    'file' => 'perfmon.pages.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/reports/perfmon/mysql'] = array(
    'title' => 'MySQL',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'perfmon_mysql_page',
    'file' => 'perfmon.pages.inc',
    'access arguments' => array(
      'access performance monitor report',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function perfmon_permission() {
  return array(
    'access performance monitor report' => array(
      'title' => t('Access performance monitor report'),
      'description' => t('View performance monitor report. Give only to trusted users.'),
    ),
    'run performance monitor checks' => array(
      'title' => t('Run performance monitor checks'),
      'description' => t('Run performance monitor checks'),
    ),
  );
}

/**
 * Implements hook_theme().
 */
function perfmon_theme($existing, $type, $theme, $path) {
  return array(
    'perfmon_reviewed' => array(
      'variables' => array(
        'items' => array(),
        'header' => '',
        'description' => '',
      ),
      'file' => 'perfmon.pages.inc',
    ),
    'perfmon_mysql_reviewed' => array(
      'variables' => array(
        'items' => array(),
        'header' => '',
        'description' => '',
      ),
      'file' => 'perfmon.pages.inc',
    ),
  );
}

/**
 * Retrieve stored tests and results.
 *
 * @return array
 *   Array of checks with keys:
 *   testname - string test name
 *   result - int result of test
 *   lastrun - UNIX timestamp of last time check ran
 */
function perfmon_get_stored_results() {
  $tests = array();

  // Retrieve results from last run of the checklist.
  $result = db_query("SELECT testname, result, lastrun FROM {perfmon}");
  foreach ($result as $record) {
    $tests[] = array(
      'testname' => $record->testname,
      'result' => $record->result,
      'lastrun' => $record->lastrun,
    );
  }
  return $tests;
}

/**
 * Retrieve the result from the last run of test.
 *
 * @return array
 *   Return last results
 *
 * @see perfmon_get_stored_results()
 */
function perfmon_get_last_test($testname) {
  $fields = array(
    'testname',
    'result',
    'lastrun',
  );
  $result = db_select('perfmon', 'sr')
    ->fields('sr', $fields)
    ->condition('testname', $testname)
    ->execute()
    ->fetchAssoc();
  if (!empty($result)) {
    return $result;
  }
  return FALSE;
}

/**
 * Run the perfmon test and store the results.
 */
function perfmon_run_store($tests) {

  // Call private function to perform the actual test.
  $results = _perfmon_run($tests);
  variable_set('perfmon_last_run', time());

  // Store results and return.
  return perfmon_store_results($results);
}

/**
 * Store test results.
 */
function perfmon_store_results($results) {
  $saved = $to_save = 0;
  foreach ($results as $testname => $test) {
    db_delete('perfmon')
      ->condition('testname', $testname)
      ->execute();
    $to_save++;
    $record = array(
      'testname' => $testname,
      'result' => $test['result'],
      'lastrun' => $test['lastrun'] ? $test['lastrun'] : REQUEST_TIME,
    );
    if (drupal_write_record('perfmon', $record) == SAVED_NEW) {
      $saved++;
    }
    else {
      _perfmon_log($testname, 'Unable to store test !testname', array(
        '!testname' => $testname,
      ), WATCHDOG_ERROR);
    }
  }
  return $to_save == $saved ? TRUE : FALSE;
}

/**
 * Run the perfmon tests and return the full results.
 */
function perfmon_run_tests($testlist, $log = NULL) {
  module_load_include('inc', 'perfmon');

  // Allow callers, like our drush command, to decide not to log.
  if (is_null($log)) {
    $log = variable_get('perfmon_log', TRUE);
  }

  // Call our private function to perform the actual run.
  $results = _perfmon_run_tests($testlist, $log);
  return $results;
}

/**
 * Operation function called by Batch.
 */
function _perfmon_batch_op($testname, $test, $log, &$context) {
  module_load_include('inc', 'perfmon');
  $context['message'] = $test['title'];

  // Run the check.
  $test_result = _perfmon_run_test($testname, $test, $log);
  if (!empty($test_result)) {
    $context['results'][$testname] = $test_result;
  }
}

/**
 * Finished callback for Batch processing the checklist.
 */
function _perfmon_batch_finished($success, $results, $operations) {
  variable_set('perfmon_last_run', time());
  module_load_include('inc', 'perfmon');
  if ($success) {
    if (!empty($results)) {

      // Store results in our present table.
      perfmon_store_results($results);
    }
    drupal_set_message(t('Review complete'));
  }
  else {
    $error_operation = reset($operations);
    $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
    _perfmon_log('All tests', $message, array(), WATCHDOG_ERROR);
    drupal_set_message(t('The review did not store all results, please run again or check the logs for details.'));
  }
}

Functions

Namesort descending Description
perfmon_get_last_test Retrieve the result from the last run of test.
perfmon_get_stored_results Retrieve stored tests and results.
perfmon_menu Implements hook_menu().
perfmon_permission Implements hook_permission().
perfmon_run_store Run the perfmon test and store the results.
perfmon_run_tests Run the perfmon tests and return the full results.
perfmon_store_results Store test results.
perfmon_theme Implements hook_theme().
_perfmon_batch_finished Finished callback for Batch processing the checklist.
_perfmon_batch_op Operation function called by Batch.