Junior — Middle
Can you explain what the profiling tool Pprof is and how it is used in development?
sobes.tech AI
Answer from AI
Pprof is a profiling tool built into the Go language that allows collecting and analyzing application performance data, such as CPU usage, memory, blocking, and other metrics.
How it is used:
- The
net/http/pprofpackage is imported into the application code, which automatically registers HTTP endpoints for profile collection. - The application is run with pprof enabled.
- Using the
go tool pprofcommand or a web interface, you can connect to the application and obtain profiles.
Example of connecting pprof in Go:
import (
"net/http"
_ "net/http/pprof"
)
func main() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// rest of the application code
}
After starting, you can get a CPU profile with the command:
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
Pprof helps identify bottlenecks in performance and optimize the application.