Completed
Push — 6.0 ( bcbbbf...49864a )
by liu
03:05
created

Debug::getMemPeak()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Debug
Loading history...
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 = [])
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

37
    public function __construct(App $app, /** @scrutinizer ignore-unused */ array $config = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
38
    {
39
        $this->app = $app;
40
    }
41
42
    public static function __make(App $app)
2 ignored issues
show
Coding Style introduced by
Method name "Debug::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Debug::__make" must not be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
43
    {
44
        return new static($app);
45
    }
46
47
    /**
48
     * 记录时间(微秒)和内存使用情况
49
     * @access public
50
     * @param  string    $name 标记位置
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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 结束标签
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
70
     * @param  integer|string    $dec 小数位
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
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 结束标签
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
108
     * @param  integer|string    $dec 小数位
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
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 结束标签
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
154
     * @param  integer|string    $dec 小数位
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
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 变量
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
179
     * @param  boolean       $echo 是否输出 默认为true 如果为false 则返回输出字符串
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
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);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var) looks like debug code. Are you sure you do not want to remove it?
Loading history...
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