PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/error/profiler.php

https://github.com/joebushi/joomla
PHP | 197 lines | 93 code | 17 blank | 87 comment | 7 complexity | 13ac061ed203e5584aec6c202cafe1bb MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Error
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Utility class to assist in the process of benchmarking the execution
  13. * of sections of code to understand where time is being spent.
  14. *
  15. * @package Joomla.Framework
  16. * @subpackage Error
  17. * @since 1.0
  18. */
  19. class JProfiler extends JObject
  20. {
  21. /**
  22. * The start time.
  23. *
  24. * @var int
  25. */
  26. protected $_start = 0;
  27. /**
  28. * The prefix to use in the output
  29. *
  30. * @var string
  31. */
  32. protected $_prefix = '';
  33. /**
  34. * The buffer of profiling messages.
  35. *
  36. * @var array
  37. */
  38. protected $_buffer= null;
  39. /**
  40. * @var float
  41. * @since 1.6
  42. */
  43. protected $_previous_time = 0.0;
  44. /**
  45. * @var float
  46. * @since 1.6
  47. */
  48. protected $_previous_mem = 0.0;
  49. /**
  50. * Boolean if the OS is Windows.
  51. *
  52. * @var boolean
  53. * @since 1.6
  54. */
  55. protected $_iswin = false;
  56. /**
  57. * Constructor
  58. *
  59. * @param string Prefix for mark messages
  60. */
  61. public function __construct($prefix = '')
  62. {
  63. $this->_start = $this->getmicrotime();
  64. $this->_prefix = $prefix;
  65. $this->_buffer = array();
  66. $this->_iswin = (substr(PHP_OS, 0, 3) == 'WIN');
  67. }
  68. /**
  69. * Returns the global Profiler object, only creating it
  70. * if it doesn't already exist.
  71. *
  72. * @param string Prefix used to distinguish profiler objects.
  73. * @return JProfiler The Profiler object.
  74. */
  75. public static function getInstance($prefix = '')
  76. {
  77. static $instances;
  78. if (!isset($instances)) {
  79. $instances = array();
  80. }
  81. if (empty($instances[$prefix])) {
  82. $instances[$prefix] = new JProfiler($prefix);
  83. }
  84. return $instances[$prefix];
  85. }
  86. /**
  87. * Output a time mark
  88. *
  89. * The mark is returned as text enclosed in <div> tags
  90. * with a CSS class of 'profiler'.
  91. *
  92. * @param string A label for the time mark
  93. * @return string Mark enclosed in <div> tags
  94. */
  95. public function mark($label)
  96. {
  97. $current = self::getmicrotime() - $this->_start;
  98. if (function_exists('memory_get_usage'))
  99. {
  100. $current_mem = memory_get_usage() / 1048576;
  101. $mark = sprintf(
  102. '<code>%s %.3f seconds (+%.3f); %0.2f Mb (+%0.2f) - %s</code>',
  103. $this->_prefix,
  104. $current,
  105. $current - $this->_previous_time,
  106. $current_mem,
  107. $current_mem - $this->_previous_mem,
  108. $label
  109. );
  110. }
  111. else
  112. {
  113. $mark = sprintf(
  114. '<code>%s %.3f seconds (+%.3f) - %s</code>',
  115. $this->_prefix,
  116. $current,
  117. $current - $this->_previous_time,
  118. $label
  119. );
  120. }
  121. $this->_previous_time = $current;
  122. $this->_previous_mem = $current_mem;
  123. $this->_buffer[] = $mark;
  124. return $mark;
  125. }
  126. /**
  127. * Get the current time.
  128. *
  129. * @return float The current time
  130. */
  131. public static function getmicrotime()
  132. {
  133. list($usec, $sec) = explode(' ', microtime());
  134. return ((float)$usec + (float)$sec);
  135. }
  136. /**
  137. * Get information about current memory usage.
  138. *
  139. * @return int The memory usage
  140. * @link PHP_MANUAL#memory_get_usage
  141. */
  142. public function getMemory()
  143. {
  144. if (function_exists('memory_get_usage')) {
  145. return memory_get_usage();
  146. }
  147. else
  148. {
  149. // Initialise variables.
  150. $output = array();
  151. $pid = getmypid();
  152. if ($this->_iswin)
  153. {
  154. // Windows workaround
  155. @exec('tasklist /FI "PID eq ' . $pid . '" /FO LIST', $output);
  156. if (!isset($output[5])) {
  157. $output[5] = null;
  158. }
  159. return substr($output[5], strpos($output[5], ':') + 1);
  160. }
  161. else
  162. {
  163. @exec("ps -o rss -p $pid", $output);
  164. return $output[1] *1024;
  165. }
  166. }
  167. }
  168. /**
  169. * Get all profiler marks.
  170. *
  171. * Returns an array of all marks created since the Profiler object
  172. * was instantiated. Marks are strings as per {@link JProfiler::mark()}.
  173. *
  174. * @return array Array of profiler marks
  175. */
  176. public function getBuffer()
  177. {
  178. return $this->_buffer;
  179. }
  180. }