Published Aug 03 2017
The Go ecosystem provides a very easy way to profile your applications.
I’ll explain profiling using a package by Dave Cheney which makes programs very easy to debug, by adding a one-liner to our main()
.
All you need to get started is follow these X easy steps.
github.com/pkg/profile
Can’t be simpler than running
go get github.com/pkg/profile
and you’re done.
main()
function of your commandpackage main
import (
//...
"github.com/pkg/profile"
)
func main() {
// CPU profiling by default
defer profile.Start().Stop()
//...
}
This will generate a *.pprof
file in a temp folder, and tell you where it’s located (will be needed later)
2017/08/03 14:26:28 profile: cpu profiling enabled, /var/...../cpu.pprof
graphviz
if you don’t have it installed yetThis is used to generate the graph on a pdf.
On a Mac, it’s a simple brew install graphviz
. Refer to https://www.graphviz.org for other platforms.
go tool pprof
Pass your binary location, and the location of the cpu.pprof
file as returned when running your program.
You can generate the analysis in various formats. The PDF one is pretty amazing:
go tool pprof --pdf ~/go/bin/yourbinary /var/path/to/cpu.pprof > file.pdf
You can generate other kind of visualizations as well, e.g. txt
:
go tool pprof --txt ~/go/bin/yourbinary /var/path/to/cpu.pprof > file.txt
Memory profiling is essentially the same as CPU profiling, but instead of using the default configuration for profile.Start()
, we pass a profile.MemProfile
flag:
defer profile.Start(profile.MemProfile).Stop()
thus the code becomes
package main
import (
//...
"github.com/pkg/profile"
)
func main() {
// Memory profiling
defer profile.Start(profile.MemProfile).Stop()
//...
}
and when running the program, it will generate a mem.pprof
file instead of cpu.pprof
.
This is just a start. Read more at:
github.com/pkg/profile
http://godoc.org/github.com/pkg/profileI wrote an entire book on this topic 👇
© 2023 Flavio Copes
using
Notion to Site
Interested in solopreneurship?