We are looking for a Java resume, coordinate: Beijing.

preface

If you want to perform performance tuning during Golang development, you generally need to use Pprof, and this article describes how to use the PPROF tool.

Download the test code

You can obtain the test program from go Get. Add -d to avoid automatic installation after downloading

Github

go get -d github.com/wolfogre/go-pprof-practice
cd $GOPATH/src/github.com/wolfogre/go-pprof-practice
Copy the code

If you can’t download go Get, you can download git Clone

gir clone https://github.com/wolfogre/go-pprof-practice
Copy the code

Compile the code

Then run

go mod init
go mod tidy
Copy the code

Last run

go build
./go-pprof-practice
Copy the code

Keep the program is running, open a browser to http://localhost:6060/debug/pprof/, you can see the following page:

Parameters that

type describe note
allocs Sample information about memory allocation You can open it in a browser, but it’s not very readable
blocks Sampling information for blocking operations You can open it in a browser, but it’s not very readable
cmdline Displays program startup commands and parameters You can open it in a browser and it will show./go-pprof-practice
goroutine Stack information for all current coroutines You can open it in a browser, but it’s not very readable
heap Sample information about memory usage on the heap You can open it in a browser, but it’s not very readable
mutex Sampling information for lock contention You can open it in a browser, but it’s not very readable
profile Sampling information about CPU usage The browser opens and downloads the file
threadcreate Sample information about system thread creation You can open it in a browser, but it’s not very readable
trace Program run trace information Opening a browser will download the file, which is not covered in this article

Code instructions

Test the description of the main function in the code program

Import (// omit _ "net/ HTTP /pprof" // automatically register handler to HTTP server, Func main() {// GOMAXPROCS(1) {// GOMAXPROCS(1); Avoid overload runtime. SetMutexProfileFraction (1) / / open the track of lock calls the runtime SetBlockProfileRate (1) / / open the track of blocking operations go func () {/ / start a If err := HTTP.ListenAndServe(":6060", nil); err ! = nil {log.fatal (err)} os.exit (0)}()Copy the code

Check whether the CPU usage is too high

You can view the Practice usage through the activity monitor.

You can use the go tool pprof http://localhost:6060/debug/pprof/profile of illness.

Enter the top command to view the calls with high CPU usage:

Can see CPU usage is high github.com/wolfogre/go-pprof-practice/animal/felidae/tiger. Tiger (*). Input the list the Eat Eat, see the question which one location in the code:

You can see that 10 billion empty loops are taking up a lot of CPU time, so you’ve located the problem.

Welcome to pay attention to the public number: programmer wealth free road

We are looking for a Java resume, coordinate: Beijing. It has my contact information on it

The resources

  • Blog.wolfogre.com/posts/go-pp…
  • Geektutu.com/post/hpg-co…