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