$30 off During Our Annual Pro Sale. View Details »

Goのパフォーマンス計測 / go performance measurement

convto
February 20, 2019

Goのパフォーマンス計測 / go performance measurement

go 標準のツールチェインでプロファイル取るときのメモ

convto

February 20, 2019
Tweet

More Decks by convto

Other Decks in Technology

Transcript

  1. Goのパフォーマンス計測
    2019/02/20(水) Makuake LT Party #9

    View Slide

  2. convto
    jisibari
    Twitter: @convto
    Github: convto
    2

    View Slide

  3. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  4. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  5. どういう時に必要?
    - ボトルネック調査
    - コードレビュー
    - その他

    View Slide

  6. どうせツールの導入が大変でしょ...?

    View Slide

  7. 標準のツールチェイン&パッケージでできます
    - testing
    - runtime/pprof
    - runtime

    View Slide

  8. 標準のツールチェイン&パッケージでできます
    - testing
    - runtime/pprof
    - runtime
    Thank you

    View Slide

  9. 除霊

    View Slide

  10. どういう時に必要?
    - ボトルネック調査
    - コードレビュー
    - その他

    View Slide

  11. どういう時に必要?
    - ボトルネック調査
    - コードレビュー
    - その他
    気軽に試せるのでぜひ

    View Slide

  12. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  13. Bench mark test
    - testingパッケージでできる
    - `Benchmark` プレフィックスが対象
    - `$ go test -bench .` で実行できる
    - オプションがいくつかある

    View Slide


  14. View Slide

  15. 実行

    View Slide

  16. 実行
    実行回数 1回あたり実行時間

    View Slide

  17. 実行
    実行回数 1回あたり実行時間 1回あたり使用メモリ
    1回あたりメモリ割り
    当て回数

    View Slide

  18. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  19. runtime/pprofパッケージ
    - runtimeパッケージのwrapperみたいなもの
    - 可視化ツールで想定されている形式で調査結果
    を吐き出す
    - 可視化は `go tool pprof` を利用する

    View Slide


  20. Package pprof writes runtime
    profiling data in the format
    expected by the pprof
    visualization tool.
    https://golang.org/pkg/runtime/pprof/

    View Slide

  21. pprof (with test)
    - benchmark の結果をプロファイルとして出力する
    機能
    - `go test` コマンドに組み込まれている
    - `go test -memprofile mem.out -bench .` のように実
    行する

    View Slide

  22. pprof (with test)
    - benchmark の結果をプロファイルとして出力する
    機能
    - `go test` コマンドに組み込まれている
    - `go test -memprofile mem.out -bench .` のように実
    行する
    テストコードをいじるだけでできます
    ビルドされるコードに影響はでません

    View Slide

  23. 実行

    View Slide

  24. [demo] 吐かれたプロファイルを go tool で
    分析してみる

    View Slide

  25. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  26. pprof (埋め込み)
    - スタンドアロンなプログラムで使う
    - バイナリに埋め込むので配布先でもプロファイル
    が吐ける

    View Slide

  27. ドキュメントで紹介されている例

    View Slide

  28. ドキュメントで紹介されている例
    main関数にこれを埋めるとプロファイル吐け
    るようになる

    View Slide

  29. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  30. pprof (with http)
    - net/httpのサブパッケージ(net/http/pprof)を使う
    - HTTP経由でプロファイルを取得できる
    - testも書いてないし、バイナリに埋め込むのは面
    倒、とかでサクッと試す時におすすめ

    View Slide

  31. こう書く

    View Slide

  32. こうやってプロファイルとる

    View Slide

  33. 対応してるエンドポイント
    - http://localhost:6060/debug/pprof/profile
    - http://localhost:6060/debug/pprof/heap
    - http://localhost:6060/debug/pprof/block
    - http://localhost:6060/debug/pprof/mutex
    - より詳しくはこちら
    https://golang.org/pkg/net/http/pprof/

    View Slide

  34. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  35. runtimeパッケージでもパフォーマンス観測はできる
    - たとえば
    https://golang.org/pkg/runtime/#MemProfile を
    使ったりする
    - が、ドキュメントでも「ほとんどの場合はpprof使っ
    た方がいいよ」とちょいちょい出てくる

    View Slide

  36. runtimeパッケージのメリット
    - https://golang.org/pkg/runtime/#ReadMemStats を使うとstack
    領域のメモリ使用量がわかる
    - pprofはstackは基本的に読めない、heapのみ
    - その他、比較的低レイヤーな処理を生で触れる

    View Slide

  37. runtimeパッケージのメリット
    - https://golang.org/pkg/runtime/#ReadMemStats を使うとstack
    領域のメモリ使用量がわかる
    - pprofはstackは基本的に読めない、heapのみ
    - その他、比較的低レイヤーな処理を生で触れる
    ランタイムレベルの操作をサポートするパッ
    ケージなので、wrapperがあれば
    理由がない限りそっちを使おう

    View Slide

  38. こうやってプロファイルとる

    View Slide

  39. もくじ
    - どういう時に必要?
    - bench mark test
    - pprof (with test)
    - pprof (埋め込み)
    - pprof (with http)
    - runtime
    - まとめ

    View Slide

  40. まとめ
    - Goは標準でいろいろ覗けるからよい
    - pprofは色々な方法で使える
    - runtimeでの計測は基本しなくていい

    View Slide

  41. - プレゼンテーションテーマは SlidesCarnival の ヨークプレゼンテーションテンプレー
    ト を利用しています
    クレジット表記

    View Slide

  42. ご静聴ありがとう
    ございました

    View Slide