facebook twitter hatena line email

Php/速度改善/XHGui

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

前提

php/速度改善/XHProf [ショートカット]をインストール

インストール

cd /usr/local/src
wget http://github.com/preinheimer/xhprof/tarball/master --no-check-certificate
tar -xvf master
cd preinheimer-xhprof-a73fca8
cp xhprof_lib/config.sample.php xhprof_lib/config.php
mkdir /var/www/xhgui -m 777
cp -r /usr/local/src/preinheimer-xhprof-a73fca8/xhprof_lib/* /var/www/xhgui/xhprof_lib
cp -r /usr/local/src/preinheimer-xhprof-a73fca8/xhprof_html/* /var/www/xhgui/xhprof_html


mysqlのDB・ユーザ設定

vi xhprof_lib/config.php

dbのportを変更する場合

vi xhprof_lib/config.php
$_xhprof['dbhost'] = '192.168.1.1;port=1234';

mysqlスキーマ作成

CREATE DATABASE xhprof DEFAULT CHARACTER SET utf8;
use xhprof;
CREATE TABLE `details` (
  `id` char(17) NOT NULL,
  `url` varchar(255) default NULL,
  `c_url` varchar(255) default NULL,
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `server name` varchar(64) default NULL,
  `perfdata` MEDIUMBLOB,
  `type` tinyint(4) default NULL,
  `cookie` BLOB,
  `post` BLOB,
  `get` BLOB,
  `pmu` int(11) default NULL,
  `wt` int(11) default NULL,
  `cpu` int(11) default NULL,
  `server_id` char(3) NOT NULL default 't11',
  `aggregateCalls_include` varchar(255) DEFAULT NULL,
  PRIMARY KEY  (`id`),
  KEY `url` (`url`),
  KEY `c_url` (`c_url`),
  KEY `cpu` (`cpu`),
  KEY `wt` (`wt`),
  KEY `pmu` (`pmu`),
  KEY `timestamp` (`timestamp`),
  KEY `server name` (`server name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

コード例

xhgui/XhguiCustom.php

class XhguiCustom
{
  public function start()
  {
    xhprof_enable();
  }
  // stop profiler
  public function finish()
  {
       // stop profiler
       $xhprof_data = xhprof_disable();
       $XHPROF_ROOT = dirname(__FILE__); // xhprofをインストールしたディレクトリ
       $XHPROF_SOURCE_NAME = 'app_name';            // アプリ名とか識別する名前
       define('XHPROF_LIB_ROOT', $XHPROF_ROOT . '/xhprof_lib/');
       global $_xhprof;
       include_once $XHPROF_ROOT . "/xhprof_lib/config.php";
       include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
       // include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs_mysql.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=\"/xhgui/xhprof_html/index/php\">xhprof Result</a>\n-->";
     }
}

index.php

if (file_exists(dirname(__FILE__) . '/xhgui/XhguiCustom.php')) {
     require_once dirname(__FILE__) . '/xhgui/XhguiCustom.php';
     XhguiCustom::start();
     register_shutdown_function(array('XhguiCustom', 'finish'));
}

You do not have permission to view this page.エラーが出る場合

xhprof_lib/config.phpの$controlIPsにアクセス元IP追加

failed to shell execute cmd=" "" -Tpng" エラーがでる場合

config.phpの以下がコメントアウトされてないか確認

$_xhprof['dot_binary']  = '/usr/bin/dot';
$_xhprof['dot_tempdir'] = '/tmp';
$_xhprof['dot_errfile'] = '/tmp/xh_dot.err';

Call to undefined function mysqli_connect()エラーとなる場合

xhprof_lib/display/xhprof.phpの以下を修正

//include_once XHPROF_LIB_ROOT . '/utils/xhprof_runs.php';
include_once XHPROF_LIB_ROOT . '/utils/xhprof_runs_mysql.php';

Error producing callgraph, check /tmp/xh_dot.errエラーとなる場合

yum --enablerepo=remi install graphviz graphviz-devel graphviz-gd graphviz-php

以下エラーが出る際

  • sh: /usr/bin/dot: No such file or directory
  • Error producing callgraph, check /tmp/xh_dot.err

config.phpを適宜書き換える

-$_xhprof['dot_binary']  = '/usr/bin/dot';
+$_xhprof['dot_binary']  = '/usr/local/bin/dot';

複数サイトでxhguiを使う場合

以下sqlが走って重くなるので、

SELECT DISTINCT(`server name`) FROM `details`

テーブルに以下indexを追加する

KEY `server name` (`server name`)

noticeエラーを削除

<?php
error_reporting(E_ALL & ~E_NOTICE);

自動ログ削除

以下scriptを作成

以下の例だと5000レコードを超えると削除される。

  • xhprof_lib/utils/xhprof_old_del.php
<?php
define('XHPROF_LIB_ROOT', __DIR__ . "/../");
require_once XHPROF_LIB_ROOT . '/config.php';
$xhprofOldDel = new XhprofOldDel();
$xhprofOldDel->exec();
/**
 * 古いXhprofログ削除
 */
class XhprofOldDel
{
  protected $db;
  protected $cnt = 5000;// を超えたログは削除
  public function __construct($dir = null)
  {
    $this->db();
  }
  protected function db()
  {
  global $_xhprof;
  require_once XHPROF_LIB_ROOT.'/utils/Db/'.$_xhprof['dbadapter'].'.php';
    $class = self::getDbClass();
    $this->db = new $class($_xhprof);
    $this->db->connect();
  }
  public static function getDbClass()
  {
      global $_xhprof;
      $class = 'Db_'.$_xhprof['dbadapter'];
      return $class;
  }
  public function exec()
  {
    $rs = $this->db->query("select count(*) as cnt from details limit 1;");
    $row = $this->db->getNextAssoc($rs);
    if ($row['cnt'] > $this->cnt) {
      $rs = $this->db->query(sprintf("select * from details order by timestamp desc limit %d,1;", $this->cnt));
      $row = $this->db->getNextAssoc($rs);
      $rs = $this->db->query(sprintf("select count(*) as delcnt from details where timestamp < '%s'", $this->db->escape($row['timestamp'])));
      $delrow = $this->db->getNextAssoc($rs);
      $rs = $this->db->query(sprintf("delete from details where timestamp < '%s'", $this->db->escape($row['timestamp'])));
      echo sprintf("%s timestamp < %s delcnt=%d\n", date('Y-m-d H:i:s'), $row['timestamp'], $delrow['delcnt']);
    }
  }
}
$ crontab -e
1 * * * * /usr/bin/php /var/www/xhgui/xhprof_lib/utils/xhprof_old_del.php > /dev/null 2>&1