剖析PHP脚本的最简单方法 [英] Simplest way to profile a PHP script
本文介绍了剖析PHP脚本的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
配置PHP脚本最简单的方法是什么?
What's the easiest way to profile a PHP script?
我喜欢在上面显示一些内容,这些内容可以显示所有函数调用的转储以及它们花了多长时间,但我也可以在特定函数周围放些东西.
I'd love tacking something on that shows me a dump of all function calls and how long they took but I'm also OK with putting something around specific functions.
我尝试使用 microtime 函数进行实验:
I tried experimenting with the microtime function:
$then = microtime();
myFunc();
$now = microtime();
echo sprintf("Elapsed: %f", $now-$then);
但这有时会给我带来负面结果.另外,在我的代码中撒满这些代码也很麻烦.
but that sometimes gives me negative results. Plus it's a lot of trouble to sprinkle that all over my code.
推荐答案
PECL APD 扩展名是用法如下:
<?php
apd_set_pprof_trace();
//rest of the script
?>
之后,使用pprofp
解析生成的文件.
After, parse the generated file using pprofp
.
示例输出:
Trace for /home/dan/testapd.php
Total Elapsed Time = 0.00
Total System Time = 0.00
Total User Time = 0.00
Real User System secs/ cumm
%Time (excl/cumm) (excl/cumm) (excl/cumm) Calls call s/call Memory Usage Name
--------------------------------------------------------------------------------------
100.0 0.00 0.00 0.00 0.00 0.00 0.00 1 0.0000 0.0009 0 main
56.9 0.00 0.00 0.00 0.00 0.00 0.00 1 0.0005 0.0005 0 apd_set_pprof_trace
28.0 0.00 0.00 0.00 0.00 0.00 0.00 10 0.0000 0.0000 0 preg_replace
14.3 0.00 0.00 0.00 0.00 0.00 0.00 10 0.0000 0.0000 0 str_replace
警告:APD的最新版本为2004年,不再维护扩展名 /a>,并且存在各种兼容性问题(请参见评论).
这篇关于剖析PHP脚本的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文