net/http/pprof

Guided tour · Networking · pkg.go.dev →

Serve runtime profiling data over HTTP at /debug/pprof/. Import for side effects to enable.

Enable the handlers

Blank import — handlers attach to http.DefaultServeMux

If you use a custom mux, you'll need to register the handlers yourself. Never expose pprof publicly — keep it on localhost or behind auth.

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

func main() {
    go http.ListenAndServe("localhost:6060", nil)
    // ...
}

Profile with go tool pprof

# 30-second CPU profile
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30

# heap snapshot
go tool pprof http://localhost:6060/debug/pprof/heap

# goroutines
curl http://localhost:6060/debug/pprof/goroutine?debug=2