xhprof的使用

来源:互联网 发布:西门子选型软件sizer 编辑:程序博客网 时间:2024/04/29 22:56

听说服务器上使用xhprof更好,于是参考了老王的博客,动手试了下。

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib
<directory_for_htdocs>
cd extension
phpize
./configure
make
make install

编辑php.ini:

[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:
如,在a.php中
// start profiling
xhprof_enable();

// run program
....

// stop profiler
$xhprof_data = xhprof_disable();

//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; //注意,这里和下一行的路径一定要正确,否则下面的new XHProfRuns_Default();就提示找不到class了
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new XHProfRuns_Default();

// Save the run under a namespace "xhprof_foo".
//
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
//
$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";

通过浏览器访问a.php,则在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件

然后可以通过web方式来查看

http://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo

<xhprof-ui-address>为xhprof_html所在的webserver的host,例,我的xhprof_html拷至了apache的htdocs下,webserver的documentroot为apache/htdocs,因此为http://a.com/xhprof_html/index.php?run=49bafaa3a3f66&source=xhprof_foo。source的值是死的,run就是xhprof.output_dir下要查看的文件名,不必多说了。


原创粉丝点击