Skip to content

AlphaWong/How-to-setup-a-go-profiling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

How-to-setup-a-go-profiling

Objective

Help developers profile their go application

Tools

  1. FlameGraph
  2. github.com/uber/go-torch
  3. pacaur -S graphviz
  4. github.com/google/pprof

Code level

Please ensure following handler have been enabled by your application in http server.

imports (
  ...
  "net/http/pprof"
  ...
)

func someFunction(){
  ...
	http.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
	http.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
	http.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
	http.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
	http.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
  ...
}

Native solution ( go 1.10 )

  1. git clone https://github.com/brendangregg/FlameGraph.git
  2. cp flamegraph.pl /usr/local/bin
  3. go tool pprof --seconds 25 http://localhost:9090/debug/pprof/profile?seconds=25
  4. go get github.com/google/pprof
  5. pacaur -S graphviz
  6. pprof -http=127.0.0.1:9000 /home/alpha/pprof/pprof.main.samples.cpu.001.pb.gz

Run the pprof in pi

from arm32v7/golang

RUN go get github.com/google/pprof
RUN apt update && apt install graphviz -y

ENTRYPOINT ["pprof"]
  1. run docker run -it -p 9000:9000 --network="host" -v $(pwd):/tmp go-pprof -http=10.10.8.111:9000 /tmp/pprof.main.samples.cpu.001.pb.gz

Installation

  1. git clone https://github.com/brendangregg/FlameGraph.git
  2. cp flamegraph.pl /usr/local/bin
  3. go get -v github.com/uber/go-torch

Show api call map

Step 1

# It will capture incoming call in 25s
go tool pprof --seconds 25 http://localhost:9090/debug/pprof/profile

Step 2

# Use your fav stress test tool e.g. tsenart/vegeta

Step 3

# Use web to output the call map
Fetching profile from http://localhost:9090/debug/pprof/profile?seconds=25
Please wait... (25s)
Saved profile in /Users/lihaoquan/pprof/pprof.localhost:9090.samples.cpu.014.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) web

Output

Call map

Show flame graph

# It will capture 30s incoming call
# Then generate a torch.svg in your current dir
go-torch -u http://localhost:9090 -t 30

Output

Cpu map

How to read

  • x-axis means the cpu time
  • y-axis means sequence of function call
  • Read from bottom to top
  • Flame color is meaningless

Reference

  1. http://cizixs.com/2017/09/11/profiling-golang-program
  2. http://lihaoquan.me/2017/1/1/Profiling-and-Optimizing-Go-using-go-torch.html
  3. https://stackoverflow.com/questions/30560859/cant-use-go-tool-pprof-with-an-existing-server

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published