PHP擴充--XHProf最佳化PHP程式

來源:互聯網
上載者:User

標籤:

簡介

XHProf 是一個輕量級的分層效能測量分析器。 在資料收集階段,它跟蹤調用次數與測量資料,展示程式動態調用的弧線圖。 它在報告、後期處理階段計算了獨佔的效能度量,例如運行經過的時間、CPU 計算時間和記憶體開銷。 函數效能報告可以由調用者和被調用者終止。 在資料搜集階段 XHProf 通過調用圖的迴圈來檢測遞迴函式,通過賦予唯一的深度名稱來避免遞迴調用的迴圈。

XHProf 包含了一個基於 HTML 的簡單使用者介面(由 PHP 寫成)。 基於瀏覽器的使用者介面使得瀏覽、分享效能資料結果更加簡單方便。 同時也支援查看調用圖。

XHProf 的報告對理解代碼執行結構常常很有協助。 比如此分層報告可用於確定在哪個調用鏈裡調用了某個函數。

XHProf 對兩次運行進行比較(又名 “diff” 報告),或者多次運行資料的合計。 對比、合并報告,很像針對單次啟動並執行“平式視圖”效能報告,就像“分層式視圖”的效能報告。

安裝/配置

編譯安裝

wget http://pecl.php.net/get/xhprof-0.9.4.tgztar -zxvf xhprof-0.9.4.tgzcd xhprof-0.9.4/cd extension//usr/local/php-5.3.3/bin/phpize./configure --with-php-config=/usr/local/php-5.3.3/bin/php-configmakemake install

修改php.ini檔案

[xhprof]extension=xhprof.soxhprof.output_dir=/var/log/xhprof

以上完成後重啟apache,可以看到xhprof擴充

最後寫個測試檢驗一下

//test.php<?phpxhprof_enable();strlen(‘hello world‘);$xhprof_data = xhprof_disable();var_dump($xhprof_data);?>

返回結果(為了減少篇幅長度,這裡做了格式化)

array(3) {    ["main()==>strlen"]=>array(2) {        ["ct"]=> int(1)        ["wt"]=> int(4)    }    ["main()==>xhprof_disable"]=>array(2) {        ["ct"]=> int(1)        ["wt"]=> int(1)    }    ["main()"]=>array(2) {        ["ct"]=> int(1)        ["wt"]=> int(20)    }}
預定義常量

XHPROF_FLAGS_NO_BUILTINS (integer) | 使得跳過所有內建(內部)函數。
XHPROF_FLAGS_CPU (integer) | 使輸出的效能資料中添加 CPU 資料。
XHPROF_FLAGS_MEMORY (integer) | 使輸出的效能資料中添加記憶體資料。

可視化

xhprof 為我們提供了可視化的工具,將tar包裡面的xhprof_lib、xhprof_html複製到網站目錄下即可

cp -r xhprof_html/ xhprof_lib/ /usr/local/httpd-2.4.7/htdocs/xhprof

寫一個測試指令碼。把結果輸出到可視化介面上

<?phpxhprof_enable();for ($i = 0; $i <= 1000; $i++) {    $a = $i * $i;}$xhprof_data = xhprof_disable();$XHPROF_ROOT = "/www/project/merchant.wbaipay.com/html/xhprof";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_testing");echo "http://localhost/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";?>

開啟連結,可以查看效果

在需要調試的地方加如header.php

php_admin_value auto_prepend_file /var/www/xhprof/external/header.php

header.php

<?phpif (extension_loaded(‘xhprof‘) ){    xhprof_enable();    register_shutdown_function(function(){        $xhprof_data = xhprof_disable();        $XHPROF_ROOT = __DIR__.‘/..‘;        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_testing");        echo "http://192.168.41.121:93/?run={$run_id}&source=xhprof_testing\n";    });}else{    error_log(‘xhgui - either extension xhprof or uprofiler must be loaded‘);}

安裝Graphviz

wget "http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz"tar zxf graphviz-2.26.3.tar.gzcd graphviz-2.26.3./configure --prefix=/usrmake && make install


來自為知筆記(Wiz)

PHP擴充--XHProf最佳化PHP程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.