Introduction
The Go runtime has built in support for several types of profiling that can be used to inspect the performance of your programs. A common way to leverage this support is via the testing
package, but if you want to profile a full application it is sometimes complicated to configure the various profiling mechanisms correctly.
I wrote profile to scratch my own itch and create a simple way to profile an existing Go program without having to restructure it as a benchmark.
Installation
profile is go get
able so installation is a simple as
go get github.com/davecheney/profile
Usage
Enabling profiling in your application is as simple as one line at the top of your main
function
import "github.com/davecheney/profile" func main() { defer profile.Start(profile.CPUProfile).Stop() ... }
Options
What to profile is controlled by the *profile.Config
value passed to profile.Start
. A nil
*profile.Config
is the same as choosing all the defaults. By default no profiles are enabled.
In this more complicated example a *profile.Config
is constructed by hand which enables memory profiling, but disables the shutdown hook.
import "github.com/davecheney/profile" func main() { cfg := profile.Config { MemProfile: true, NoShutdownHook: true, // do not hook SIGINT } // p.Stop() must be called before the program exits to // ensure profiling information is written to disk. p := profile.Start(&cfg) ... }
Several convenience variables are provided for cpu, memory, and block (contention) profiling.
For more complex options, consult the documentation on the profile.Config
type. Enabling more than one profile may cause your results to be less reliable as profiling itself is not without overhead.
Example
To show profile in action, I modified cmd/godoc
following the instructions in the first example.
% godoc -http=:8080 2013/07/07 15:29:11 profile: cpu profiling enabled, /tmp/profile002803/cpu.pprof
In another window I visited http://localhost:8080
a few times to have some profiling data to record, then stopped godoc
.
^C2013/07/07 15:29:33 profile: caught interrupt, stopping profiles % go tool pprof $(which godoc) /tmp/profile002803/cpu.pprof Welcome to pprof! For help, type 'help'. (pprof) top10 Total: 15 samples 2 13.3% 13.3% 2 13.3% go/scanner.(*Scanner).next 2 13.3% 26.7% 2 13.3% path.Clean 1 6.7% 33.3% 3 20.0% go/scanner.(*Scanner).Scan 1 6.7% 40.0% 1 6.7% main.hasPathPrefix 1 6.7% 46.7% 3 20.0% main.mountedFS.translate 1 6.7% 53.3% 1 6.7% path.Dir 1 6.7% 60.0% 1 6.7% path/filepath.(*lazybuf).append 1 6.7% 66.7% 1 6.7% runtime.findfunc 1 6.7% 73.3% 2 13.3% runtime.makeslice 1 6.7% 80.0% 2 13.3% runtime.mallocgc
Note In the example above we’re passing the godoc
binary and the profile produced by running that binary to go tool pprof
. When profiling your own code, you must pass your binary, and its profile to go tool pprof
otherwise the profile will not make sense.
go tool pprof server /var/folders/jr/hvhybrvn0j54rrn6mygq2ldh0000gn/T/profile121190984/cpu.pprof