Profiling in Golang
We often face memory leakage issues while writing large data processing codebase. An efficient way to find if the code is running efficiently is by checking the memory heap and CPU usage.
To check the CPU and memory usage and other profiles of a Go application at runtime, we can use `pprof` package.
You can check for the following details at runtime based on the profile you chose:
- CPU
- Memory/Heap
- Thread/Go routines
- Block
- Mutex
There will be few actions on profiling the code
1.Setup profiling in the code
Import pprof package and set up a webserver for getting the go profiles.
To do that just add the proof package to imports -`import _ “net/http/pprof” and start a HTTP server on any port.
2.Save the profile at the certain point of time:
Run the Go application and Choose any profile you want to analyse and call the profiling webserver with the profile.
Example for getting heap profile: `curl http://localhost:6060/debug/pprof/heap`
If the profile is needed for last ’n’ seconds you can profile it by setting ‘seconds’ parameter in the query.
`http://localhost:6060/debug/pprof/heap?seconds=n`
You can easily save profile by curl http://localhost:6060/debug/pprof/heap --output heap.tar.gz
3.Analyse the profile snapshot using a tool:
To analyse the profile we can use `go tool pprof`
`go tool pprof <file_created_from_previous_step>`
ie: go tool pprof heap.tar.gz
Once you enter into the interactive shell, enter top
to see the functions which are using memory and total memory usage of each function.
To simplify the Step 2 and Step 3, we can get the profile and display the profile in neat diagram using
`go tool pprof -web http://localhost:6060/debug/pprof/heap` which would give output like
This way of visualizing the profile gives more advantage when the stack of function is high.
Learnt and used the example from here — https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/