Categories


Our Customer Success Stories

Repustate has helped organizations worldwide turn their data into actionable insights.

Learn how these insights helped them increase productivity, customer loyalty, and sales revenue.

See all Stories

Table of Contents

There’s an old blog post floating around on the Go Blog with instructions on how to profile your code using pprof - except it’s out of date and might confuse some people. Here’s an up-to-date (as of Go 1.1) way to profile your code.

  1. import runtime/pprof
  2. Where you want the CPU profiling to start, you write:
pprof.StartCPUProfile(f) defer pprof.StopCPUProfile()

This has the effect of starting the profiler and then ending it once the containing function (often your “main” function) exits. The argument you pass to StartCPUProfile, “f”, is an open file descriptor to which the profile information will be written to. You can create this file by writing:

f, err := os.Create("my_profile.file")
  1. OK we have our profiling code setup. Now, build your binary. Let’s say my go file is called “foo.go”, I would run “go build foo.go”. This will create a binary called “foo”.
  2. Run your binary (e.g. ./foo)
  3. Now you run “go tool pprof foo my_profile.file” and you will be in the pprof interactive prompt. Here you can issue commands like top10 or top50 etc. Read up on pprof to find out all the commands you can enter.