nidBox

PHP 效能檢測工具:XHProf

2009-09-30 17:39


XHProf 是 facebook 開發的軟體 (open source)


XHProf 下載
http://pecl.php.net/package/xhprof


XHProf Documentation (Draft)  (facebook 官方文件)
http://mirror.facebook.net/facebook/xhprof/doc.html
(note: facebook 的一些open source/軟體 http://developers.facebook.com/opensource.php )

安裝

# wget http://pecl.php.net/get/xhprof-0.9.2.tgz
# zcat xhprof-0.9.2.tgz | tar xfv -
# cd xhprof-0.9.2
# cp -r xhprof_html xhprof_lib ~www/htdocs
# cd extension
# phpize
# ./configure
# make
# make install



/usr/local/lib/php.ini
加 3 行
[xhprof]
extension=xhprof.so
xhprof.output_dir= /tmp
  (資料暫存檔存放位置,注意讀寫權限)


若要以圖形來顯示 profile
安裝 Graphviz

# wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
# zcat graphviz-2.24.0.tar.gz | tar xfv -
# cd graphviz-2.24.0
# ./configure
# make
# make install


php的使用方式:

<?
xhprof_enable();


程式碼 1
程式碼 2
程式碼 3
..........


$xhprof_data = xhprof_disable();

$XHPROF_ROOT='/home/htdocs/';

include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

$profiler_namespace='xhprof_foo';
$profiler_url =sprintf('/xhprof_html/index.php?run=%s&source=%s',$run_id, $profiler_namespace);
echo "<a href=$profiler_url>profiling</a>";
?>


report畫面的幾個名詞:
  1. Inclusive Time (or Subtree Time): Includes time spent in the function as well as in descendant functions called from a given function.
  2. Exclusive Time/Self Time: Measures time spent in the function itself. Does not include time in descendant functions.
  3. Wall Time: a.k.a. Elapsed time or wall clock time.
  4. CPU Time: CPU time in user space + CPU time in kernel space

wordpress 執行結果: