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