1、安装扩展
windows下把 xhprof.dll 放到extensions目录下
修改配置文件
[xhprof]
extension=xhprof.so;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
;xhprof.output_dir=<directory_for_storing_xhprof_runs>
;调试信息的保存路径
xhprof.output_dir=/tmp/xhprof
linux下安装
wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9..tgz cd xhprof-0.9./extension/ sudo phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
把生成的 xhprof.so 放到扩展的目录下,并配置记录存放的路径
php中增加调试代码 sample.php 文件
function bar($x) {
if ($x > 0) {
bar($x - 1);
}
} function foo() {
for ($idx = 0; $idx < 5; $idx++) {
bar($idx);
$x = strlen("abc");
}
} //开启调试
xhprof_enable();
// cpu:XHPROF_FLAGS_CPU 内存:XHPROF_FLAGS_MEMORY
// 如果两个一起:XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
//要测试的php代码
foo(); //停止监测
$xhprof_data = xhprof_disable(); // display raw xhprof data for the profiler run
print_r($xhprof_data); //包含工具类,在下载的 tgz 包中可以找到
$XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; // save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default(); // xhprof_foo 指命名空间,可以为任意字符串
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); echo "---------------\n".
"Assuming you have set up the http based UI for \n".
"XHProf at some address, you can view run at \n".
"http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";
以表格方式查看
访问地址:http://test.cm/xhprof/xhprof_html/index.php?run=539d612de570e&source=xhprof_foo
run后的参数指生成的文件名, 目录再php.ini中的 xhprof.output_dir 指定
以图表方式查看
1、安装 Graphviz 软件(windows,linux版都有)
2、修改配置文件 config.php
3、 然后点击 view full callgraph 链接即可
红色节点是整个php程序执行过程中的瓶颈,黄色路径为整个过程耗时最长的路径
输出结果的含义:
ct 函数调用次数,
wt 花费的时间,
cpu 花费的 CPU 时间(微秒即百万分之一秒),
mu 使用的内存(bytes),
pmu 使用的内存峰值(bytes)。
web 分析结果页面含义
Calls:函数的调用次数
Incl. Wall Time (microsec) :包含内部函数花费的时间,单位微秒
Excl. Wall Time (microsec):不包含内部函数花费的时间,单位微秒
及所占百分比(%) 注:Incl.:为 Including 包含的简写
Excl.:为 Excluding 不包含的简写
Wall Time:意为挂钟时间即任务花费的时间
main():一个虚构的函数,程序根节点
bar@2:递归调用 2 次
Incl. CPU (microsecs):包含内部函数 CPU 花费的时间,单位微秒
Excl. CPU (microsec):不包含内部函数 CPU 花费的时间,单位微秒
Incl. MemUse (bytes):包含内部函数所占内存,单位字节
Excl. MemUse (bytes):不包含内部函数所占内存,单位字节
Incl. PeakMemUse (bytes):包含内部函数所占内存峰值,单位字节
Excl. PeakMemUse (bytes):不包含内部函数所占内存峰值,单位字节
及所占百分比(%) 可以认为共三种情况:
1. 包括内部函数
2. 不包括内部函数或者说函数本身
3. 所占总数(时间或内存使用)的百分比
参考文章:http://blog.aboutc.net/profiling/17/php-profiler-xhprof
http://www.cnblogs.com/bluefrog/archive/2012/03/01/2374922.html
软件下载(并非官方源文件)http://dev.freshsite.pl/php-extensions/xhprof.html