[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/inc/_core/model/ -> _timer.class.php (source)

   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-2007 by Francois PLANQUE - {@link http://fplanque.net/}

   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,v 1.1 2007/06/25 10:58:55 fplanque Exp $

  33   */
  34  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  35  
  36  /**

  37   * This is a simple class to allow timing/profiling of code portions.

  38   */
  39  class Timer
  40  {
  41      /**

  42       * Remember times.

  43       *

  44       * We store for each category (primary key) the state, start/resume time and the total passed time.

  45       *

  46       * @access protected

  47       */
  48      var $_times = array();
  49  
  50  
  51      /**

  52       * Constructor.

  53       *

  54       * @param string|NULL If a category is given the timer starts right away.

  55       */
  56  	function Timer( $category = NULL )
  57      {
  58          if( is_string($category) )
  59          {
  60              $this->start( $category );
  61          }
  62      }
  63  
  64  
  65      /**

  66       * Reset a timer category.

  67       */
  68  	function reset( $category )
  69      {
  70          $this->_times[$category] = array( 'total' => 0, 'count' => 0 );
  71      }
  72  
  73  
  74      /**

  75       * Start a timer.

  76       */
  77  	function start( $category, $log = true )
  78      {
  79          global $Debuglog;
  80          if( $log && is_object( $Debuglog ) ) $Debuglog->add( 'Starting timer '.$category, 'timer' );
  81          $this->reset( $category );
  82          $this->resume( $category );
  83      }
  84  
  85  
  86      /**

  87       * Stops a timer category. It may me resumed later on, see {@link resume()}. This is an alias for {@link pause()}.

  88       *

  89       * @return boolean false, if the timer had not been started.

  90       */
  91  	function stop( $category )
  92      {
  93          global $Debuglog;
  94  
  95          if( ! $this->pause( $category ) )
  96              return false;
  97  
  98          if( is_object( $Debuglog ) )
  99          {
 100              $Debuglog->add( 'Stopped timer '.$category.' at '.$this->get_duration( $category, 3 ), 'timer' );
 101          }
 102  
 103          return true;
 104      }
 105  
 106  
 107      /**

 108       * Pauses a timer category. It may me resumed later on, see {@link resume()}.

 109       *

 110       * NOTE: The timer needs to be started, either through the {@link Timer() Constructor} or the {@link start()} method.

 111       *

 112       * @return boolean false, if the timer had not been started.

 113       */
 114  	function pause( $category )
 115      {
 116          if( !isset($this->_times[$category]['resumed']) )
 117          { // Timer has not been started!
 118              return false;
 119          }
 120          $since_resume = $this->get_current_microtime() - $this->_times[$category]['resumed'];
 121          $this->_times[$category]['total'] += $since_resume;
 122          $this->_times[$category]['state'] = 'paused';
 123  
 124          return true;
 125      }
 126  
 127  
 128      /**

 129       * Resumes the timer on a category.

 130       */
 131  	function resume( $category )
 132      {
 133          if( !isset($this->_times[$category]['total']) )
 134          {
 135              $this->start($category);
 136          }
 137          else
 138          {
 139              $this->_times[$category]['resumed'] = $this->get_current_microtime();
 140              $this->_times[$category]['count']++;
 141  
 142              $this->_times[$category]['state'] = 'running';
 143          }
 144      }
 145  
 146  
 147      /**

 148       * Get the duration for a given category.

 149       *

 150       * @param string Category name

 151       * @param integer Number of decimals after dot.

 152       * @return string

 153       */
 154  	function get_duration( $category, $decimals = 3 )
 155      {
 156          return number_format( $this->get_microtime($category), $decimals ); // TODO: decimals/seperator by locale!

 157      }
 158  
 159  
 160      /**

 161       * Get number of timer resumes (includes start).

 162       *

 163       * @return integer

 164       */
 165  	function get_count( $category )
 166      {
 167          if( isset( $this->_times[$category] ) )
 168          {
 169              return $this->_times[$category]['count'];
 170          }
 171  
 172          return false;
 173      }
 174  
 175  
 176      /**

 177       * Get the time in microseconds that was spent in the given category.

 178       *

 179       * @return float

 180       */
 181  	function get_microtime( $category )
 182      {
 183          switch( $this->get_state($category) )
 184          {
 185              case 'running':
 186                  // The timer is running, we need to return the additional time since the last resume.

 187                  return $this->_times[$category]['total']
 188                      + $this->get_current_microtime() - $this->_times[$category]['resumed'];
 189  
 190              case 'paused':
 191                  return $this->_times[$category]['total'];
 192  
 193              default:
 194                  return (float)0;
 195          }
 196      }
 197  
 198  
 199      /**

 200       * Get the state a category timer is in.

 201       *

 202       * @return string 'unknown', 'not initialised', 'running', 'paused'

 203       */
 204  	function get_state( $category )
 205      {
 206          if( !isset($this->_times[$category]) )
 207          {
 208              return 'unknown';
 209          }
 210  
 211          if( !isset($this->_times[$category]['state']) )
 212          {
 213              return 'not initialised';
 214          }
 215  
 216          return $this->_times[$category]['state'];
 217      }
 218  
 219  
 220      /**

 221       * Get a list of used categories.

 222       *

 223       * @return array

 224       */
 225  	function get_categories()
 226      {
 227          return array_keys( $this->_times );
 228      }
 229  
 230  
 231      /**

 232       * Get the current time in microseconds.

 233       *

 234       * @return float

 235       */
 236  	function get_current_microtime()
 237      {
 238          list($usec, $sec) = explode(' ', microtime());
 239          return ((float)$usec + (float)$sec);
 240      }
 241  }
 242  
 243  /*

 244   * $Log: _timer.class.php,v $

 245   * Revision 1.1  2007/06/25 10:58:55  fplanque

 246   * MODULES (refactored MVC)

 247   *

 248   * Revision 1.6  2007/04/26 00:11:08  fplanque

 249   * (c) 2007

 250   *

 251   * Revision 1.5  2006/11/24 18:27:27  blueyed

 252   * Fixed link to b2evo CVS browsing interface in file docblocks

 253   */
 254  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics