Debug.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ qrmaTa+NriMTJ48TshnokxMdVBOgkgKyaCcqQAEZOSU=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2016 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.8
  11. */
  12. namespace Cube;
  13. /**
  14. * debug and different usage statistics class class
  15. *
  16. * Class Debug
  17. *
  18. * @package Cube
  19. */
  20. class Debug
  21. {
  22. protected static $_memoryStart;
  23. protected static $_memoryEnd;
  24. protected static $_timeStart;
  25. protected static $_timeEnd;
  26. protected static $_sqlQueries = array();
  27. protected static $_sqlCount = 0;
  28. protected static $_cpuUsageStart;
  29. protected static $_cpuUsageEnd;
  30. protected static $_cpuTimeUsageStart;
  31. protected static $_cpuTimeUsageEnd;
  32. public static function setMemoryStart()
  33. {
  34. self::$_memoryStart = self::_getMemory();
  35. }
  36. public static function setMemoryEnd()
  37. {
  38. self::$_memoryEnd = self::_getMemory();
  39. }
  40. public static function setTimeStart()
  41. {
  42. self::$_timeStart = self::_getCurrentTime();
  43. }
  44. public static function setTimeEnd()
  45. {
  46. self::$_timeEnd = self::_getCurrentTime();
  47. }
  48. public static function addSqlCount()
  49. {
  50. self::$_sqlCount++;
  51. }
  52. public static function setCpuUsageStart()
  53. {
  54. self::$_cpuUsageStart = self::_getCpuUsage();
  55. self::$_cpuTimeUsageStart = microtime(true);
  56. }
  57. public static function setCpuUsageEnd()
  58. {
  59. self::$_cpuUsageEnd = self::_getCpuUsage();
  60. self::$_cpuTimeUsageEnd = microtime(true);
  61. }
  62. /**
  63. *
  64. * add sql query to the debug log
  65. *
  66. * @param string $query
  67. * @param null $count
  68. */
  69. public static function addSqlQuery($query, $count = null)
  70. {
  71. if ($count === null) {
  72. $count = count(self::$_sqlQueries);
  73. }
  74. $time = self::_getCurrentTime();
  75. if (array_key_exists($count, self::$_sqlQueries)) {
  76. $time -= self::$_sqlQueries[$count]['time'];
  77. }
  78. self::$_sqlQueries[$count] = array(
  79. 'query' => (string)$query,
  80. 'time' => $time,
  81. );
  82. }
  83. /**
  84. *
  85. * get current sql query count
  86. *
  87. * @return integer
  88. */
  89. public static function getSqlCount()
  90. {
  91. return self::$_sqlCount;
  92. }
  93. /**
  94. *
  95. * will return the memory usage in KB
  96. *
  97. * @return string
  98. */
  99. public static function getMemoryUsage()
  100. {
  101. self::setMemoryEnd();
  102. return number_format((self::$_memoryEnd - self::$_memoryStart) / 1024, 2);
  103. }
  104. /**
  105. *
  106. * will return the loading time in seconds with 3 decimals
  107. *
  108. * @return string
  109. */
  110. public static function getLoadingSpeed()
  111. {
  112. self::setTimeEnd();
  113. return number_format((self::$_timeEnd - self::$_timeStart), 3);
  114. }
  115. /**
  116. *
  117. * will return the sql queries executed
  118. * if min time is > 0, return queries with running time that is greater
  119. * than the input value
  120. *
  121. * @param float $minTime
  122. *
  123. * @return array
  124. */
  125. public static function getSqlQueries($minTime = null)
  126. {
  127. $queries = array();
  128. if ($minTime > 0) {
  129. foreach (self::$_sqlQueries as $query) {
  130. if ($query['time'] >= $minTime) {
  131. $queries[] = $query;
  132. }
  133. }
  134. return $queries;
  135. }
  136. return self::$_sqlQueries;
  137. }
  138. /**
  139. *
  140. * will return the number of sql queries executed
  141. *
  142. * @return integer
  143. */
  144. public static function getCountSqlQueries()
  145. {
  146. return count(self::$_sqlQueries);
  147. }
  148. /**
  149. *
  150. * returns cpu usage
  151. *
  152. * @return float
  153. */
  154. public static function getCpuUsage()
  155. {
  156. self::setCpuUsageEnd();
  157. $time = (self::$_cpuTimeUsageEnd - self::$_cpuTimeUsageStart) * 1000000;
  158. if ($time > 0) {
  159. $usec = self::$_cpuUsageEnd - self::$_cpuUsageStart;
  160. return sprintf("%01.2f", ($usec / $time) * 100);
  161. }
  162. return 0;
  163. }
  164. /**
  165. *
  166. * sets a loading time variable
  167. *
  168. * @return integer
  169. */
  170. protected static function _getCurrentTime()
  171. {
  172. list($usec, $sec) = explode(" ", microtime());
  173. return ((float)$usec + (float)$sec);
  174. }
  175. /**
  176. *
  177. * sets a memory load variable
  178. *
  179. * @return integer
  180. */
  181. protected static function _getMemory()
  182. {
  183. return memory_get_usage();
  184. }
  185. /**
  186. *
  187. * get cpu usage
  188. *
  189. * @return float|null
  190. */
  191. protected static function _getCpuUsage()
  192. {
  193. if (function_exists('getrusage')) {
  194. $data = getrusage();
  195. return $data["ru_utime.tv_sec"] * 1e6 + $data["ru_utime.tv_usec"];
  196. }
  197. return 0;
  198. }
  199. }