phpDocumentor profiler
[ class tree: profiler ] [ index: profiler ] [ all elements ]

Source for file Timer.php

Documentation is available at Timer.php

  1. <?
  2. /**
  3. * @package profiler
  4. * @filesource
  5. * @see HTML_PROFILER_COMPONENT_PATH.'/Timer.php'
  6. * @copyright (c) http://Finn-Rasmussen.com
  7. * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
  8. * @author http://Finn-Rasmussen.com
  9. * @version 1.9
  10. * @since 21-oct-2005
  11. */
  12.  
  13. /**
  14. * The required files
  15. */
  16. require_once(HTML_PATH.'/Object.php');
  17.  
  18. if (!defined('DEFAULT_TIMER_NAME')) {
  19. define('DEFAULT_TIMER_NAME', 'timerName');
  20. }
  21. if (!defined('DEFAULT_TIMER_PROFILER')) {
  22. define('DEFAULT_TIMER_PROFILER', 'timerProfiler');
  23. }
  24. if (!defined('DEFAULT_TIMER_SCALE')) {
  25. define('DEFAULT_TIMER_SCALE' , 6); // The number of decimals
  26. }
  27.  
  28. /**
  29. * Used as a Profiler to track down bottle necks
  30. * Note: This class will use a global profiler name
  31. * to hold one instance of a profiler object
  32. * <code>
  33. * Usage:
  34. * $timer = new Timer($name);
  35. * $timer->start($name);
  36. * print $timer->get($name);
  37. * $timer->reset($name);
  38. * Or
  39. * Timer::display($name); // as text
  40. * Timer::dump($name); // as html comment
  41. * </code>
  42. * @package profiler
  43. * @todo simulate bcsub(), if not installed on your system
  44. */
  45.  
  46. class Timer extends Object {
  47. /**
  48. * @var array $timerStart An array of named start timers
  49. */
  50. var $timerStart = array();
  51. /**
  52. * @var array $timerStop An array of named stop timers
  53. */
  54. var $timerStop = array();
  55.  
  56. /**
  57. * Constructor
  58. * @param String $name The name of the timer
  59. */
  60. function Timer($name='') {
  61. $this->start($name);
  62. }
  63.  
  64. /**
  65. * Start the named timer
  66. * @param String $name The name of the timer
  67. * @return String The time formatted as <sec><msec>
  68. */
  69. function start($name='') {
  70. $index = $name!=''?$name:DEFAULT_TIMER_NAME;
  71. $this->timerStart[$index] = $this->getTime();
  72. return $this->timerStart[$index];
  73. }
  74.  
  75. /**
  76. * Stop the named timer
  77. * @param String $name The name of the timer
  78. * @return String The time formatted as <sec><msec>
  79. */
  80. function stop($name='') {
  81. $index = $name!=''?$name:DEFAULT_TIMER_NAME;
  82. $this->timerStop[$index] = $this->getTime();
  83. return $this->timerStop[$index];
  84. }
  85.  
  86. /**
  87. * Reset the named timer
  88. * @param String $name The name of the timer
  89. */
  90. function reset($name='') {
  91. $index = $name!=''?$name:DEFAULT_TIMER_NAME;
  92. $time = 0;
  93. $this->timerStart[$index] = $time;
  94. $this->timerStop [$index] = $time;
  95. }
  96.  
  97. /**
  98. * Init profiler
  99. * <code>
  100. * Usage:
  101. * Timer::init($name);
  102. * </code>
  103. * @static
  104. * @param String $name The name of the timer
  105. */
  106. function init($name='') {
  107. if (empty($GLOBALS[DEFAULT_TIMER_PROFILER])) {
  108. $GLOBALS[DEFAULT_TIMER_PROFILER] = new Timer($name);
  109. } else {
  110. $GLOBALS[DEFAULT_TIMER_PROFILER]->start($name);
  111. }
  112. }
  113.  
  114. /**
  115. * Dump html
  116. * <code>
  117. * Usage:
  118. * Timer::dump($text);
  119. * </code>
  120. * @static
  121. * @param String $text The plain text or as hidden html comments
  122. */
  123. function dump($text='') {
  124. $html = '';
  125. if (empty($GLOBALS[DEFAULT_TIMER_PROFILER])) {
  126. print "The Profiler was NOT started, use Timer::init()";
  127. } else {
  128. // Ok, Profiler is running
  129. }
  130. foreach ($GLOBALS[DEFAULT_TIMER_PROFILER]->timerStart as $key=>$value) {
  131. if ($text!='') {
  132. $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->get($key)."\r\n";
  133. } else {
  134. $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->getHtml($key);
  135. }
  136. }
  137. print $html;
  138. }
  139.  
  140. /**
  141. * Get the current time, format and return it
  142. * @param String $name The name of the timer
  143. * @return String The time formatted as <sec><msec>
  144. */
  145. function getTime($name='') {
  146. $index = $name!=''?$name:DEFAULT_TIMER_NAME;
  147. $mt = explode(' ', microtime()); // format: '.<msec> <sec>'
  148. $time = $mt[1].substr($mt[0], 1);
  149. if (!empty($this->timerStop[$index]) && $this->timerStop[$index]!=0) {
  150. $time = $this->timerStop[$index]; // Timer has stopped
  151. }
  152. return $time;
  153. }
  154.  
  155. /**
  156. * Simulates thec php-bcsub(), in case it is not installed on your system
  157. * @param String $left The left parameter
  158. * @param String $right The right parameter
  159. * @param String $scale The number of digits
  160. * @return String The result like php-bcsub($left,$right,$scale)
  161. */
  162. function bcsub($left,$right,$scale) {
  163. return round($left - $right, $scale);
  164. }
  165.  
  166. /**
  167. * Get the time from started for the named timer
  168. * @param String $name The name of the timer
  169. * @return String The elapsed time formatted as '<sec><msec>'
  170. */
  171. function get($name='') {
  172. $index = $name!=''?$name:DEFAULT_TIMER_NAME;
  173. return $index.': '.$this->bcsub($this->getTime($index), $this->timerStart[$index], DEFAULT_TIMER_SCALE);
  174. }
  175.  
  176. /**
  177. * Builds the html for a timer stamp
  178. * @param String $name The name of the timer
  179. * @param String $text The plain text or as hidden html comments
  180. * @return String The result as html
  181. */
  182. function getHtml($name='',$text='') {
  183. $html = '';
  184. $html .= '<!-- ';
  185. $html .= $this->get($name);
  186. if ($text!='') {
  187. $html .= ' : '.$text;
  188. }
  189. $html .= " -->\r\n";
  190. return $html;
  191. }
  192.  
  193. /**
  194. * Display html
  195. * <code>
  196. * Usage:
  197. * Timer::display($name,$text);
  198. * </code>
  199. * @static
  200. * @param String $name The name of the timer
  201. * @param String $text The plain text or as hidden html comments
  202. */
  203. function display($name='',$text='') {
  204. $html = '';
  205. if (empty($GLOBALS[DEFAULT_TIMER_PROFILER])) {
  206. $GLOBALS[DEFAULT_TIMER_PROFILER] = new Timer($name);
  207. } else {
  208. // Ok, Profiler is running
  209. }
  210. //$GLOBALS[DEFAULT_TIMER_PROFILER]->stop($name); // Stop timer
  211. if ($name!='') {
  212. $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->get($name)."\r\n";
  213. } else {
  214. $html .= $GLOBALS[DEFAULT_TIMER_PROFILER]->getHtml($name,$text);
  215. }
  216. $GLOBALS[DEFAULT_TIMER_PROFILER]->addHtml($html);
  217. }
  218. }
  219. ?>

Documentation generated on Thu, 22 Dec 2005 17:20:47 +0100 by phpDocumentor 1.3.0RC3