2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

APDでPHP5.4のスクリプトをプロファイルする

Posted at

最近PHPスクリプトから時間のボトルネックを特定するようなことをやってましたので、その経過をメモしておきます。

関係なソフトのバージョン

  • Ubuntu 14.04
  • Apache 2.2
  • PHP 5.4
  • APD 1.0.1 (プロファイルのためのphp extension)

ビルド手順

  1. ビルドに必要なパッケージ

    sudo apt-get install build-essential
    sudo apt-get build-dep apache2 php5

  2. ビルド用スクリプト

  • gistからスクリプトを保存して、1回実行する
  • 各プログラムはスクリプトがあるフォルダにインストールされるため、特別な権限はいりません

APDをPHPインタプリタにロードさせる

php.iniの最後に、こんな行を1つ追加する (apd.soの正確なパスはスクリプトパスに依存するので、`find -name "apd.so"で探せます)

zend_extension=/home/xxx/yyy/local/zzz/apd.so

プロフィールデータの出力、分析

  1. トレースデータの出力を有効する: 下記コードをPHPスクリプトの最初に入れる。

    apd_set_pprof_trace('.');
    

    (トレースデータは pprof.22141.0のようなファイルに出力されます)

  2. PHPスクリプトを実行する

    $ xxx/local/bin/php code-to-profile.php
    

    注意点:

    • トレースデータを出力する時、実行が遅くなります (時間が2~3倍程度)
    • トレースデータファイルがかなり大きい:1時間実行すると数GB程度になります。
  3. トレースデータから、各関数の実行時間を分析する

    $pprofp -R /tmp/pprof.22141.0
    

参照:

apd examples

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?