剖析PHP脚本的最简单方法
时间:2020-03-05 18:41:46 来源:igfitidea点击:
剖析PHP脚本的最简单方法是什么?
我喜欢在上面显示一些内容,这些内容可以显示所有函数调用的转储以及它们花了多长时间,但我也可以在特定函数周围放些东西。
我尝试使用microtime函数进行实验:
$then = microtime(); myFunc(); $now = microtime(); echo sprintf("Elapsed: %f", $now-$then);
但这有时给我负面的结果。另外,在我的代码中撒满这些代码也很麻烦。
解决方案
回答
我喜欢使用phpDebug进行性能分析。
http://phpdebug.sourceforge.net/www/index.html
它输出所有使用的SQL以及所有包含的文件的所有时间/内存使用情况。显然,它在抽象的代码上效果最佳。
对于函数和类分析,我将仅使用microtime()
+get_memory_usage()
+get_peak_memory_usage()
。
回答
我想你想要xdebug。将其安装在服务器上,打开电源,通过kcachegrind(对于Linux)或者wincachegrind(对于Windows)泵送输出,它将为我们显示一些漂亮的图表,详细说明了确切的时间,计数和内存使用情况(但我们会为此需要另一个扩展)。
它摇晃,严重:D
回答
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
回答
对于基准测试,就像示例一样,我使用了梨基准测试包。我们设置用于测量的标记。该课程还提供了一些演示帮助器,或者我们可以根据需要处理数据。
实际上,我使用__destruct方法将其包装在另一个类中。当脚本退出时,输出将通过log4php记录到syslog中,因此我有很多性能数据可以使用。