一、安裝擴展 php
windows下把 xhprof.dll 放到extensions目錄下html
修改配置文件linux
[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下安裝web
wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2/extension/ sudo phpize ./configure --with-php-config=/usr/local/php/bin/php-config sudo make sudo make install
把生成的 xhprof.so 放到擴展的目錄下,並配置記錄存放的路徑windows
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 ui
run後的參數指生成的文件名, 目錄再php.ini中的 xhprof.output_dir 指定this
以圖表方式查看
一、安裝 Graphviz 軟件(windows,linux版都有)
二、修改配置文件 config.php
三、 而後點擊 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