- <?
- /**
- * @package profiler
- * @filesource
- * @see HTML_PROFILER_COMPONENT_PATH.'/Timer.php'
- * @copyright (c) http://Finn-Rasmussen.com
- * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
- * @author http://Finn-Rasmussen.com
- * @version 1.9
- * @since 21-oct-2005
- */
-
- /**
- * The required files
- */
- require_once(HTML_PATH.'/Object.php');
-
- if (!defined('DEFAULT_TIMER_NAME')) {
- define('DEFAULT_TIMER_NAME', 'timerName');
- }
- if (!defined('DEFAULT_TIMER_PROFILER')) {
- define('DEFAULT_TIMER_PROFILER', 'timerProfiler');
- }
- if (!defined('DEFAULT_TIMER_SCALE')) {
- define('DEFAULT_TIMER_SCALE' , 6); // The number of decimals
- }
-
- /**
- * Used as a Profiler to track down bottle necks
- * Note: This class will use a global profiler name
- * to hold one instance of a profiler object
- * <code>
- * Usage:
- * $timer = new Timer($name);
- * $timer->start($name);
- * print $timer->get($name);
- * $timer->reset($name);
- * Or
- * Timer::display($name); // as text
- * Timer::dump($name); // as html comment
- * </code>
- * @package profiler
- * @todo simulate bcsub(), if not installed on your system
- */
-
- class Timer extends Object {
- /**
- * @var array $timerStart An array of named start timers
- */
- var $timerStart = array();
-
- /**
- * @var array $timerStop An array of named stop timers
- */
- var $timerStop = array();
-
- /**
- * Constructor
- * @param String $name The name of the timer
- */
- function Timer($name='') {
- $this->start($name);
- }
-
- /**
- * Start the named timer
- * @param String $name The name of the timer
- * @return String The time formatted as <sec><msec>
- */
- function start($name='') {
- $index = $name!=''?$name:DEFAULT_TIMER_NAME;
- $this->timerStart[$index] = $this->getTime();
- return $this->timerStart[$index];
- }
-
- /**
- * Stop the named timer
- * @param String $name The name of the timer
- * @return String The time formatted as <sec><msec>
- */
- function stop($name='') {
- $index = $name!=''?$name:DEFAULT_TIMER_NAME;
- $this->timerStop[$index] = $this->getTime();
- return $this->timerStop[$index];
- }
-
- /**
- * Reset the named timer
- * @param String $name The name of the timer
- */
- function reset($name='') {
- $index = $name!=''?$name:DEFAULT_TIMER_NAME;
- $time = 0;
- $this->timerStart[$index] = $time;
- $this->timerStop [$index] = $time;
- }
-
- /**
- * Init profiler
- * <code>
- * Usage:
- * Timer::init($name);
- * </code>
- * @static
- * @param String $name The name of the timer
- */
- function init($name='') {
- if (empty($GLOBALS[DEFAULT_TIMER_PROFILER])) {
- $GLOBALS[DEFAULT_TIMER_PROFILER] = new Timer($name);
- } else {
- $GLOBALS[DEFAULT_TIMER_PROFILER]->start($name);
- }
- }
-
- /**
- * Dump html
- * <code>
- * Usage:
- * Timer::dump($text);
- * </code>
- * @static
- * @param String $text The plain text or as hidden html comments
- */
- function dump($text='') {
- $html = '';
- if (empty($GLOBALS[DEFAULT_TIMER_PROFILER])) {
- print "The Profiler was NOT started, use Timer::init()";
- } else {
- // Ok, Profiler is running
- }
- foreach ($GLOBALS[DEFAULT_TIMER_PROFILER]->timerStart as $key=>$value) {
- if ($text!='') {
- $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->get($key)."\r\n";
- } else {
- $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->getHtml($key);
- }
- }
- print $html;
- }
-
- /**
- * Get the current time, format and return it
- * @param String $name The name of the timer
- * @return String The time formatted as <sec><msec>
- */
- function getTime($name='') {
- $index = $name!=''?$name:DEFAULT_TIMER_NAME;
- $mt = explode(' ', microtime()); // format: '.<msec> <sec>'
- $time = $mt[1].substr($mt[0], 1);
- if (!empty($this->timerStop[$index]) && $this->timerStop[$index]!=0) {
- $time = $this->timerStop[$index]; // Timer has stopped
- }
- return $time;
- }
-
- /**
- * Simulates thec php-bcsub(), in case it is not installed on your system
- * @param String $left The left parameter
- * @param String $right The right parameter
- * @param String $scale The number of digits
- * @return String The result like php-bcsub($left,$right,$scale)
- */
- function bcsub($left,$right,$scale) {
- return round($left - $right, $scale);
- }
-
- /**
- * Get the time from started for the named timer
- * @param String $name The name of the timer
- * @return String The elapsed time formatted as '<sec><msec>'
- */
- function get($name='') {
- $index = $name!=''?$name:DEFAULT_TIMER_NAME;
- return $index.': '.$this->bcsub($this->getTime($index), $this->timerStart[$index], DEFAULT_TIMER_SCALE);
- }
-
- /**
- * Builds the html for a timer stamp
- * @param String $name The name of the timer
- * @param String $text The plain text or as hidden html comments
- * @return String The result as html
- */
- function getHtml($name='',$text='') {
- $html = '';
- $html .= '<!-- ';
- $html .= $this->get($name);
- if ($text!='') {
- $html .= ' : '.$text;
- }
- $html .= " -->\r\n";
- return $html;
- }
-
- /**
- * Display html
- * <code>
- * Usage:
- * Timer::display($name,$text);
- * </code>
- * @static
- * @param String $name The name of the timer
- * @param String $text The plain text or as hidden html comments
- */
- function display($name='',$text='') {
- $html = '';
- if (empty($GLOBALS[DEFAULT_TIMER_PROFILER])) {
- $GLOBALS[DEFAULT_TIMER_PROFILER] = new Timer($name);
- } else {
- // Ok, Profiler is running
- }
- //$GLOBALS[DEFAULT_TIMER_PROFILER]->stop($name); // Stop timer
- if ($name!='') {
- $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->get($name)."\r\n";
- } else {
- $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->getHtml($name,$text);
- }
- $GLOBALS[DEFAULT_TIMER_PROFILER]->addHtml($html);
- }
- }
- ?>