Sobes.tech
Senior

What profiling tools are available in Go?

sobes.tech AI

Answer from AI

In Go, the net/http/pprof package is used for profiling applications, allowing collection of data on CPU, memory, locks, and goroutines. It can be attached to an HTTP server, and then profiles can be accessed through a standard interface.

There is also the go tool pprof tool for analyzing collected profiles.

Example of attaching pprof in code:

import (
    "net/http"
    _ "net/http/pprof"
)

func main() {
    go func() {
        http.ListenAndServe(":6060", nil) // Profiling is available at http://localhost:6060/debug/pprof/
    }()
    // main application code
}

To analyze the CPU profile:

go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30

Additionally, there are third-party tools, such as the pprof visualizer, go-torch for flamegraphs, and integrations with monitoring systems like Prometheus and Grafana.

What profiling tools are available in Go? — Golang - sobes.tech