Pre-Requisite
Install Graphviz
Mac:
brew install graphviz
Linux:
sudo apt install graphviz
Windows:
See https://graphviz.org/download/
Benchmarks
We can add in a benchmark test which will allow us to profile a section of code.
Creating Profiles from Benchmark Test
go test -bench=. -run=x -benchmem -memprofile mem.prof -cpuprofile cpu.prof -benchtime=5s > 0.bench
After running the benchmark we can use the outputted profiles with pprof.
Adding Profiling to your Application
Add the following code:
import ( echopprof "github.com/hiko1129/echo-pprof" "github.com/labstack/echo/v4" ) func Start(){ e := echo.New() echopprof.Wrap(e) e.Start(":8080") }
Pull Metric from Endpoint
curl -sK -v http://localhost:8888/debug/pprof/heap > heap.out
Open heap.out in pprof
go tool pprof heap.out
Profiling with Pprof
Profile CPU
$ go tool pprof cpu.prof Type: cpu Time: Jul 15, 2022 at 1:25pm (EDT) Duration: 7.34s, Total samples = 7.45s (101.51%) Entering interactive mode (type "help" for commands, "o" for options) (pprof)
top 20
(pprof) top 20 Showing nodes accounting for 7.18s, 96.38% of 7.45s total Dropped 81 nodes (cum <= 0.04s) Showing top 20 nodes out of 134 flat flat% sum% cum cum% 1.56s 20.94% 20.94% 1.56s 20.94% runtime.kevent 1.45s 19.46% 40.40% 1.45s 19.46% runtime.madvise 0.79s 10.60% 51.01% 1.18s 15.84% encoding/json.checkValid 0.68s 9.13% 60.13% 0.68s 9.13% encoding/json.unquoteBytes 0.48s 6.44% 66.58% 0.48s 6.44% encoding/json.(*decodeState).rescanLiteral 0.37s 4.97% 71.54% 0.37s 4.97% encoding/json.stateInString 0.37s 4.97% 76.51% 0.37s 4.97% runtime.pthread_cond_signal 0.31s 4.16% 80.67% 0.31s 4.16% runtime.pthread_kill 0.26s 3.49% 84.16% 0.27s 3.62% encoding/json.(*encodeState).string 0.21s 2.82% 86.98% 0.21s 2.82% syscall.syscall 0.20s 2.68% 89.66% 0.20s 2.68% runtime.usleep 0.19s 2.55% 92.21% 0.19s 2.55% runtime.pthread_cond_wait 0.13s 1.74% 93.96% 0.13s 1.74% unicode/utf8.RuneCountInString 0.04s 0.54% 94.50% 0.04s 0.54% runtime.memmove 0.04s 0.54% 95.03% 0.10s 1.34% runtime.scanobject 0.03s 0.4% 95.44% 0.07s 0.94% kafka-azure-sink/src/internal/gateways/kafka/consumer.randomString 0.03s 0.4% 95.84% 0.04s 0.54% runtime.findObject 0.02s 0.27% 96.11% 0.44s 5.91% runtime.gcDrain 0.01s 0.13% 96.24% 1.18s 15.84% encoding/json.(*decodeState).objectInterface 0.01s 0.13% 96.38% 0.13s 1.74% runtime.gopreempt_m (pprof)
Visualize It
(pprof) web
References
Reference | URL |
---|---|
Go (Golang) Profiling Tutorial | https://www.youtube.com/watch?v=HEwSkhr_8_M |
How I investigated memory leaks in Go using pprof on a large codebase | https://www.freecodecamp.org/news/how-i-investigated-memory-leaks-in-go-using-pprof-on-a-large-codebase-4bec4325e192/ |