b2evolution PHP Cross Reference Blogging Systems

Source: /inc/_core/model/_timer.class.php - 276 lines - 6773 bytes - Summary - Text - Print

Description: This file implements the Timer class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the Timer class.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10   *
  11   * {@internal License choice
  12   * - If you have received this file as part of a package, please find the license.txt file in
  13   *   the same folder or the closest folder above for complete license terms.
  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15   *   then you must choose one of the following licenses before using the file:
  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18   * }}
  19   *
  20   * {@internal Open Source relicensing agreement:
  21   * Daniel HAHLER grants Francois PLANQUE the right to license
  22   * Daniel HAHLER's contributions to this file and the b2evolution project
  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24   * }}
  25   *
  26   * @package evocore
  27   *
  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  29   * @author fplanque: Francois PLANQUE.
  30   * @author blueyed: Daniel HAHLER.
  31   *
  32   * @version $Id: _timer.class.php 6136 2014-03-08 07:59:48Z manuel $
  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  
  37  // DEBUG: (Turn switch on or off to log debug info for specified category)
  38  $GLOBALS['debug_timer'] = true;
  39  
  40  
  41  /**
  42   * This is a simple class to allow timing/profiling of code portions.
  43   */
  44  class Timer
  45  {
  46      /**
  47       * Remember times.
  48       *
  49       * We store for each category (primary key) the state, start/resume time and the total passed time.
  50       *
  51       * @access protected
  52       */
  53      var $_times = array();
  54  
  55  
  56      /**
  57       * @access protected
  58       * @var integer Level of internal indentation, used to indent Debuglog messages.
  59       */
  60      var $indent = 0;
  61  
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @param string|NULL If a category is given the timer starts right away.
  67       */
  68  	function Timer( $category = NULL )
  69      {
  70          if( is_string($category) )
  71          {
  72              $this->start( $category );
  73          }
  74      }
  75  
  76  
  77      /**
  78       * Reset a timer category.
  79       */
  80  	function reset( $category )
  81      {
  82          $this->_times[$category] = array( 'total' => 0, 'count' => 0 );
  83      }
  84  
  85  
  86      /**
  87       * Start a timer.
  88       */
  89  	function start( $category, $log = true )
  90      {
  91          $this->reset( $category );
  92          $this->resume( $category, $log );
  93      }
  94  
  95  
  96      /**
  97       * Stops a timer category. It may me resumed later on, see {@link resume()}. This is an alias for {@link pause()}.
  98       *
  99       * @return boolean false, if the timer had not been started.
 100       */
 101  	function stop( $category )
 102      {
 103          global $Debuglog;
 104  
 105          if( ! $this->pause( $category ) )
 106              return false;
 107  
 108          $Debuglog->add( str_repeat('&nbsp;', $this->indent*4).$category.' stopped at '.$this->get_duration( $category, 3 ), 'timer' );
 109  
 110          return true;
 111      }
 112  
 113  
 114      /**
 115       * Pauses a timer category. It may me resumed later on, see {@link resume()}.
 116       *
 117       * NOTE: The timer needs to be started, either through the {@link Timer() Constructor} or the {@link start()} method.
 118       *
 119       * @return boolean false, if the timer had not been started.
 120       */
 121  	function pause( $category, $log = true )
 122      {
 123          global $Debuglog;
 124          $since_resume = $this->get_current_microtime() - $this->_times[$category]['resumed'];
 125          if( $log )
 126          {
 127              $this->indent--;
 128              if( $this->indent < 0 ) $this->indent = 0;
 129              $Debuglog->add( str_repeat('&nbsp;', $this->indent*4).$category.' paused at '.$this->get_duration( $category, 3 ).' (<strong>+'.number_format($since_resume, 4).'</strong>)', 'timer' );
 130          }
 131          if( $this->get_state($category) != 'running' )
 132          { // Timer is not running!
 133              $Debuglog->add("Warning: tried to pause already paused '$category'.", 'timer');
 134              return false;
 135          }
 136  
 137          $this->_times[$category]['total'] += $since_resume;
 138          $this->_times[$category]['state'] = 'paused';
 139  
 140          return true;
 141      }
 142  
 143  
 144      /**
 145       * Resumes the timer on a category.
 146       */
 147  	function resume( $category, $log = true )
 148      {
 149          global $Debuglog;
 150  
 151          if( !isset($this->_times[$category]['total']) )
 152          {
 153              $this->start( $category, $log );
 154              return;
 155          }
 156  
 157          $this->_times[$category]['resumed'] = $this->get_current_microtime();
 158          $this->_times[$category]['count']++;
 159  
 160          $this->_times[$category]['state'] = 'running';
 161  
 162          if( $log )
 163          {
 164              $Debuglog->add( str_repeat('&nbsp;', $this->indent*4).$category.' resumed at '.$this->get_duration( $category, 3 ), 'timer' );
 165              $this->indent++;
 166          }
 167      }
 168  
 169  
 170      /**
 171       * Get the duration for a given category in seconds,microseconds (configurable number of decimals)
 172       *
 173       * @param string Category name
 174       * @param integer Number of decimals after dot.
 175       * @return string
 176       */
 177  	function get_duration( $category, $decimals = 3 )
 178      {
 179          return number_format( $this->get_microtime($category), $decimals ); // TODO: decimals/seperator by locale!
 180      }
 181  
 182  
 183      /**
 184       * Log a duration with Application Performance Monitor
 185       *
 186       * @param mixed $category
 187       */
 188  	function log_duration( $category )
 189      {
 190          apm_log_custom_metric( $category, $this->get_microtime($category) * 1000 );
 191      }
 192  
 193  
 194      /**
 195       * Get number of timer resumes (includes start).
 196       *
 197       * @return integer
 198       */
 199  	function get_count( $category )
 200      {
 201          if( isset( $this->_times[$category] ) )
 202          {
 203              return $this->_times[$category]['count'];
 204          }
 205  
 206          return false;
 207      }
 208  
 209  
 210      /**
 211       * Get the time that was spent in the given category in seconds with microsecond precision
 212       *
 213       * @return float (seconds.microseconds)
 214       */
 215  	function get_microtime( $category )
 216      {
 217          switch( $this->get_state($category) )
 218          {
 219              case 'running':
 220                  // The timer is running, we need to return the additional time since the last resume.
 221                  return $this->_times[$category]['total']
 222                      + $this->get_current_microtime() - $this->_times[$category]['resumed'];
 223  
 224              case 'paused':
 225                  return $this->_times[$category]['total'];
 226  
 227              default:
 228                  return (float)0;
 229          }
 230      }
 231  
 232  
 233      /**
 234       * Get the state a category timer is in.
 235       *
 236       * @return string 'unknown', 'not initialised', 'running', 'paused'
 237       */
 238  	function get_state( $category )
 239      {
 240          if( !isset($this->_times[$category]) )
 241          {
 242              return 'unknown';
 243          }
 244  
 245          if( !isset($this->_times[$category]['state']) )
 246          {
 247              return 'not initialised';
 248          }
 249  
 250          return $this->_times[$category]['state'];
 251      }
 252  
 253  
 254      /**
 255       * Get a list of used categories.
 256       *
 257       * @return array
 258       */
 259  	function get_categories()
 260      {
 261          return array_keys( $this->_times );
 262      }
 263  
 264  
 265      /**
 266       * Get the current time with microsecond precision
 267       *
 268       * @return float (seconds.microseconds)
 269       */
 270  	function get_current_microtime()
 271      {
 272          list($usec, $sec) = explode(' ', microtime());
 273          return ((float)$usec + (float)$sec);
 274      }
 275  }
 276  ?>

title

Description

title

Description

title

Description

title

title

Body