Profiling Golang

Concise guide to profiling Go programs

TJ Holowaychuk
2 min readJul 9, 2014

There are plenty of guides for profiling Golang, just writing this here so I can find it again easily ☺

The runtime/pprof package offers lower level control over creating profiles, and the very cool net/http/pprof package registers HTTP end-points for profiling live applications, this is a really slick feature.

To simplify things even further Dave Cheney created github.com/davecheney/profile, all you need in your main is:

defer profile.Start(profile.CPUProfile).Stop()

Next up you should build your program with go build, run the program and you’ll see that “profile” has created a temporary file for you. All you need to do now is run the pprof tool against the binary:

$ go build
$ ./myprogram
$ go tool pprof —text ./myprogram /var/folders/jm/j__b36sn04n5__scnnw6bpzr0000gn/T/profile152611925/cpu.pprof
Total: 492 samples
411 83.5% 83.5% 411 83.5% runtime.mach_semaphore_signal
11 2.2% 85.8% 431 87.6% runtime.mallocgc
9 1.8% 87.6% 33 6.7% github.com/mreiferson/go-ujson.(*Decoder).decodeString
8 1.6% 89.2% 8 1.6% sweepspan
7 1.4% 90.7% 7 1.4% scanblock
5 1.0% 91.7% 427 86.8% hash_insert
4 0.8% 92.5% 24 4.9% github.com/mreiferson/go-ujson.simpleStore.NewString·i
3 0.6% 93.1% 8 1.6% copyin
3 0.6% 93.7% 3 0.6% runtime.aeshashbody
3 0.6% 94.3% 3 0.6% runtime.nanotime

Alternatively you can generate other kinds of files, for example a PDF call-graph:

$ go tool pprof —pdf ./myprogram /var/folders/jm/j__b36sn04n5__scnnw6bpzr0000gn/T/profile152611925/cpu.pprof > out.pdf

--

--