プロファイルも標準だけでできる。
import (
"os"
"flag"
"fmt"
"runtime/pprof"
)
// main() の前にこれを 書く
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
// main() の頭にこれを書く
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Println("Error: ", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// ... your main code here ...
}
main() はプロファイルを取るに十分なだけの時間処理を回す(for とか)。
その main をビルドしておく
$ go build main.go
プロファイルを取って、 PDF で出す。
ps2pdf と dot コマンドが必要。
$ brew update
$ brew install gts
$ brew install graphviz
$ brew install ghostscript
$ go tool pprof --pdf main cpu.pprof > callgraph.pdf
見方は、なんとなくしかわかってない。