暫無描述
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.

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