LoginSignup
7
7

More than 5 years have passed since last update.

Go でプロファイリングして PDF で見る

Posted at

プロファイルも標準だけでできる。

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

見方は、なんとなくしかわかってない。

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