Here are some tips on profiling golang code with pprof.
Run a profiling server in your code on port 20020:
import (
"net/http"
_ "net/http/pprof"
// ...
)
func main() {
port := 20020
go func() {
err := http.ListenAndServe("localhost:"+strconv.Itoa(port), nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
// ...
}
-
Start you program
-
Connect to your profiling server
go tool pprof http://localhost:20020/debug/pprof/allocs
-
Create a memory-allocation graph
(pprof) svg Generating report in profile001.svg
-
Show memory allocations in a specific function, find function by name, for ex.
parseRegistrant
(pprof) list parseRegistrant ...
First of all we are interested in checking if cpu is intensevely used for garbage collection.
Now we'll take a look at a 30-second CPU profile:
go tool pprof http://localhost:20020/debug/pprof/profile?seconds=30
Note: the profiling slice will be effectively cut only if the program is executing any operations, the program that sleeps will not be profiled
Missing port definition in - port = 20020