Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今日から始めるpprof / Pprof workshop for beginners

Avatar for ymotongpoo ymotongpoo
September 25, 2025

今日から始めるpprof / Pprof workshop for beginners

Go Conference 2025のワークショップ資料です
https://gocon.jp/2025/workshops/1018925/

Avatar for ymotongpoo

ymotongpoo

September 25, 2025
Tweet

More Decks by ymotongpoo

Other Decks in Technology

Transcript

  1. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 1 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 今⽇から始めるpprof Yoshi Yamaguchi (@ymotongpoo) G O C O N F E R E N C E 2 0 2 5 D A Y 1 Senior Developer Advocate Amazon Web Services Japan, G.K.
  2. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 2 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⾃⼰紹介 ⼭⼝ 能迪(やまぐち よしふみ) アマゾンウェブサービスジャパン合同会社 シニアデベロッパーアドボケイト 専⾨領域 • オブザーバビリティ • SRE全般 @ymotongpoo
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 3 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. チューター紹介 鈴⽊ 悠⼤(すずき ゆうだい) 株式会社HRBrain 執⾏役員CTO 2018年にHRBrainに10⼈⽬の社員、3⼈⽬のエンジニアとして⼊社。 Goは2015年から10年書いています。 @yudppp
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 4 今⽇のワークショップ⽤コード https://github.com/ymotongpoo/20250927-gocon-workshop
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 5 もくじ 1. プロファイルとは 2. pprof の基本機能の説明 3. pprof をプログラムに組み込む 4. pprof でプロファイルを取得する 5. pprof でプロファイルを可視化する 6. pprof の読み⽅を理解する 7. pprof の結果に基づいて改善する 8. 改善結果を pprof で確認する 9. (ボーナス)実際の開発に活かせる継続的プロファイルの解説
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 6 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. プロファイルとは
  7. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 7 プロファイル profile とは Goでは通常パフォーマンスプロファイルのことを指す • プログラムのどこが遅いかを調査するための⼿法 • 統計的に特定の数値(例: CPU使⽤率、メモリ使⽤率、スレッド数)などのコー ルスタック(関数)レベルで取得する • 有名なツールには Java Flight Recorder (Java)、cProfile (Python)、perf (Linux) などがある
  8. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 8 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. pprof の基本機能
  9. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 9 pprof のGoプロジェクト内での扱い • 元々は⾔語間共通のプロファイルデータの可視化ツール • Go⾔語にはサンプリングツールも含めて標準ツールとして同梱 • src/runtime/pprof: サンプリングツール • src/cmd/pprof: github.com/google/pprof のコピー つまり • pprofパッケージ: サンプリングツール • go tool pprof: ビジュアライザー
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 10 pprof の基本機能 • プログラムからのプロファイルデータの取得 • プロファイルデータの可視化 • Top • Graph • Flame Graph • Peek • Source • Disassemble
  11. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 11 演習1: まずは起動してみる git clone https://github.com/ymotongpoo/20250927-gocon-workshop.git cd 20250927-gocon-workshop cd sample go tool pprof -http :9999 cpu.sample.prof 次のコマンドを実⾏して、ブラウザで http://localhost:9999 にアクセスし、 UIを確認してみましょう
  12. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 12 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. pprof をプログラムに組み込む(計装)
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 13 実装をしないとプロファイルは取得できない pprof を使うためにはプロファイルの取得を宣⾔しないといけない • runtime/pprof: CLIやバッチツールなどで利⽤ • net/http/pprof: HTTPサーバーのプロファイルを取りたい場合に利⽤
  14. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 14 プログラムが終了するものは runtime/pprof 今回は CLI ツールの改善を演習で⾏うので runtime/pprof を使う
  15. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 15 ウェブサーバーには net/http/pprof net/http/pprof の場合は基本的には空 import をするだけ
  16. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 16 演習2: 計装してみよう sample/main.go を参考に exercise/cut.go に pprof を計装してみよう
  17. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 17 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. pprof でプロファイルを取得
  18. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 18 計装されていれば取得は簡単 runtime/pprof の場合は計装したプログラムを実⾏するだけ cpu.prof がプロファイル
  19. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 19 ウェブサーバーは外部から取得 net/http/pprof の場合は設定されたエンドポイントにHTTP GETアクセス • /debug/pprof/profile: CPU使⽤量 • /debug/pprof/heap: ヒープサイズ • /debug/pprof/block: goroutineブロック • /debug/pprof/mutex: mutexロックの保持 • /debug/pprof/trace: トレース
  20. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 20 演習3: プロファイルを取得してみよう exercise/cut.go は GNU Coreutils の cut(1) のGoポート(不完全)です。 これをビルド、実⾏し、プロファイルが⽣成されたか確認しよう cd exercise go build ./exercise sample10k.csv ls # cpu.prof(⾃分で名前をつけた場合はそのファイル)があれば成功
  21. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 21 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. pprof でプロファイルを可視化
  22. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 22 pprof でプロファイルを可視化する $ go tool pprof –http :9999 path/to/profile.data するとローカルでウェブサーバーが⽴ち上がってウェブUIが起動する
  23. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 23 ウェブUIを使わない⽅法もある デフォルトはターミナルでのインタラクティブモードになっている $ go tool pprof cpu.prof File: cut1 Build ID: caaa6856cbec8315b44f8b87d39b371abe34ef1f Type: cpu Time: 2025-09-08 08:39:44 UTC Duration: 1.80s, Total samples = 1.61s (89.21%) Entering interactive mode (type "help" for commands, "o" for options) (pprof)
  24. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 24 ウェブUIの操作⽅法 ビューの切り替え 表⽰データの切り替え フィルタリング
  25. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 25 演習4: ⾃分で取得したプロファイルを可視化しよう 演習3で取得したプロファイルを使ってpprofを起動します cd exercise go tool pprof -http :9999 cpu.prof
  26. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 26 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. pprof の読み⽅を理解する
  27. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 27 Flame Graph フレイムグラフ • たぶん1番使う。プロファイルと⾔ったらこれ、というくらい浸透した • 元々は Brendan Gregg が作ったツールで広まった
  28. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 28 Flame Graph フレイムグラフ 覚えること: ⻑い = リソース使っている = 悪い
  29. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 29 Top トップ Linux コマンドの top と同じ。スタックをリソース消費順に表形式で表⽰。 Cum Flat
  30. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 30 Graph グラフ
  31. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 31 Graph グラフ • 箱が⼤きいほうがリソース消費が⼤きい • ⽐較の場合、緑〜灰⾊〜⾚でグラデーションでマイナス→0→プラス
  32. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 32 Source ソース ⾏レベルでのリソース消費を表⽰(Goアセンブリにも対応)
  33. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 33 演習5: pprof の UI に慣れよう 演習4で起動したウェブUIを操作して各ビューを確認しよう • フレイムグラフ • グラフ • ソース • トップ
  34. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 34 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. pprof の結果に基づいて改善する
  35. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 35 改善にあたっての結果の⾒⽅ • 基本⾃分が書いたものに原因がある • 標準パッケージや著名なパッケージはまずパフォーマンスは良いものと考えておく • Flat が遅い場合にはアルゴリズムに問題がある • Cum が遅い場合には外部関数の呼び出し⽅に問題がある • Flame Graph、Graph でボトルネックの関数の⾒当をつけて、Source で深堀りする
  36. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 36 演習6: pprof を使って改善案を出してみよう exercise/cut.go の実装には改善できる点が複数あります。 pprof を使って次の内容を記述してください。 1. cut.go の中にある改善が必要な部分の指摘 2. 1を踏まえての改善案
  37. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 37 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 改善結果を pprof で確認する
  38. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 38 常に計測する • 改善結果は計測によってのみ判断される • XXXミリ秒処理が速くなった • X% ヒープサイズが⼩さくなった • 改善結果を客観的に共有するには図表が効果的
  39. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 39 pprof で変化を確認する pprof には差分を確認するためのオプションがある $ go tool pprof -http :9999 -base cpu.prof cpu.prof.2
  40. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 40 差分表⽰を確認 グラフ トップ
  41. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 41 演習7: pprof を使って改善結果を確認しよう 演習6で確認した exercise/cut.go への改善案を実施して、改善したプログラム でプロファイルを取得し、元のバージョンと⽐較してみましょう。 プロファイルは異なる名前で保存することに注意してください。 どのような結果が読み取れるか説明してください。
  42. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 42 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. テストの⼀環としてのプロファイル
  43. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 43 「ビジネスロジックに計装をしたくない」 go test -bench がプロファイルも出せるのでベンチマークを書きましょう
  44. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 44 CIでベンチマークとプロファイルの両⽅を取得 $ go test -bench=. -cpuprofile=cpu.prof goos: linux goarch: amd64 pkg: github.com/ymotongpoo/20250927-gocon-workshop/exercise cpu: Intel(R) Core(TM) i5-7260U CPU @ 2.20GHz BenchmarkCut-4 650418 1805 ns/op ok github.com/ymotongpoo/20250927-gocon-workshop/exercise 1.711s ヒープを取得したい場合には -memprofile オプションを付けます go test -bench=. -memprofile=mem.prof
  45. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 45 演習8: プロファイルをベンチマークで取得しよう プロファイルの計装を削除し、ベンチマークを取得したい部分を関数として切 り出して、 go test -bench を利⽤してプロファイルを取得してください 取得したプロファイルが先ほどと同様に go tool pprof で可視化できること を確認してください
  46. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 46 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. 実際の開発に活かせる 継続的プロファイルの解説
  47. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 47 しかしCIはあくまでテスト実⾏時 本番環境で動作しているもののプロファイルも継続的に取得したい 継続的プロファイルを⾏うことで可能に
  48. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 48 継続的プロファイル CNCFのオブザーバビリティ⽩書にも重要なテレメトリーとしてプロファイルを挙げ ている • ログ • メトリクス • トレース • プロファイル • ダンプ ここで⾔うプロファイルは「継続的プロファイル」のこと
  49. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 49 なぜ継続的プロファイルなのか 常時稼働のサービスの場合パフォーマンスの問題は突然やってくる CPU使⽤率 ここで気がついたとしても ここでしか取れない
  50. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 50 なぜ継続的プロファイルなのか それにそなえて定期的にプロファイルを取得しておく CPU使⽤率
  51. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 51 継続的プロファイルの⾃作 1/2 pprof では複数の同⼀タイプのプロファイルをマージして表⽰する機能がある go tool pprof -http :9999 cpu.prof.1 cpu.prof.2 CPU プロファイルとヒーププロファイルを混ぜるといったことはできない
  52. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 52 継続的プロファイルの⾃作 2/2 1. net/http/pprof で公開したエンドポイントに定期的にアクセスして保存 2. メトリクスを⾒てパフォーマンス劣化が認められる時間帯のプロファイルをまと めて評価
  53. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 53 継続的プロファイルのサービスはたくさんある https://www.cncf.io/blog/2022/05/31/what-is-continuous-profiling/
  54. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 54 Pyroscopeの例 2つのピークが⾒られる • 14:25 くらい • 14:50〜15:30
  55. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 55 Pyroscopeの例 プロファイルを取り続けているので 特定の時間帯のプロファイルを確認可能
  56. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 56 演習9: 継続的プロファイルのサービスを使ってみる 利⽤しているオブザーバビリティバックエンドが継続的プロファイルに対応してい る場合に、⾃分のプログラムを対応させてみましょう。 対応していない場合は OSS の継続的プロファイルを試してみましょう。 例: Pyroscope の場合 docker run -it –p 4040:4040 grafana/pyroscope
  57. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. 57 Thank you! © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Yoshi Yamaguchi @ymotongpoo Any questions?