LoginSignup
8
8

More than 5 years have passed since last update.

Phalcon で xhprof を使う

Posted at

インストール

phalcon は入れてる前提.

また,自分は VirtualHost で /var/www 以下などにドメイン名でアプリを入れて ワイルドカードで直接そこを参照するようにする設定を行っているのでその前提でやる.

例えば下記のような設定とか.

<VirtualHost *:80>
    ServerName exmple.com
    ServerAlias *.example.com
    Options -Indexes +FollowSymLinks
    VirtualDocumentRoot "/var/www/%0/web"
</VirtualHost>

xhprof を入れる

apache 経由で参照できる場所に設置.ここでは前述のとおり /var/www 以下に置く. phpize がどうやって入るかはまかせた.


$ git clone https://github.com/facebook/xhprof xhprof.example.com
$ cd xhprof
$ phpize
$ make
$ sudo make install      # 環境による
$ ln -s xhprof_html web  # VirtualDocumentRoot が web 以下だから

phalcon プロジェクトに適用する

例えば phalcon/devtool で modules で作った場合の public/prof.php に下記のように書く.ココらへんは上で設置した xhprof の doc のページに書いてあるものを取ってきて貼り付けて動くようにしたもの.

<?php

use Phalcon\Mvc\Application;

error_reporting(E_ALL);

try {

    $PROF_URL = 'http://xhprof.example.com/';
    $XHPROF_ROOT = '/var/www/xhprof.example.com/';

    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

    /**
     * Include services
     */
    require __DIR__ . '/../config/services.php';

    /**
     * Handle the request
     */
    $application = new Application();

    /**
     * Assign the DI
     */
    $application->setDI($di);

    /**
     * Include modules
     */
    require __DIR__ . '/../config/modules.php';

    echo $application->handle()->getContent();

    // xhprof
    $xhprof_data = xhprof_disable();

    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();

    $entryId = mt_rand();

    $run_id = $xhprof_runs->save_run($xhprof_data, $entryId);
    echo '<pre>'.htmlspecialchars("---------------\n".
            "Assuming you have set up the http based UI for \n".
            "XHProf at some address, you can view run at \n".
            $PROF_URL."index.php?run=$run_id&source=$entryId\n".
            "---------------\n").'</pre>';
} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e) {
    echo $e->getMessage();
}

動作確認

上記のようにするとアクセスした時に最後に下記の様に表示される.

---------------
Assuming you have set up the http based UI for 
XHProf at some address, you can view run at 
http://xhprof.example.com/index.php?run=53837aa5adf00&source=1372228594
---------------

その URL にアクセスしに行くと下記の票に 時間やメモリ消費量などが表示される. 4ms 早いですね.

screenshot 2014-05-27 2.33.31.png

graphvis を入れてると下記のようにコールグラフも見られる.赤くなってるのは全体に対する割合が大きいからだけどそもそも実行時間とかが小さいのであんまり意味ない.

screenshot 2014-05-27 2.35.24.png

これでさらにパフォーマンス・チューニングがはかどりますね.

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8