剖析PHP脚本的最简单方法是什么?
我喜欢在上面显示一些内容,这些内容可以显示所有函数调用的转储以及它们花费了多长时间,但我也可以在特定函数周围添加一些内容。
我尝试使用microtime函数进行实验:
$then = microtime(); myFunc(); $now = microtime(); echo sprintf("Elapsed: %f", $now-$then);
但这有时给我负面的结果。另外,在我的代码中撒满代码也很麻烦。
所述PECL APD扩展的使用如下:
<?php apd_set_pprof_trace(); //rest of the script ?>
之后,使用解析生成的文件pprofp。
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年,该扩展不再维护,并存在各种兼容性问题(请参阅注释)。