docs/advanced-guide/debugging/page.md
pprof in GoFr ApplicationsIn GoFr applications, pprof profiling is automatically enabled. The profiling endpoints are served on the METRICS_PORT, which defaults to 2121 if not specified.
This guide explains how to enable and use pprof in GoFr applications.
pprof in GoFrEnsure the METRICS_PORT is set (default is 2121):
METRICS_PORT=2121
GoFr automatically registers the following pprof routes:
/debug/pprof/cmdline/debug/pprof/profile/debug/pprof/symbol/debug/pprof/trace/debug/pprof/ (index)pprof EndpointsOnce pprof is enabled, you can access the profiling endpoints at http://localhost:<METRICS_PORT>/debug/pprof/. For example, if METRICS_PORT is 2121, the endpoints will be available at:
http://localhost:2121/debug/pprof//debug/pprof/cmdline:
/debug/pprof/profile:
/debug/pprof/symbol:
/debug/pprof/trace:
/debug/pprof/ (index):
To collect a CPU profile:
curl -o cpu.pprof http://localhost:2121/debug/pprof/profile
To collect a memory profile:
curl -o mem.pprof http://localhost:2121/debug/pprof/heap
To collect information about running goroutines:
curl -o goroutine.pprof http://localhost:2121/debug/pprof/goroutine
To collect an execution trace:
curl -o trace.out http://localhost:2121/debug/pprof/trace
To analyze CPU, memory, or goroutine profiles:
go tool pprof <profile_file>
topShows the functions consuming the most resources (e.g., CPU or memory).
go tool pprof cpu.pprof
(pprof) top
listDisplays the source code of a specific function, along with resource usage.
(pprof) list <function_name>
Example:
(pprof) list main.myFunction
webGenerates a visual representation of the profile in your browser. This requires Graphviz to be installed.
(pprof) web
To analyze execution traces:
go tool trace trace.out
Set Environment Variables:
METRICS_PORT=2121
Run Your GoFr Application:
go run main.go
Collect Profiling Data:
curl -o cpu.pprof http://localhost:2121/debug/pprof/profile
curl -o mem.pprof http://localhost:2121/debug/pprof/heap
Analyze the Data:
go tool pprof cpu.pprof
(pprof) top
(pprof) list main.myFunction
(pprof) web
go tool pprof mem.pprof
(pprof) top
(pprof) list main.myFunction
(pprof) web