標籤:
Xhprof是facebook開源出來的一個PHP效能監控工具,佔用資源很少,甚至能夠在生產環境中進行部署。它可以結合graphviz使用,能夠以圖片的形式很直觀的展示代碼執行耗時wget http://pecl.php.net/get/xhprof-0.9.4.tgztar zxvf xhprof-0.9.4.tgzcd xhprof-0.9.4/extension//usr/bin/phpize./configure --with-php-config=/usr/local/php/bin/php-configmake && make install# 編輯php.ini:[xhprof]extension = xhprof.soxhprof.output_dir=/tmp重啟服務 service php-fpm restart # 測試下<?phpfunction my_dump($str) { print $str;}xhprof_enable(); // start profilingmy_dump(‘Funsion Wu‘); // run program$xhprof_data = xhprof_disable(); // stop profilerprint_r($xhprof_data); // display raw xhprof data for the profiler run?># 最後返回數組,就表示安裝好了。具體哪些值是什麼意思先別管,因為下面有UI的配置。會很直觀!yum -y install libjpeg freetype freetype-devel libjpeg-devel liberation-sans-fonts.noarch 自動安裝 yum install -y graphviz手動安裝:wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gztar zxf graphviz-2.24.0.tar.gzcd graphviz-2.24.0./configuremake && make install安裝完成後,會產生 /usr/local/bin/dot 檔案,你應該確保路徑在PATH環境變數裡,以便XHProf能找到它。vi /root/.bashrcexport PATH="$PATH:/usr/local/bin/dot"為Xhprof配置一個訪問網站(虛擬機器主機)比如做一個虛擬網域名稱 dev.xhprof.com綁定到xhprof的網站根目錄 /usr/local/xhprof-0.9.2/xhprof_html# 找到你要分析的代碼,在代碼開始處添加xhprof_enable(); // start profiling# 在代碼結束位置添加$xhprof_data = xhprof_disable(); // stop profiler, display raw xhprof data for the profiler runinclude_once ("/usr/local/src/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php"); # 請注意佈建網站 include_path 許可權include_once ("/usr/local/src/xhprof-0.9.4/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");$str = "\r\n------------------\r\n". "Assuming you have set up the http based UI for \r\n". "XHProf at some address, you can view run at \r\n". "http://dev.xhprof.com/index.php?run=$run_id&source=xhprof_foo". "\r\n------------------\r\n";echo nl2br($str);然後進入程式輸出的網址(比如 http://dev.xhprof.com/index.php?run=52c0ea0bef834&source=xhprof_foo ),進行查看下面是一些參數說明Inclusive Time 包括子函數所有執行時間。Exclusive Time/Self Time 函數執行本身花費的時間,不包括子樹執行時間。Wall Time 花去了的時間或掛鐘時間。CPU Time 使用者耗的時間+核心耗的時間Inclusive CPU 包括子函數一起所佔用的CPUExclusive CPU 函數自身所佔用的CPU點擊 [View Full Callgraph] 能夠以圖文的形式查看,很方便注意: 需要使用ctype這個擴充,Callgraph圖片產生依賴一些PHP系統級的函數,所以,最好去掉 php.ini 中的函數禁用
Xhprof安裝筆記(PHP效能監控)