$shibayu36->blog;

クラスター株式会社のソフトウェアエンジニアです。エンジニアリングや読書などについて書いています。

Action Graphを使ってGoのpackageごとのビルド時間を可視化する

前やったことあるがブログに書いてなかったのでメモしておく。

まずGoのビルド時間についてはAnalyzing Go Build Times | howardjohn's blogが非常に分かりやすく参考になる。この中でAction Graphというものに言及があり、これを使うことでパッケージごとのビルド時間を可視化できる。

例えば自分のgo_todo_appというものを使ってみる。 まずgo buildでactiongraph.jsonを吐き出し

$ go build -debug-actiongraph=actiongraph.json ./...

その後時間がかかっている順にランキングを出せる。今回のレポジトリは非常に小さいのでruntime/cgoのビルドが一番重そうだ。

$ actiongraph -f ./actiongraph.json top
  0.956s   6.88%  build runtime/cgo
  0.768s  12.41%  build runtime
  0.560s  16.44%  build net
  0.473s  19.85%  build net/http
  0.401s  22.74%  build debug/dwarf
  0.378s  25.45%  build math/big
  0.354s  28.01%  build vendor/golang.org/x/text/unicode/norm
  0.318s  30.30%  build gopkg.in/yaml.v3
  0.289s  32.38%  build golang.org/x/net/html
  0.275s  34.36%  build github.com/google/go-cmp/cmp
  0.258s  36.22%  link  github.com/shibayu36/go_todo_app
  0.250s  38.01%  build encoding/xml
  0.243s  39.77%  build crypto/tls
  0.239s  41.48%  build encoding/json
  0.222s  43.08%  build golang.org/x/text/internal/language
  0.205s  44.55%  build reflect
  0.190s  45.92%  build testing
  0.186s  47.26%  build database/sql
  0.166s  48.45%  build github.com/jmoiron/sqlx
  0.161s  49.61%  build github.com/go-playground/validator/v10

また、treeを使うことでnestされたpackageのどれに時間がかかっているかを可視化したり

$ actiongraph -f ./actiongraph.json tree -L 2
 13.634s          (root)
 10.935s            std
  1.860s   0.768s     std/runtime
  1.644s   0.012s     std/crypto
  1.293s   0.560s     std/net
  1.037s              std/vendor
  0.861s              std/internal
  0.737s   0.018s     std/encoding
  0.550s   0.089s     std/math
  0.459s              std/debug
  0.245s              std/database
  ...
  1.682s            github.com
  0.357s              github.com/google
  0.261s              github.com/go-playground
  0.197s              github.com/gabriel-vasile
  0.189s              github.com/jmoiron
  0.179s              github.com/stretchr
  0.140s              github.com/shibayu36
  0.103s              github.com/go-sql-driver
  0.068s              github.com/davecgh
  0.057s              github.com/caarlos0
  0.048s              github.com/pmezard
  0.043s              github.com/leodido
  0.040s              github.com/go-chi
  0.699s            golang.org
  0.699s              golang.org/x
  0.318s            gopkg.in
  0.318s   0.318s     gopkg.in/yaml.v3

actiongraphのREADMEに書かれているように、あるパッケージがどこから依存されているかの可視化もできる。

便利ですね。

参考