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";
相關文章
相關標籤/搜索