分析 PHP 脚本的最简单方法 [英] Simplest way to profile a PHP script

查看:37
本文介绍了分析 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 功能:

$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 年,扩展名 不再维护 并且有各种兼容性问题(请参阅评论).

Warning: the latest release of APD is dated 2004, the extension is no longer maintained and has various compability issues (see comments).

这篇关于分析 PHP 脚本的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆