分析PHP脚本最简单的方法是什么?
我喜欢在上面添加一些东西,告诉我所有函数调用的转储以及它们花了多长时间,但我也可以在特定的函数周围放一些东西。
我尝试了微时间功能:
$then = microtime();
myFunc();
$now = microtime();
echo sprintf("Elapsed: %f", $now-$then);
但这有时会给我带来负面结果。另外,在我的代码中散布这些代码会带来很多麻烦。
分析PHP脚本最简单的方法是什么?
我喜欢在上面添加一些东西,告诉我所有函数调用的转储以及它们花了多长时间,但我也可以在特定的函数周围放一些东西。
我尝试了微时间功能:
$then = microtime();
myFunc();
$now = microtime();
echo sprintf("Elapsed: %f", $now-$then);
但这有时会给我带来负面结果。另外,在我的代码中散布这些代码会带来很多麻烦。
当前回答
PECL XHPROF看起来也很有趣。它有可点击的HTML界面,用于查看报告和相当简单的文档。不过我还没有测试过。
其他回答
我喜欢使用phpDebug进行分析。 http://phpdebug.sourceforge.net/www/index.html
它输出使用的任何SQL以及所有包含的文件的所有时间/内存使用情况。显然,它在抽象的代码上工作得最好。
对于函数和类分析,我只使用microtime() + get_memory_usage() + get_peak_memory_usage()。
不需要扩展,只需使用这两个函数进行简单的概要分析。
// Call this at each point of interest, passing a descriptive string
function prof_flag($str)
{
global $prof_timing, $prof_names;
$prof_timing[] = microtime(true);
$prof_names[] = $str;
}
// Call this when you're done and want to see the results
function prof_print()
{
global $prof_timing, $prof_names;
$size = count($prof_timing);
for($i=0;$i<$size - 1; $i++)
{
echo "<b>{$prof_names[$i]}</b><br>";
echo sprintf(" %f<br>", $prof_timing[$i+1]-$prof_timing[$i]);
}
echo "<b>{$prof_names[$size-1]}</b><br>";
}
下面是一个例子,在每个检查点调用prof_flag(),并在结束时调用prof_print():
prof_flag("Start");
include '../lib/database.php';
include '../lib/helper_func.php';
prof_flag("Connect to DB");
connect_to_db();
prof_flag("Perform query");
// Get all the data
$select_query = "SELECT * FROM data_table";
$result = mysql_query($select_query);
prof_flag("Retrieve data");
$rows = array();
$found_data=false;
while($r = mysql_fetch_assoc($result))
{
$found_data=true;
$rows[] = $r;
}
prof_flag("Close DB");
mysql_close(); //close database connection
prof_flag("Done");
prof_print();
输出如下所示:
开始0.004303连接DB 0.003518执行查询0.000308检索数据0.000009关闭DB 0.000049Done
PECL APD扩展的用途如下:
<?php
apd_set_pprof_trace();
//rest of the script
?>
之后,使用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年,扩展不再维护,并有各种兼容性问题(见评论)。
我认为你需要xdebug。在服务器上安装它,打开它,通过kcachegrind (linux)或wincachegrind (windows)输出,它会向你展示一些漂亮的图表,详细说明确切的时间,计数和内存使用(但你需要另一个扩展)。
这真的很震撼:D
PECL XHPROF看起来也很有趣。它有可点击的HTML界面,用于查看报告和相当简单的文档。不过我还没有测试过。