[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/dtools/bench_chart/ -> bench_chart.module (source)

   1  <?php
   2  /*
   3      version: $Id: bench_chart.module,v 1.1.2.5 2009/06/18 14:39:22 kenorb Exp $
   4      author: kenorb@gmail.com
   5  */
   6  $bench_chart_timer = timer_start('bench_chart');
   7  bench_chart_my_hook(array('(load)', 'start'));
   8  
   9  //module_load_include('inc', 'bench_chart', 'bench_chart');
  10  
  11  function bench_chart_get_hooks() {
  12      return array(
  13      'init', 'access', 'actions_delete', 'action_info', 'action_info_alter', 'block', 'comment', 'db_rewrite_sql', 'delete', 
  14  
  15      'elements', 'file_download', 'filter', 'filter_tips', 'flush_caches', 'footer', 'form', 'forms', 'form_alter',
  16      'help', 'hook_info', 'insert', 'link', 'link_alter', 'locale', 'mail', 'mail_alter', 'menu_alter',
  17      'menu_link_alter', 'nodeapi', 'node_access_records', 'node_info', 'node_operations', 'node_type', 'ping', 'prepare', 'profile_alter', 'requirements',
  18      'schema_alter', 'search', 'search_preprocess', 'system_info_alter', 'taxonomy', 'term_path', 'theme_registry_alter', 'translated_menu_link_alter', 'disable', 'update', 
  19      'update_index', 'update_status_alter', 'user', 'user_operations', 'validate', 'view', 'watchdog', 'xmlrpc', 'content_extra_fields', 'content_build_modes', 'views_default_views',
  20      'flag_definitions', 'node_grants',
  21          /* node hooks */
  22          'load', 
  23      );
  24      //'schema',
  25      //'install',
  26      //'uninstall',
  27      //'enable',
  28      //'cron',
  29      // 'exit',   // TODO
  30      // 'theme',  // TODO
  31  }
  32  
  33  function bench_chart_gen_hooks($fname = 'bench_chart', $type = 'end') { // generate function hooks
  34      $hooks = bench_chart_get_hooks();
  35      foreach ($hooks as $hook) {
  36          if (!function_exists("$fname}_$hook}")) {
  37              $code = "function $fname}_$hook}() { return bench_chart_my_hook(array('$hook', '$type')); }";
  38              eval($code);
  39          }
  40      }
  41  }
  42  
  43  /**
  44   * Implementation of hook_boot(). Runs even for cached pages.
  45   */
  46  function bench_chart_boot() {
  47    global $drupal_path;
  48    $drupal_path = getcwd();
  49    bench_chart_my_hook(array('boot','start'));
  50    register_shutdown_function('bench_chart_shutdown');
  51    bench_chart_gen_hooks('bench_chart', 'start'); // generate function hooks
  52  } 
  53  
  54  /**
  55   * Implementation of hook_perm().
  56   */
  57  function bench_chart_perm() {
  58      bench_chart_my_hook(array('perm','start'));
  59      return array('access hook stats');
  60  }
  61  
  62   /**
  63   * Implementation of hook_menu
  64   *
  65   */
  66  function bench_chart_menu() {
  67      bench_chart_my_hook(array('menu','start'));
  68      $items['admin/reports/charts/hook_stat'] = array(
  69      'access arguments'  => array('access site reports'),
  70      'file'              => 'bench_chart_charts.inc',
  71      'page callback'     => 'bench_chart_charts',
  72      'title'             => 'Hooks',
  73      'type'              => MENU_LOCAL_TASK
  74      );
  75      $items['admin/settings/hook_chart'] = array(
  76      'access arguments'  => array('access site reports'),
  77      'file'              => 'bench_chart_conf.inc',
  78      'page callback'     => 'bench_chart_conf',
  79      'title'             => 'Hook Chart',
  80      'type'              => MENU_LOCAL_TASK
  81      );
  82      return $items;
  83  }
  84  
  85  /**
  86   * Callback of register_shutdown_function()
  87   */
  88  function bench_chart_shutdown() {
  89      /* fix Drupal path during shutdown */
  90      if (!file_exists('./includes/bootstrap.inc')) {
  91          global $drupal_path;
  92          !empty($drupal_path) ? chdir($drupal_path) : NULL; // change dir to Drupal path
  93      }
  94  
  95      bench_chart_my_hook(array('(exit)', 'end'));
  96      bench_chart_save_to_db();
  97      timer_stop('bench_chart');
  98  }
  99  
 100  function bench_chart_save_to_db() {
 101      global $hooks_run;
 102  
 103      $menu = (function_exists('menu_get_item') && function_exists('arg') && !defined('MAINTENANCE_MODE')) ? menu_get_item() : NULL; // get alias to the current menu (if menu is already loaded)
 104      $path = !empty($menu['path']) ? $menu['path'] : $_GET['q']; // set alias, if doesn't exist, get path directly
 105      $totaltime = (float)0;
 106      foreach ($hooks_run as $hook_name => $hook) { // for each executed hook
 107      $calc_time = (float)0;
 108      $count = 0;
 109      if (isset($hook['start']) && is_array($hook['start'])) {
 110          foreach ($hook['start'] as $key => $time) { // for each calculated time
 111          $count++;
 112          if (isset($hook['end'])) {
 113              $calc_time += ($hook['end'][$key] - $hook['start'][$key]);
 114              $lasttime = $hook['end'][$key];
 115          }
 116          }
 117          $totaltime += $calc_time;
 118          db_query("REPLACE INTO {bench_chart} (hook, count, time, totaltime, path) VALUES ('%s', '%d', '%f', '%f', '%s')", $hook_name, $count, $calc_time, $totaltime, $path);
 119              if ($calc_time>2000) { // TODO: Move to the settings
 120                  bench_chart_run_hook($hook_name);
 121              }
 122      }
 123      }
 124      //$time_rest = ($totaltime - ($lasttime - $hooks_run['(before boot)']['start'][0]));
 125      //db_query("INSERT INTO {bench_chart} (hook, count, time, totaltime, path) VALUES ('%s', '%s', '%s', '%s', '%s')", '(other)', 1, $time_rest, $totaltime, $path);
 126  }
 127  
 128  /**
 129   * Implementation of each hook
 130   *
 131   */
 132  function bench_chart_my_hook() {
 133      $args = func_get_args(); // get function arguments
 134      $hook = $args[0]; // get first parameter
 135      list($hook, $type) = array($args[0][0], $args[0][1]); // explode our first function argument
 136      unset($args[0]); 
 137      global $hooks_run, $bench_chart_run;
 138   // prepare globals
 139      if ($type == 'start' && $bench_chart_run) { // if hook already run, don't start another one (recurency not supported)...
 140          return; // ...just do exit
 141      } else {
 142          $bench_chart_run = FALSE;
 143      }
 144      $timer = timer_read('bench_chart');
 145      $hooks_run[$hook][$type][] = (float)$timer;
 146      $hooks_run[$hook]['#args'][] = $args;
 147      switch ($hook) {
 148      case 'exit':
 149          $hooks_run['(exit)']['start'][] = (float)$timer;
 150      break;
 151      }
 152  }
 153  
 154  
 155  /**
 156   * Implementation of each hook
 157   *
 158   */
 159  function bench_chart_run_hook() {
 160      global $bench_chart_run; // prepare global variables
 161      $args = func_get_args(); // get function arguments
 162      $hook = $args[0]; // get and set first parameter
 163      unset($args[0]); // and remove it from argument list
 164      $hook_timer = array(); // reset hook timer
 165      $menu = menu_get_item(); // get alias to the current menu
 166      $path = !empty($menu['path']) ? $menu['path'] : $_GET['q']; // set alias, if doesn't exist, get path directly
 167      timer_start("hook_$hook"); // start timer
 168      foreach (module_implements($hook) as $module) {
 169          $function = $module .'_'. $hook;
 170          if (function_exists($function)) {
 171              timer_start("hook_$module}_$hook"); // start timer
 172              $result = call_user_func_array($function, $args);
 173              $hook_timer_stop = timer_stop("hook_$module}_$hook"); // end timer
 174              $hook_timer[$module] = $hook_timer_stop['time']; // calculate the time of executed hook
 175              $hook_total_time[$module] = timer_read("hook_$hook"); // end timer
 176          }
 177      }
 178      foreach($hook_timer as $module => $time) {
 179          db_query("REPLACE INTO {bench_chart} (hook, count, time, totaltime, path) VALUES ('%s', '%d', '%f', '%f', '%s')", "$module}_$hook", 0, $time, $hook_total_time[$module], $path);
 180      }
 181  }
 182  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7