日頃の行い

個人的な日頃の行いをつらつら書いてます\\\\ ٩( 'ω' )و ////

PHPのプロファイリングが出来るblackfireを試してみた

またもやISUCONで使おうと思ってblackfireをいうものを試してみました。
有料な機能も結構ありますが、
無料で使える機能でもどのメソッドがどれくらいの回数呼ばれてるのかとか、
どれくらいの割合を使っているのかとかがわかって便利でした。

blackfire.io

今回検証に利用したコードはここに置きました。

github.com

blackfireに既に登録済みで、server id, server tokenが手に入っていて、
docker-composeとGNU makeが入ってれば make compose id={server id} token={server token} で動かせるはずです。

やること

  • blackfireに登録してserver id, server tokenを取得
  • サーバにblackfire agentのインストール
  • blackfireのchrome extensionをインストール
  • プロファイリングする

という感じです。

1. blackfireに登録してserver id, server tokenを取得

しましょう!
Login - Blackfire

2. サーバにblackfire agentのインストール

この辺をやるだけです。

Installation - Blackfire

Dockerで作成した時のログはこんな感じでした。

# Configuring the Debian Repository

Step 4/10 : RUN wget -O - https://packagecloud.io/gpg.key | apt-key add -
 ---> Running in ee7d25eb98d1
converted 'https://packagecloud.io/gpg.key' (ANSI_X3.4-1968) -> 'https://packagecloud.io/gpg.key' (UTF-8)
--2017-10-15 16:41:40--  https://packagecloud.io/gpg.key
Resolving packagecloud.io (packagecloud.io)... 50.97.198.58, 2607:f0d0:2101:270::2
Connecting to packagecloud.io (packagecloud.io)|50.97.198.58|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3889 (3.8K) [application/octet-stream]
Saving to: 'STDOUT'

     0K ...                                                   100% 53.7M=0s

2017-10-15 16:41:40 (53.7 MB/s) - written to stdout [3889/3889]

OK
 ---> 032abc5bc6fb
Removing intermediate container ee7d25eb98d1
Step 5/10 : RUN echo "deb http://packages.blackfire.io/debian any main" | tee /etc/apt/sources.list.d/blackfire.list
 ---> Running in be9b5a302725
deb http://packages.blackfire.io/debian any main
 ---> 0437a3abc6ce
Removing intermediate container be9b5a302725
Step 6/10 : RUN apt-get update
 ---> Running in 44f15f9f8197
Hit http://security.debian.org jessie/updates InRelease
Ign http://deb.debian.org jessie InRelease
Get:1 http://security.debian.org jessie/updates/main amd64 Packages [547 kB]
Hit http://deb.debian.org jessie-updates InRelease
Get:2 http://packages.blackfire.io any InRelease [23.2 kB]
Hit http://deb.debian.org jessie Release.gpg
Get:3 http://deb.debian.org jessie-updates/main amd64 Packages [23.1 kB]
Hit http://deb.debian.org jessie Release
Get:4 http://deb.debian.org jessie/main amd64 Packages [9063 kB]
Get:5 http://packages.blackfire.io any/main amd64 Packages [1138 B]
Fetched 9658 kB in 4s (2006 kB/s)
Reading package lists...
 ---> 7bb05b5d43cc
Removing intermediate container 44f15f9f8197

# Configuring the Debian Repository -- ここまで
# Installing the Agent

Step 7/10 : RUN apt-get install -y blackfire-agent
 ---> Running in 792b428f7e45
Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
  blackfire-agent
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 6004 kB of archives.
After this operation, 16.4 MB of additional disk space will be used.
Get:1 http://packages.blackfire.io/debian/ any/main blackfire-agent amd64 1.14.1 [6004 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 6004 kB in 22s (263 kB/s)
Selecting previously unselected package blackfire-agent.
(Reading database ... 12926 files and directories currently installed.)
Preparing to unpack .../blackfire-agent_1.14.1_amd64.deb ...
Unpacking blackfire-agent (1.14.1) ...
Processing triggers for systemd (215-17+deb8u7) ...
Setting up blackfire-agent (1.14.1) ...
creating blackfire system user


    ADDITIONAL STEP REQUIRED!

    If this is a new install please run:

    sudo blackfire-agent --register
    sudo /etc/init.d/blackfire-agent start

 ---> 3129728f20d6
Removing intermediate container 792b428f7e45

# sudo blackfire-agent -register
# 上記コマンドを実行するとインタラクティブにserver id, server tokenを設定してくれます。
# しかし、最終的には /etc/blackfire/agent に書き込まれるのでここではserver id, tokenを設定したファイルを置いています。

Step 8/10 : COPY ./blackfire.ini /etc/blackfire/agent
 ---> fb9e504cb609
Step 9/10 : RUN /etc/init.d/blackfire-agent restart
 ---> Running in dea658865681
Restarting Blackfire Agent: blackfire-agent.
 ---> 58a923e72362
Removing intermediate container dea658865681

# Installing the Agent  -- ここまで
# Installing the Blackfire CLI tool は飛ばします
# Installing the PHP Probe

Step 10/10 : RUN apt-get install blackfire-php
 ---> Running in 1dc806414984
Reading package lists...
Building dependency tree...
Reading state information...
Suggested packages:
  php5-common php5-cli php7.1-cli php7.1-common
The following NEW packages will be installed:
  blackfire-php
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 2006 kB of archives.
After this operation, 5532 kB of additional disk space will be used.
Get:1 http://packages.blackfire.io/debian/ any/main blackfire-php amd64 1.18.0 [2006 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 2006 kB in 9s (214 kB/s)
Selecting previously unselected package blackfire-php.
(Reading database ... 12938 files and directories currently installed.)
Preparing to unpack .../blackfire-php_1.18.0_amd64.deb ...
Unpacking blackfire-php (1.18.0) ...
Setting up blackfire-php (1.18.0) ...
blackfire-php install completed.

# Installing the PHP Probe -- ここまで
# これをやれば php.ini 周りの設定にblackfireの設定が追加されます。
# なので php -i | grep blackfireをやった時にblackfireが設定されていることがわかるはずです。
# されていない場合は別のバージョンのphpにインストールされている可能性があるので
# extension=blackfire.so とか設定に追加すればもしかしたら動くかも・・・w

$ php -i |grep blackfire
Additional .ini files parsed => /usr/local/etc/php/conf.d/zz-blackfire.ini
    with blackfire v1.18.0~linux-x64-non_zts71, https://blackfire.io, by SensioLabs
blackfire
blackfire.agent_socket => unix:///var/run/blackfire/agent.sock => unix:///var/run/blackfire/agent.sock
blackfire.agent_timeout => 0.25 => 0.25
blackfire.env_id => no value => no value
blackfire.env_token => no value => no value
blackfire.log_file => no value => no value
blackfire.log_level => 1 => 1
blackfire.server_id => no value => no value
blackfire.server_token => no value => no value

3. blackfireのchrome extensionをインストール

blackfire cliを使えばなんか出来るっぽいですが、
とりあえずchrome拡張を入れて動かした感じです。

Google Chrome - Blackfire

4. プロファイリングする

プロファイリングしたいページに行って、
chrome拡張を利用するとこんな感じになります。

f:id:arata3da4:20171016021409p:plain

f:id:arata3da4:20171016021418p:plain

f:id:arata3da4:20171016021427p:plain

そして View Call Graph を押すと下記のようなページが出るはずです。
なんか便利そう!

f:id:arata3da4:20171016021538p:plain

感想

  • なんか便利そうなので仕事でも使っていこうかなと思いました(小並感