Sunday, April 1, 2012

Real time profiling with PProf

Profiling Go Programs introduced how to profile a go program in a generic way. With the release of go 1, the profiling has become much easier: you can register various profile handlers to the running http server, and profile the program when it's running. The trick is very easy:

link the pprof http handler by adding the following line into your program(usually the main file):

      import _ "net/http/pprof"

Then you can easier grab head profile(understand how the memory is consumed) by using the commands below:

   go tool pprof http://localhost:6060/debug/pprof/heap

Or to look at a 30-second CPU profile:

      go tool pprof http://localhost:6060/debug/pprof/profile

Or to view all available profiles:

      go tool pprof http://localhost:6060/debug/pprof/

For more information on how the understand the pprof output, you can go to Profiling Go Programs or the homepage of google perf tools.

BTW: since there pprof handlers are so convenient, it is generally a good practice to start a debug http server with pprof enabled even if the program is not going to serve any http traffic.