原创文章,转载请注明: 转载自始终不够
本文链接地址: 简单PHP性能记录工具
转载请注明:始终不够 » 简单PHP性能记录工具
最近写了一个异步mysql客户端的封装,想与传统的串行方式做下性能对比。包括运行时间、内存使用情况等信息。在github和packagist上搜索并没有找到自己想要的,xhprof又太大了,结果也太复杂,不符合现在的需要。所以决定自己写一个package,来实现对php脚本运行时间和内存使用情况的监控,并生成报告。
项目地址:https://github.com/huyanping/php-timer
示例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
require
dirname(dirname(
__FILE__
)) . DIRECTORY_SEPARATOR .
'vendor'
. DIRECTORY_SEPARATOR .
'autoload.php'
;
//初始化,设置内存单位
$timer
=
new
\Jenner\Timer(\Jenner\Timer::UNIT_KB);
//记录a状态
$timer
->mark(
'a'
);
sleep(2);
//记录b状态
$timer
->mark(
'b'
);
sleep(3);
//记录c状态
$timer
->mark(
'c'
);
sleep(4);
//记录d状态
$timer
->mark(
'd'
);
//打印总体报告(不包含差值)
$timer
->printReport();
//获取总体报告,返回数组
$report
=
$timer
->getReport();
//获取一个mark的报告
$a_report
=
$timer
->getReport(
'a'
);
print_r(
$a_report
);
//打印a状态和b状态的差异信息,包含运行时间、使用内存等
$timer
->printDiffReportByStartAndEnd(
'a'
,
'b'
);
//获取a状态和b状态的差异报告
$ab_diff_report
=
$timer
->getDiffReportByStartAndEnd(
'a'
,
'b'
);
//打印第一个mark和最后一个mark之间的差异信息
$timer
->printDiffReport();
//获取第一个mark和最后一个mark之间的差异信息
$diff_report
=
$timer
->getDiffReport();
|
输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
------------------------------------------
mark:a
time
:1437535424.9998s
memory_real:1280KB
memory_emalloc:833.046875KB
memory_peak_real:1280KB
memory_peak_emalloc:843.2890625KB
------------------------------------------
mark:b
time
:1437535427s
memory_real:1280KB
memory_emalloc:834.2265625KB
memory_peak_real:1280KB
memory_peak_emalloc:843.2890625KB
------------------------------------------
mark:c
time
:1437535430.0002s
memory_real:1280KB
memory_emalloc:835.1875KB
memory_peak_real:1280KB
memory_peak_emalloc:843.2890625KB
------------------------------------------
mark:d
time
:1437535434.0004s
memory_real:1280KB
memory_emalloc:836.1484375KB
memory_peak_real:1280KB
memory_peak_emalloc:843.2890625KB
Array
(
[
time
] => 1437535424.9998
[memory_real] => 1310720
[memory_emalloc] => 853040
[memory_peak_real] => 1310720
[memory_peak_emalloc] => 863528
)
------------------------------------------
mark:[
diff
] start_mark:a end_mark:b
time
:2.0001850128174s
memory_real:0KB
memory_emalloc:1.1796875KB
memory_peak_real:0KB
memory_peak_emalloc:0KB
------------------------------------------
mark:[total
diff
]
time
:9.0006000995636s
memory_real:0KB
memory_emalloc:3.1015625KB
memory_peak_real:0KB
memory_peak_emalloc:0KB
|