Go có hỗ trợ profiling tích hợp sẵn thông qua package net/http/pprof: chỉ cần import _ "net/http/pprof" là server tự expose các endpoint profiling tại /debug/pprof/.
- Để phân tích CPU, dùng
go test -cpuprofile=cpu.profrồi mở bằnggo tool pprof cpu.prof; tương tự với memory profiling dùng flag-memprofile=mem.profđể tìm các hàm allocate nhiều bộ nhớ. - Công cụ pprof hỗ trợ xem flame graph trực quan qua
pprof -http=:8080, giúp nhanh chóng nhận diện hot functions, excessive allocations, và goroutine leaks. - Ngoài ra
go tool tracecho phép xem chi tiết execution trace theo timeline, hữu ích khi debug vấn đề concurrency và latency.
Go has built-in profiling via net/http/pprof: just import _ "net/http/pprof" and the server exposes profiling endpoints at /debug/pprof/.
- For CPU analysis:
go test -cpuprofile=cpu.profthen open withgo tool pprof cpu.prof. - For memory:
-memprofile=mem.profto find high-allocation functions. - The pprof tool renders interactive flame graphs via
pprof -http=:8080, helping identify hot functions, excessive allocations, and goroutine leaks.go tool traceprovides a detailed timeline for debugging concurrency and latency issues.