b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _timer.class.php

Documentation is available at _timer.class.php

  1. <?php
  2. /**
  3.  * This file implements the Timer class.
  4.  *
  5.  * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.
  6.  * See also {@link http://sourceforge.net/projects/evocms/}.
  7.  *
  8.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}.
  9.  *  Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10.  *
  11.  * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)
  12.  *  {@internal 
  13.  *  b2evolution is free software; you can redistribute it and/or modify
  14.  *  it under the terms of the GNU General Public License as published by
  15.  *  the Free Software Foundation; either version 2 of the License, or
  16.  *  (at your option) any later version.
  17.  *
  18.  *  b2evolution is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License
  24.  *  along with b2evolution; if not, write to the Free Software
  25.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  26.  *  }}}
  27.  *
  28.  *  {@internal 
  29.  *  Daniel HAHLER grants François PLANQUE the right to license
  30.  *  Daniel HAHLER's contributions to this file and the b2evolution project
  31.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  32.  *  }}}
  33.  *
  34.  * @package evocore
  35.  *
  36.  *  {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  37.  * @author blueyed: Daniel HAHLER.
  38.  *
  39.  * @version $Id: _timer.class.php,v 1.1.2.1 2005/09/02 21:31:34 fplanque Exp $
  40.  */
  41. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  42.  
  43. /**
  44.  * This is a simple class to allow timing/profiling of code portions.
  45.  */
  46. class Timer
  47. {
  48.     /**
  49.      * Remember times.
  50.      *
  51.      * We store for each category (primary key) the state, start/resume time and the total passed time.
  52.      *
  53.      * @access protected
  54.      */
  55.     var $_times = array();
  56.  
  57.  
  58.     /**
  59.      * Constructor.
  60.      *
  61.      * @param string|NULLIf a category is given the timer starts right away.
  62.      */
  63.     function Timer$category NULL )
  64.     {
  65.         ifis_string($category) )
  66.         {
  67.             $this->start$category );
  68.         }
  69.     }
  70.  
  71.  
  72.     /**
  73.      * Reset a timer category.
  74.      */
  75.     function reset$category )
  76.     {
  77.         $this->_times[$categoryarray'total' => );
  78.     }
  79.  
  80.  
  81.     /**
  82.      * Start a timer.
  83.      */
  84.     function start$category )
  85.     {
  86.         debug_log'Starting timer '.$category'timer' );
  87.         $this->reset$category );
  88.         $this->resume$category );
  89.     }
  90.  
  91.  
  92.     /**
  93.      * Stops a timer category. It may me resumed later on, see {@link resume()}. This is an alias for {@link pause()}.
  94.      *
  95.      * @return boolean false, if the timer had not been started.
  96.      */
  97.     function stop$category )
  98.     {
  99.         return $this->pause$category );
  100.     }
  101.  
  102.  
  103.     /**
  104.      * Pauses a timer category. It may me resumed later on, see {@link resume()}.
  105.      *
  106.      * NOTE: The timer needs to be started, either through the {@link Timer() Constructor} or the {@link start()} method.
  107.      *
  108.      * @return boolean false, if the timer had not been started.
  109.      */
  110.     function pause$category )
  111.     {
  112.         if!isset($this->_times[$category]['resumed']) )
  113.         // Timer has not been started!
  114.             return false;
  115.         }
  116.         $since_pause $this->get_current_microtime($this->_times[$category]['resumed'];
  117.         $this->_times[$category]['total'+= $since_pause;
  118.  
  119.         $this->_times[$category]['state''paused';
  120.  
  121.         return true;
  122.     }
  123.  
  124.  
  125.     /**
  126.      * Resumes the timer on a category.
  127.      */
  128.     function resume$category )
  129.     {
  130.         if!isset($this->_times[$category]['total']) )
  131.         {
  132.             $this->start($category);
  133.         }
  134.         $this->_times[$category]['resumed'$this->get_current_microtime();
  135.  
  136.         $this->_times[$category]['state''running';
  137.     }
  138.  
  139.  
  140.     /**
  141.      *
  142.      *
  143.      * @return 
  144.      */
  145.     function display_time$category$decimals )
  146.     {
  147.         echo $this->get_duration$category$decimals );
  148.     }
  149.  
  150.  
  151.     /**
  152.      *
  153.      *
  154.      * @return string 
  155.      */
  156.     function get_duration$category$decimals )
  157.     {
  158.         return number_format$this->get_microtime($category)$decimals )// TODO: decimals/seperator by locale!
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Get the time in microseconds that was spent in the given category.
  164.      *
  165.      * @return float 
  166.      */
  167.     function get_microtime$category )
  168.     {
  169.         switch$this->get_state($category) )
  170.         {
  171.             case 'running':
  172.                 // The timer is running, we need to return the additional time since the last resume.
  173.                 return $this->_times[$category]['total']
  174.                     + $this->get_current_microtime($this->_times[$category]['resumed'];
  175.  
  176.             case 'paused':
  177.                 return $this->_times[$category]['total'];
  178.  
  179.             default:
  180.                 return (float)0;
  181.         }
  182.     }
  183.  
  184.  
  185.     /**
  186.      * Get the state a category timer is in.
  187.      *
  188.      * @return string 'unknown', 'not initialised', 'running', 'paused'
  189.      */
  190.     function get_state$category )
  191.     {
  192.         if!isset($this->_times[$category]) )
  193.         {
  194.             return 'unknown';
  195.         }
  196.  
  197.         if!isset($this->_times[$category]['state']) )
  198.         {
  199.             return 'not initialised';
  200.         }
  201.  
  202.         return $this->_times[$category]['state'];
  203.     }
  204.  
  205.  
  206.     /**
  207.      * Get the current time in microseconds.
  208.      *
  209.      * @return float 
  210.      */
  211.     function get_current_microtime()
  212.     {
  213.         list($usec$secexplode(' 'microtime());
  214.         return ((float)$usec + (float)$sec);
  215.     }
  216. }
  217.  
  218. /*
  219.  * $Log: _timer.class.php,v $
  220.  * Revision 1.1.2.1  2005/09/02 21:31:34  fplanque
  221.  * enhanced query debugging features
  222.  *
  223.  * Revision 1.1  2005/07/12 23:05:36  blueyed
  224.  * Added Timer class with categories 'main' and 'sql_queries' for now.
  225.  *
  226.  */
  227. ?>

Documentation generated on Tue, 20 May 2008 01:55:51 +0200 by phpDocumentor 1.4.2