Go (Golang) Profiling Tutorial
Go (Golang) Profiling Tutorial
In this episode we are going to look at how to improve the performance of our Go programs by using the go profiler. The go profiling tools are all embedded into the Go toolchain and the Go standard library. No external dependencies are needed. There are different ways you can carry out profiling of Go programs, one of which is by using the benchmark tool from the test package. You can also start the profile right from the source code using runtime.pprof or exposing http endpoints with profiling data using net/http/pprof. Today we are mostly looking into the benchmarking and profiling workflows, although the profiling data analytics and output are the same regardless on how you collect them.
Source Code Before Optimisation – https://play.golang.org/p/EpwipyWKyq1
Source Code After Optimisation – https://play.golang.org/p/CRv6SvYRtAo
Install benchcmp to compare benchmarks (optional)
$ go get golang.org/x/tools/cmd/benchcmp
Benchmarks Before & After
$ benchcmp 0.bench 3.bench
benchmark old ns/op new ns/op delta
BenchmarkSolve-4 7250767 791637 -89.08%
benchmark old allocs new allocs delta
BenchmarkSolve-4 50060 2002 -96.00%
benchmark old bytes new bytes delta
BenchmarkSolve-4 6167372 199288 -96.77%
How to run your profiler with benchmarks
$ go test -run=XXX -bench=. -benchmem -memprofile mem.pprof -cpuprofile cpu.pprof -benchtime=10s
How to view profiling data (interactive shell)
$ go tool pprof cpu.pprof
How to view interactive web profiling analysis (opens up an interactive web page)
$ go tool pprof cpu.pprof -http=:8080
Go (Golang) Benchmark Tutorial – https://www.youtube.com/watch?v=L-BkH-_lXpk
💼 Golang Cafe – https://golang.cafe
📬 Golang Cafe Jobs Newsletter – https://golang.cafe/newsletter
🐦 Golang Cafe Twitter – https://twitter.com/golangcafe
📣 Telegram Channel – https://t.me/golangcafe
🙏 Found this video useful? Help me make more by donating $5.00 – https://golang.cafe/5USD
Comments are closed.