Add the following code to generate a CPU profile:

import (
  "runtime/pprof"
)

func main() {
  var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
  flag.Parse()

  if *cpuprofile != "" {
    prof, err := os.Create()
    if err != nil {
      panic(err)
    }
    pprof.StartCPUProfile(prof)
    defer pprof.StopCPUProfile()
  }
}

Run your program with -cpuprofile=<file>:

$ go build foo.go
$ ./foo -cpuprofile=foo.prof

Load your profile in pprof:

$ go tool pprof foo foo.prof

Then:

  • Type kcachegrind to display the profile in KCacheGrind.
  • (Provided you have installed GraphViz and have dot in your PATH) png to generate a PNG graph.
  • Call for help to display rest of the commands.

Linkography