php性能分析工具xhprof

安装方法:php

wget http://pecl.php.net/get/xhprof-0.9.x.tgzvim

cp xhprof-0.9.x.tgz /home/www/xhprof.tgzphp-fpm

 

tar zxvf /home/www/xhprof.tgzui

cd xhprof/extensionthis

phpizespa

./configure --enable-xhprof --with-php-config=/usr/bin/php-config.net

make && make installrest

vim /etc/php.inicode

[xhprof]
extension=xhprof.so
;下面的这个地址是用来保存测量记录的目录,在页面输出测量获得的数据的时候,它会自动到这儿来找输出的文件。
xhprof.output_dir=/tmp

 service php-fpm restartblog

ok!

使用方法:

<?php
//来自xhprof的例子
function bar($x) {
  if ($x > 0) {
    bar($x - 1);
  }
}


// 在代码开头加上
xhprof_enable();
// xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);这个还能够分析cpu和内存使用状况
// 你的程序
foo();

// 在代码结尾加上
$xhprof_data = xhprof_disable();

// xhprof的数据
print_r($xhprof_data);


//这里是xhprof的地址,echo出来直接复制地址访问能够查看报告
$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();

// save the run under a namespace "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";