Debug.php 7.3 KB

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