You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

253 lines
7.1 KiB

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