├── README.md └── main.go /README.md: -------------------------------------------------------------------------------- 1 | # Go pprof & trace example 2 | 3 | This repo contains just an example on how to use the Go trace and pprof tools. It aims at being short, simple, with clear commands and a short piece of code. It also contains some links to some more details articles. 4 | 5 | ## Profiling 6 | 7 | Profiling based on CPU: 8 | 9 | ``` 10 | go tool pprof http://127.0.0.1:8080/debug/pprof/profile 11 | ``` 12 | 13 | Profiling based on heap: 14 | 15 | ``` 16 | go tool pprof http://127.0.0.1:8080/debug/pprof/heap 17 | ``` 18 | 19 | NOTE: use the `-seconds` option to limit the time of tracing. Take into account that if you use read or write timeout for your application, this will also limit your capabilities of getting the profiling data. 20 | 21 | Interesting option for memory profiling: 22 | 23 | ``` 24 | Sample value selection option (for heap profiles): 25 | -inuse_space Display in-use memory size 26 | -inuse_objects Display in-use object counts 27 | -alloc_space Display allocated memory size 28 | -alloc_objects Display allocated object counts 29 | ``` 30 | 31 | When launched an interactive mode, use `top` to see the top entries and `web` to get a nice graph in your browser. 32 | For more information, a more in detail article: 33 | 34 | http://artem.krylysov.com/blog/2017/03/13/profiling-and-optimizing-go-web-applications/ 35 | 36 | ## Tracing 37 | 38 | Get the tracing data: 39 | 40 | ``` 41 | curl http://localhost:8080/debug/pprof/trace?seconds=5 > trace.out 42 | ``` 43 | 44 | Start the tracing interface: 45 | 46 | ``` 47 | go tool trace -http=':8081' trace http://localhost:8080/debug/pprof/trace 48 | ``` 49 | 50 | NOTE: the -http option specifies the address to use for the Web API for the tracing tool. For more information on the tool, read this interesting article: https://making.pusher.com/go-tool-trace/ 51 | 52 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net/http" 7 | "net/http/pprof" 8 | ) 9 | 10 | func healthcheckHandler(w http.ResponseWriter, _ *http.Request) { 11 | w.WriteHeader(http.StatusOK) 12 | fmt.Fprintln(w, "OK") 13 | } 14 | 15 | func registerPProfHandlers(r *http.ServeMux) { 16 | r.HandleFunc("/debug/pprof/", pprof.Index) 17 | r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) 18 | r.HandleFunc("/debug/pprof/profile", pprof.Profile) 19 | r.HandleFunc("/debug/pprof/symbol", pprof.Symbol) 20 | r.HandleFunc("/debug/pprof/trace", pprof.Trace) 21 | } 22 | 23 | func main() { 24 | m := http.NewServeMux() 25 | m.HandleFunc("/healthz", healthcheckHandler) 26 | registerPProfHandlers(m) 27 | if err := http.ListenAndServe(":8080", m); err != nil { 28 | log.Fatal(err) 29 | } 30 | } 31 | --------------------------------------------------------------------------------