M. *_*awy 113 php testing performance measurement
我怎么能说哪一类(哪些都做同样的工作)执行得更快?有没有可以衡量的软件?
Pas*_*TIN 182
你有(至少)两个解决方案:
相当"天真"的是在代码的一部分之前和之后使用microtime(true)来获得执行期间已经过了多少时间; 其他答案已经说过,并且已经举了一些例子,所以我不会说更多.
如果您想要对几条指令进行基准测试,这是一个很好的解决方案; 比如比较两种类型的函数 - 例如,如果做了几千次就更好了,以确保平均任何"扰动元素".
这样的事情,所以,如果你想知道序列化数组需要多长时间:
$before = microtime(true);
for ($i=0 ; $i<100000 ; $i++) {
serialize($list);
}
$after = microtime(true);
echo ($after-$before)/$i . " sec/serialize\n";
Run Code Online (Sandbox Code Playgroud)
不完美,但有用,而且设置不需要太多时间.
如果你想确定哪个函数在整个脚本中花费了大量时间,那么另一个解决方案就是使用:
要获取分析文件,您必须安装和配置Xdebug; 看一下文档的Profiling PHP Scripts页面.
我通常做的是默认情况下不启用探查器(它会生成相当大的文件,并减慢速度),但可以使用发送名为XDEBUG_PROFILEGET数据的参数的可能性来激活我需要的页面的分析.
我的php.ini中与profiling相关的部分如下所示:
xdebug.profiler_enable = 0 ; Profiling not activated by default
xdebug.profiler_enable_trigger = 1 ; Profiling activated when requested by the GET parameter
xdebug.profiler_output_dir = /tmp/ouput_directory
xdebug.profiler_output_name = files_names
Run Code Online (Sandbox Code Playgroud)
(阅读文档了解更多信息)
此截图来自KcacheGrind中的C++程序:http
://kcachegrind.sourceforge.net/html/pics/KcgShot3Large.gif http://kcachegrind.sourceforge.net/html/pics/KcgShot3Large.gif
您将完全获得与PHP脚本相同的东西
;-)(使用KCacheGrind,我的意思是; WinCacheGrind不如KCacheGrind ...)
这使您可以很好地了解应用程序中需要花费时间的内容 - 它有时肯定有助于找到减慢所有内容的功能^^
请注意,Xdebug计算PHP花费的CPU时间; 当PHP等待数据库的答案时(例如),它无法正常工作; 只有等待.所以Xdebug会认为DB请求不会花费太多时间!
这应该在SQL服务器上进行分析,而不是PHP,所以......
希望这有用:-)
玩得开心!
Sco*_*ers 40
为了快速,我这样做(在PHP中):
$startTime = microtime(true);
doTask(); // whatever you want to time
echo "Time: " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
Run Code Online (Sandbox Code Playgroud)
您也可以使用http://xdebug.org/等分析器.
我做了一个简单的计时课程,也许对某人有用:
class TimingHelper {
private $start;
public function __construct() {
$this->start = microtime(true);
}
public function start() {
$this->start = microtime(true);
}
public function segs() {
return microtime(true) - $this->start;
}
public function time() {
$segs = $this->segs();
$days = floor($segs / 86400);
$segs -= $days * 86400;
$hours = floor($segs / 3600);
$segs -= $hours * 3600;
$mins = floor($segs / 60);
$segs -= $mins * 60;
$microsegs = ($segs - floor($segs)) * 1000;
$segs = floor($segs);
return
(empty($days) ? "" : $days . "d ") .
(empty($hours) ? "" : $hours . "h ") .
(empty($mins) ? "" : $mins . "m ") .
$segs . "s " .
$microsegs . "ms";
}
}
Run Code Online (Sandbox Code Playgroud)
使用:
$th = new TimingHelper();
<..code being mesured..>
echo $th->time();
$th->start(); // if it's the case
<..code being mesured..>
echo $th->time();
// result: 4d 17h 34m 57s 0.00095367431640625ms
Run Code Online (Sandbox Code Playgroud)
小智 9
这是您问题的直接答案
有没有可以衡量的软件?
就在这里.我想知道为什么有人还没有提到它.虽然上面提到的答案似乎很适合快速检查,但从长远来看或者对于更大的项目来说是不可扩展的.
为什么不使用专门为此而构建的应用程序性能监视(APM)工具以及更多内容.查看NewRelic,AppDynamics,Ruxit(均具有免费版本),以监控每个应用程序的执行时间,资源使用情况,吞吐量到方法级别.
如果要快速测试框架的性能,可以将它放入index.php文件中
//at beginning
$milliseconds = round(microtime(true) * 1000);
//and at the end
echo round(microtime(true) * 1000) - $milliseconds;
Run Code Online (Sandbox Code Playgroud)
每次您将获得执行时间(以毫秒为单位)。因为微秒在测试框架案例中不太有用。
| 归档时间: |
|
| 查看次数: |
76911 次 |
| 最近记录: |