PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/TimeRanger/Profiler.php

https://github.com/Shumkov/TimeRanger
PHP | 251 lines | 181 code | 43 blank | 27 comment | 34 complexity | b3fd713e1150ce9633ffa5ddc68c5b46 MD5 | raw file
  1. <?php
  2. class TimeRanger_Profiler
  3. {
  4. protected static $_enabled = false;
  5. protected static $_samplingMode = false;
  6. protected static $_startTime;
  7. protected static $_storage;
  8. protected static $_instance;
  9. protected static $_protectConstruct = true;
  10. protected static $_options = array(
  11. 'applicationName' => null,
  12. 'requestName' => null,
  13. 'cpu' => true,
  14. 'memory' => true,
  15. 'bulitins' => false,
  16. 'ignoreFunctions' => array('call_user_func', 'call_user_func_array'),
  17. 'storage' => 'db',
  18. 'storageOptions' => array(),
  19. );
  20. public static function start($probability = false)
  21. {
  22. if (self::$_enabled) {
  23. require_once 'TimeRanger/Exception.php';
  24. throw new TimeRanger_Exception('Profiler already started');
  25. }
  26. if ($probability !== false && !is_integer($probability) && $probability <= 0) {
  27. require_once 'TimeRanger/Exception.php';
  28. throw new TimeRanger_Exception('Probability must be a positive integer or false');
  29. }
  30. if (!self::$_instance) {
  31. if (!extension_loaded('xhprof')) {
  32. require_once 'TimeRanger/Exception.php';
  33. throw new TimeRanger_Exception("The xhprof extension is required: http://pecl.php.net/package/xhprof");
  34. }
  35. self::$_protectConstruct = false;
  36. self::$_instance = new self;
  37. self::$_protectConstruct = true;
  38. }
  39. if (!$probability || mt_rand(1, $probability) == 1) {
  40. $flags = 0;
  41. if (self::$_options['bulitins']) {
  42. $flags += XHPROF_FLAGS_NO_BUILTINS;
  43. }
  44. if (self::$_options['cpu']) {
  45. $flags += XHPROF_FLAGS_NO_BUILTINS;
  46. }
  47. if (self::$_options['memory']) {
  48. $flags += XHPROF_FLAGS_MEMORY;
  49. }
  50. xhprof_enable($flags, array('ignored_functions' => self::$_options['ignoreFunctions']));
  51. self::$_startTime = microtime(true);
  52. self::$_enabled = true;
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. public static function stop()
  59. {
  60. if (!self::$_enabled) {
  61. require_once 'Geometria/Profile/Exception.php';
  62. throw new TimeRanger_Exception('Profiler not started');
  63. }
  64. if (is_null(self::$_options['applicationName'])) {
  65. $applicationName = $_SERVER['SERVER_NAME'];
  66. } else {
  67. $applicationName = self::$_options['applicationName'];
  68. }
  69. if (is_null(self::$_options['requestName'])) {
  70. $requestName = $_SERVER['REQUEST_URI'];
  71. } else {
  72. $requestName = self::$_options['requestName'];
  73. }
  74. $data = array(
  75. 'profile' => xhprof_disable(),
  76. 'get' => $_GET,
  77. 'post' => $_POST,
  78. );
  79. $elapsedTime = microtime(true) - self::$_startTime;
  80. return self::getStorage()->saveRequest(
  81. self::getApplicationName(),
  82. self::getRequestName(),
  83. $data,
  84. $elapsedTime
  85. );
  86. }
  87. public static function setApplicationName($name)
  88. {
  89. self::$_options['applicationName'] = $name;
  90. return true;
  91. }
  92. public static function getApplicationName()
  93. {
  94. if (is_null(self::$_options['applicationName'])) {
  95. return $_SERVER['SERVER_NAME'];
  96. } else {
  97. return self::$_options['applicationName'];
  98. }
  99. }
  100. public static function setRequestName($name)
  101. {
  102. self::$_options['requestName'] = $name;
  103. return true;
  104. }
  105. public static function getRequestName()
  106. {
  107. if (is_null(self::$_options['requestName'])) {
  108. return $_SERVER['REQUEST_URI'];
  109. } else {
  110. return self::$_options['requestName'];
  111. }
  112. }
  113. public static function setSamplingMode($flag = true)
  114. {
  115. if (!self::$_samplingMode && $flag) {
  116. xhprof_sample_enable();
  117. return true;
  118. } else if (self::$_samplingMode && !$flag) {
  119. xhprof_sample_disable();
  120. return true;
  121. }
  122. return false;
  123. }
  124. public static function getSamplingMode()
  125. {
  126. return self::$_samplingMode;
  127. }
  128. public static function setStorage($storage)
  129. {
  130. self::$_options['storage'] = $storage;
  131. return true;
  132. }
  133. public static function getStorage()
  134. {
  135. if (!self::$_storage) {
  136. require_once 'TimeRanger/Storage/Abstract.php';
  137. self::$_storage = TimeRanger_Storage_Abstract::factory(
  138. self::$_options['storage'],
  139. self::$_options['storageOptions']
  140. );
  141. }
  142. return self::$_storage;
  143. }
  144. /**
  145. * Set options array
  146. *
  147. * @param array $options Options (see $_options description)
  148. * @return TimeRanger_Profile
  149. */
  150. public static function setOptions(array $options)
  151. {
  152. foreach($options as $name => $value) {
  153. if (method_exists($this, "set$name")) {
  154. call_user_func(array(self, "set$name"), $value);
  155. } else {
  156. self::setOption($name, $value);
  157. }
  158. }
  159. }
  160. /**
  161. * Set option
  162. *
  163. * @throws TimeRanger_Exception
  164. * @param string $name Name of option
  165. * @param mixed $value Value of option
  166. * @return TimeRanger_Profile
  167. */
  168. public static function setOption($name, $value)
  169. {
  170. if (!array_key_exists($name, self::$_options)) {
  171. require_once 'TimeRanger/Exception.php';
  172. throw new TimeRanger_Exception("Unknown option '$name'");
  173. }
  174. self::$_options[$name] = $value;
  175. }
  176. /**
  177. * Get option
  178. *
  179. * @throws TimeRanger_Exception
  180. * @param string $name Name of option
  181. * @return mixed
  182. */
  183. public static function getOption($name)
  184. {
  185. if (!array_key_exists($name, self::$_options)) {
  186. require_once 'TimeRanger/Exception.php';
  187. throw new TimeRanger_Exception("Unknown option '$name'");
  188. }
  189. return self::$_options[$name];
  190. }
  191. /**
  192. * Instance for stop on destruct
  193. */
  194. public function __construct()
  195. {
  196. if (self::$_protectConstruct) {
  197. require_once 'TimeRanger/Exception.php';
  198. throw new TimeRanger_Exception("Do TimeRanger_Profiler::start() for start profiling");
  199. }
  200. }
  201. /**
  202. * Ensure stop profile
  203. */
  204. public function __destruct()
  205. {
  206. self::stop();
  207. }
  208. }