liveBook · Manning

Pprof

As explained in 100 Go Mistakes and How to Avoid Them

net/http/pprof is a Go standard-library package that exposes runtime profiling data through HTTP handlers.

Exposing pprof does not mean continuously running a CPU profiler. Import net/http/pprof and serve HTTP to make profiling endpoints available at /debug/pprof. Performance-impacting profiles are activated for a specific period rather than running by default. section 12.8.1

package main
 
import (
    "fmt"
    "log"
    "net/http"
    _ "net/http/pprof"                                                      #1
)
 
func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {     #2
        fmt.Fprintf(w, "")
    })
    log.Fatal(http.ListenAndServe(":80", nil))
}

The blank import registers the profiling handlers; the HTTP server makes them reachable. section 12.8.1

Choose a profile for the symptom: CPU for time spent executing, heap for allocation and possible leaks, goroutine for active stack traces, mutex for lock contention, or block for waits on synchronization primitives. Requesting /debug/pprof/profile runs CPU profiling for 30 seconds by default, sampling through OS signals every 10 ms. section 12.8.1

$ go tool pprof -http=:8080 <file>

Open a collected profile with go tool pprof to inspect its call graph. section 12.8.1

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains HTTP setup, available profiles, and CPU sampling behavior.
Examples
Shows a Go HTTP server exposing pprof and a go tool pprof command for opening a profile.
What it assumes
Assumes familiarity with Go imports and HTTP servers while explaining how to enable pprof.
What you can do after
Apply it via net/http/pprof to expose profiles and go tool pprof to inspect collected data.
How it connects
Connects profiling to HTTP handlers, CPU sampling, heap allocation, goroutines, and synchronization contention.

Also explained in

Compare how 2 books explain pprof →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.