XHProfでPHPを解析する

  • 投稿者:
  • 投稿カテゴリー:PHP

ブラウザ上でプロファイリングすることが可能。xdebugよりも手軽

インストール方法

root@hostname:/usr/local/src# wget -d http://pecl.php.net/get/xhprof-0.9.4.tgz
DEBUG output created by Wget 1.13.4 on linux-gnu.
...

root@hostname:/usr/local/src# tar xzvf xhprof-0.9.4.tgz 
package.xml
xhprof-0.9.4/examples/sample.php
xhprof-0.9.4/extension/tests/common.php
xhprof-0.9.4/extension/tests/xhprof_001.phpt
xhprof-0.9.4/extension/tests/xhprof_002.phpt
xhprof-0.9.4/extension/tests/xhprof_003.phpt
xhprof-0.9.4/extension/tests/xhprof_004_inc.php
xhprof-0.9.4/extension/tests/xhprof_004_require.php
xhprof-0.9.4/extension/tests/xhprof_004.phpt
xhprof-0.9.4/extension/tests/xhprof_005.phpt
xhprof-0.9.4/extension/tests/xhprof_006.phpt
xhprof-0.9.4/extension/tests/xhprof_007.phpt
xhprof-0.9.4/extension/tests/xhprof_008.phpt
xhprof-0.9.4/extension/config.m4
xhprof-0.9.4/extension/php_xhprof.h
xhprof-0.9.4/extension/xhprof.c
xhprof-0.9.4/xhprof_html/css/xhprof.css
xhprof-0.9.4/xhprof_html/docs/index.html
xhprof-0.9.4/xhprof_html/docs/sample-callgraph-image.jpg
xhprof-0.9.4/xhprof_html/docs/sample-diff-report-flat-view.jpg
xhprof-0.9.4/xhprof_html/docs/sample-diff-report-parent-child-view.jpg
xhprof-0.9.4/xhprof_html/docs/sample-flat-view.jpg
xhprof-0.9.4/xhprof_html/docs/sample-parent-child-view.jpg
xhprof-0.9.4/xhprof_html/jquery/indicator.gif
xhprof-0.9.4/xhprof_html/jquery/jquery-1.2.6.js
xhprof-0.9.4/xhprof_html/jquery/jquery.autocomplete.css
xhprof-0.9.4/xhprof_html/jquery/jquery.autocomplete.js
xhprof-0.9.4/xhprof_html/jquery/jquery.tooltip.css
xhprof-0.9.4/xhprof_html/jquery/jquery.tooltip.js
xhprof-0.9.4/xhprof_html/js/xhprof_report.js
xhprof-0.9.4/xhprof_html/callgraph.php
xhprof-0.9.4/xhprof_html/index.php
xhprof-0.9.4/xhprof_html/typeahead.php
xhprof-0.9.4/xhprof_lib/display/typeahead_common.php
xhprof-0.9.4/xhprof_lib/display/xhprof.php
xhprof-0.9.4/xhprof_lib/utils/callgraph_utils.php
xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php
xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php
xhprof-0.9.4/CHANGELOG
xhprof-0.9.4/CREDITS
xhprof-0.9.4/README
xhprof-0.9.4/LICENSE
root@hostname:/usr/local/src# cd xhprof-0.9.4/extension/
root@hostname:/usr/local/src/xhprof-0.9.4/extension# phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

root@hostname:/usr/local/src/xhprof-0.9.4/extension# ./configure 
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
...
root@hostname:/usr/local/src/xhprof-0.9.4/extension# make
...
root@hostname:/usr/local/src/xhprof-0.9.4/extension# make install
Installing shared extensions:     /usr/lib/php5/20100525/
root@hostname:/usr/local/src/xhprof-0.9.4/extension# vi /etc/php5/conf.d/30-xhprof.ini

phpinfoで確認する
Screenshot 2014-11-11 at 01.05.00

プロファイルしてみる

root@hostname:/var/www/htdocs# cat index.php
<?php
// プロファイリングの開始
xhprof_enable();

phpinfo();

// プロファイリングの終了
$xhprof_data = xhprof_disable();
// 結果の表示
var_dump($xhprof_data);
?>

Screenshot 2014-11-11 at 01.19.48

CodeIgniterを表示ツールを利用してプロファイルする

表示ツールは、解凍ファイル内の「xhprof_html」と「xhprof_lib」が必要とのこと

root@hostname:/var/www/htdocs# cp -Rp /usr/local/src/xhprof-0.9.4 ./
root@hostname:/var/www/htdocs# mv xhprof-0.9.4 xhprof
root@hostname:/var/www/htdocs# chown -R www-data:www-data xhprof/
root@hostname:/var/www/htdocs# aptitude install graphviz # グラフ表示のために必要
以下の新規パッケージ
  fonts-liberation graphviz libcdt4{a} libcgraph5{a} libgraph4{a} libgvc5{a} libgvpr1{a} libpathplan4{a} libxdot4{a} ttf-liberation{a} 
...
root@hostname:/var/www/htdocs# vi CI/index.php # 先頭と末尾に以下のように追加する
<?php

//----------------------------------------
// XHProfを使うかどうかの設定 TRUE / FALSE
$xhprof_mode = TRUE;
 
// start profiler
if ($xhprof_mode) {
    xhprof_enable();
}
//----------------------------------------

............(中略)


//----------------------------------------
// stop profiler
if ($xhprof_mode) {
    $xhprof_data = xhprof_disable();
 
    $XHPROF_ROOT = '/var/www/htdocs/xhprof';  //xhprofをインストールしたディレクトリ
    $XHPROF_SOURCE_NAME = 'CodeIgniter';
    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_SOURCE_NAME);
 
    echo "<a href='http://hostname/xhprof/xhprof_html/index.php?run=$run_id&source=$XHPROF_SOURCE_NAME' target='_blank'>xhprof Result</a>";
}
//----------------------------------------

Screenshot 2014-11-11 at 02.14.29

Screenshot 2014-11-11 at 02.15.25

Screenshot 2014-11-11 at 02.16.26

参考URL

PHPのプロファイラー「XHProf」の使い方
http://hamamuratakuo.blog61.fc2.com/blog-entry-466.html

【CakePHP】xhprofでCakePHPのパフォーマンスを丸裸にする
http://blog.ecworks.jp/archives/467