Php/速度改善/XHProf
提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:09時点における127.0.0.1 (トーク)による版 (ページの作成:「==事前インストール== yum install gcc ==php5.5以下インストール== cd /usr/local/src wget http://pecl.php.net/get/xhprof-0.9.3.tgz tar xvfz xhprof-0.9.3...」)
目次
事前インストール
yum install gcc
php5.5以下インストール
cd /usr/local/src wget http://pecl.php.net/get/xhprof-0.9.3.tgz tar xvfz xhprof-0.9.3.tgz cd xhprof-0.9.3/extension phpize ./configure make sudo make test sudo make install
php5.6以後
sudo yum -y install php-pecl-xhprof.x86_64 --enablerepo=remi --enablerepo=remi-php56
環境設定(/etc/php.ini
[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=/tmp/xhprof
インストール確認
$ php -r "phpinfo();" | grep xhprof xhprof xhprof => 0.9.3
解析UI設置
- xhprof_html
- xhprof_lib
XHProfの使い方
xhprof_enable(); // 処理 xhprof_disable();
コード例 †
xhprof/XhprofCustom.php
<?php
class XhprofCustom
{
static private $_starttime;
public function start()
{
if ($_SERVER['REQUEST_URI'] == "/favicon.ico") return;
xhprof_enable();
self::$_starttime = microtime(true);
}
// stop profiler
public function finish()
{
if ($_SERVER['REQUEST_URI'] == "/favicon.ico") return;
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = dirname(__FILE__); // xhprofをインストールしたディレクトリ
$XHPROF_SOURCE_NAME = 'app_name'; // アプリ名とか識別する名前
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);
$xhprof_log_file = "xhprof.html";
$xhprof_log_path = $XHPROF_ROOT.'/'.$xhprof_log_file;
$time = floor((microtime(true) - self::$_starttime) * 100000) / 100;
// ビューアへのリンクログ
file_put_contents($xhprof_log_path, date("Y-m-d H:i:s")." {$time} <a href=\"./xhprof_html/index.php?run=$run_id&source=$XHPROF_SOURCE_NAME\">{$_SERVER['REQUEST_URI']}</a><br />\n", FILE_APPEND);
}
}
index.php
if (file_exists(dirname(__FILE__) . '/xhprof/XhprofCustom.php')) {
require_once dirname(__FILE__) . '/xhprof/XhprofCustom.php';
XhprofCustom::start();
register_shutdown_function(array('XhprofCustom', 'finish'));
}
以下エラーが出るときgraphvizを入れる
- Error: either we can not find profile data for run_id
yum --enablerepo=remi install graphviz graphviz-devel graphviz-gd graphviz-php
以下エラーが出るときはxdebugなどのxhprof以外のプロファイラーをuninstallする
Fatal error: Class ~ not found
