LoginSignup
4
5

More than 5 years have passed since last update.

Rust製プロファイリングツールpy-spyを試す

Last updated at Posted at 2018-09-08

py-spyとは?

benfred/py-spy: Sampling profiler for Python programs

  • Rust製のpythonプロファイリング・ツール
  • 対応しているpythonのバージョンは2.3-2.73.3-3.7 と幅広い

Install

自分はRustユーザーではないのでpip経由でインストールします。

$ pip3 install py-spy

Flask

Flaskで次のようなエントリーポイントを作成してアプリケーションを立ち上げる。

@app.route('/name/<name>.json')
def hello_world(name):
    greet = "Hello %s from flask!" % name
    result = {
        "ResultSet": {
            "Result": {
                "Greeting": greet
            }
        }
    }

    response = Response(json.dumps(result))
    response.headers['Content-Type'] = "application/json"
    response.headers['Last-Modified'] = \
        "Last-Modified: Wed, 21 Jun 2012 07:00:25 GMT"
    return response

$ python3.6 server.py

次に別のターミナルを立ち上げてpy-spyを実行する。

$ py-spy -h
py-spy 0.1.4
A sampling profiler for Python programs

USAGE:
    py-spy [FLAGS] [OPTIONS] --pid <pid> [python_program]...

FLAGS:
    -d, --dump        Dump the current stack traces to stdout
    -F, --function    Aggregate samples by function name instead of by line number
    -h, --help        Prints help information
    -V, --version     Prints version information

OPTIONS:
    -d, --duration <duration>    The number of seconds to sample for when generating a flame graph [default: 2]
    -f, --flame <flamefile>      Generate a flame graph and write to a file
    -p, --pid <pid>              PID of a running python program to spy on
    -r, --rate <rate>            The number of samples to collect per second [default: 1000]

ARGS:
    <python_program>...    commandline of a python program to run
# 20秒間で秒間1万個サンプリングをとって結果をprofile.svgとして出力する。
$ sudo py-spy --flame profile.svg -d 20 -r 10000 -p 77170
Sampling process 10000 times a second for 20 seconds
███████████████████████████████████████████████████████████████████████████████████████████████████████████████████ 200000/200000
Wrote flame graph 'profile.svg'. Samples: 200000 Errors: 0

py-spyからアプリを立ち上げる方法について

py-spy からアプリを起動する方法も紹介されているが
py-spyが終了する時にサーバープロセスが残り続ける問題があった。

$ sudo py-spy --flame profile.svg -d 20 -r 10000 \
  -- python3.6 server.py

Request

py-spyを立ち上げたらさらに別のターミナルからabコマンドでサーバーにリクエストを送ってプロファイリングを取得する。
プロファイリングの時間が20秒なので5万リクエスト程度でプロファイリング中はずっとリクエストを受けている状態になる。

$ ab -n 50000 "http://127.0.0.1:8888/name/john.json"

SVGでの結果の確認

生成されたprofile.svgをブラウザで開けば結果を確認出来る。

detail_2018-09-08 13.51.34.jpg

svgの右上に検索機能があるのでそこに今回実装したエントリーポイントの関数名hello_worldを入力するとそこがピンク色にハイライトされる。

prompt_2018-09-08 14.51.45.jpg

search_2018-09-08 13.55.23.jpg

プロファイリングの結果から今回実装したアプリケーションは非常にシンプルなもののためアプリケーション全体としては全く時間を占めていないことが分かった。

まとめ

py-spyは手軽に導入出来て幅広いpythonのバージョンに対応していることから
2.Xから3.xへの移行時などにパフォーマンス・レグレッションが起きたりしていないか確認などに使えそうという感想が得られた。

4
5
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
4
5