Karan Sharma

Ramblings on tech, cycling, and finance

Profile Golang Applications

Creating a profile

To profile Golang applications, pprof package is widely used. There's a nice wrapper around it https://github.com/pkg/profile which allows you to just drop one line in your application and start generating profiles:


package main

func main() {

  defer profile.Start(profile.ProfilePath("."), profile.NoShutdownHook).Stop()

  // rest of your application
}

Visualizing

go tool pprof -http=":8000" ./cpu.pprof

This allows you to visualize the pprof file in Graphviz and the more helpful Flamegraph visualizations.