Debug.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\exception\ClassNotFoundException;
  13. use think\response\Redirect;
  14. class Debug
  15. {
  16. // 区间时间信息
  17. protected static $info = [];
  18. // 区间内存信息
  19. protected static $mem = [];
  20. /**
  21. * 记录时间(微秒)和内存使用情况
  22. * @param string $name 标记位置
  23. * @param mixed $value 标记值 留空则取当前 time 表示仅记录时间 否则同时记录时间和内存
  24. * @return mixed
  25. */
  26. public static function remark($name, $value = '')
  27. {
  28. // 记录时间和内存使用
  29. self::$info[$name] = is_float($value) ? $value : microtime(true);
  30. if ('time' != $value) {
  31. self::$mem['mem'][$name] = is_float($value) ? $value : memory_get_usage();
  32. self::$mem['peak'][$name] = memory_get_peak_usage();
  33. }
  34. }
  35. /**
  36. * 统计某个区间的时间(微秒)使用情况 返回值以秒为单位
  37. * @param string $start 开始标签
  38. * @param string $end 结束标签
  39. * @param integer|string $dec 小数位
  40. * @return integer
  41. */
  42. public static function getRangeTime($start, $end, $dec = 6)
  43. {
  44. if (!isset(self::$info[$end])) {
  45. self::$info[$end] = microtime(true);
  46. }
  47. return number_format((self::$info[$end] - self::$info[$start]), $dec);
  48. }
  49. /**
  50. * 统计从开始到统计时的时间(微秒)使用情况 返回值以秒为单位
  51. * @param integer|string $dec 小数位
  52. * @return integer
  53. */
  54. public static function getUseTime($dec = 6)
  55. {
  56. return number_format((microtime(true) - THINK_START_TIME), $dec);
  57. }
  58. /**
  59. * 获取当前访问的吞吐率情况
  60. * @return string
  61. */
  62. public static function getThroughputRate()
  63. {
  64. return number_format(1 / self::getUseTime(), 2) . 'req/s';
  65. }
  66. /**
  67. * 记录区间的内存使用情况
  68. * @param string $start 开始标签
  69. * @param string $end 结束标签
  70. * @param integer|string $dec 小数位
  71. * @return string
  72. */
  73. public static function getRangeMem($start, $end, $dec = 2)
  74. {
  75. if (!isset(self::$mem['mem'][$end])) {
  76. self::$mem['mem'][$end] = memory_get_usage();
  77. }
  78. $size = self::$mem['mem'][$end] - self::$mem['mem'][$start];
  79. $a = ['B', 'KB', 'MB', 'GB', 'TB'];
  80. $pos = 0;
  81. while ($size >= 1024) {
  82. $size /= 1024;
  83. $pos++;
  84. }
  85. return round($size, $dec) . " " . $a[$pos];
  86. }
  87. /**
  88. * 统计从开始到统计时的内存使用情况
  89. * @param integer|string $dec 小数位
  90. * @return string
  91. */
  92. public static function getUseMem($dec = 2)
  93. {
  94. $size = memory_get_usage() - THINK_START_MEM;
  95. $a = ['B', 'KB', 'MB', 'GB', 'TB'];
  96. $pos = 0;
  97. while ($size >= 1024) {
  98. $size /= 1024;
  99. $pos++;
  100. }
  101. return round($size, $dec) . " " . $a[$pos];
  102. }
  103. /**
  104. * 统计区间的内存峰值情况
  105. * @param string $start 开始标签
  106. * @param string $end 结束标签
  107. * @param integer|string $dec 小数位
  108. * @return mixed
  109. */
  110. public static function getMemPeak($start, $end, $dec = 2)
  111. {
  112. if (!isset(self::$mem['peak'][$end])) {
  113. self::$mem['peak'][$end] = memory_get_peak_usage();
  114. }
  115. $size = self::$mem['peak'][$end] - self::$mem['peak'][$start];
  116. $a = ['B', 'KB', 'MB', 'GB', 'TB'];
  117. $pos = 0;
  118. while ($size >= 1024) {
  119. $size /= 1024;
  120. $pos++;
  121. }
  122. return round($size, $dec) . " " . $a[$pos];
  123. }
  124. /**
  125. * 获取文件加载信息
  126. * @param bool $detail 是否显示详细
  127. * @return integer|array
  128. */
  129. public static function getFile($detail = false)
  130. {
  131. if ($detail) {
  132. $files = get_included_files();
  133. $info = [];
  134. foreach ($files as $key => $file) {
  135. $info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )';
  136. }
  137. return $info;
  138. }
  139. return count(get_included_files());
  140. }
  141. /**
  142. * 浏览器友好的变量输出
  143. * @param mixed $var 变量
  144. * @param boolean $echo 是否输出 默认为true 如果为false 则返回输出字符串
  145. * @param string $label 标签 默认为空
  146. * @param integer $flags htmlspecialchars flags
  147. * @return void|string
  148. */
  149. public static function dump($var, $echo = true, $label = null, $flags = ENT_SUBSTITUTE)
  150. {
  151. $label = (null === $label) ? '' : rtrim($label) . ':';
  152. ob_start();
  153. var_dump($var);
  154. $output = ob_get_clean();
  155. $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
  156. if (IS_CLI) {
  157. $output = PHP_EOL . $label . $output . PHP_EOL;
  158. } else {
  159. if (!extension_loaded('xdebug')) {
  160. $output = htmlspecialchars($output, $flags);
  161. }
  162. $output = '<pre>' . $label . $output . '</pre>';
  163. }
  164. if ($echo) {
  165. echo($output);
  166. return;
  167. } else {
  168. return $output;
  169. }
  170. }
  171. public static function inject(Response $response, &$content)
  172. {
  173. $config = Config::get('trace');
  174. $type = isset($config['type']) ? $config['type'] : 'Html';
  175. $request = Request::instance();
  176. $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\' . ucwords($type);
  177. unset($config['type']);
  178. if (class_exists($class)) {
  179. $trace = new $class($config);
  180. } else {
  181. throw new ClassNotFoundException('class not exists:' . $class, $class);
  182. }
  183. if ($response instanceof Redirect) {
  184. //TODO 记录
  185. } else {
  186. $output = $trace->output($response, Log::getLog());
  187. if (is_string($output)) {
  188. // trace调试信息注入
  189. $pos = strripos($content, '</body>');
  190. if (false !== $pos) {
  191. $content = substr($content, 0, $pos) . $output . substr($content, $pos);
  192. } else {
  193. $content = $content . $output;
  194. }
  195. }
  196. }
  197. }
  198. }