This is a creation in Article, where the information may have evolved or changed.
Performance optimization is an eternal topic, and many times when we are doing performance optimization, often based on the intuition on the code, all can think of optimization has been optimized again, good over any small optimization point, the result of the logic of the entire code becomes extremely complex, and performance is not much improvement. In fact, performance problems tend to focus on some small points, and sometimes small changes can have a huge boost, so the key to the problem is how to find out these optimization points, fortunately Golang in the design of the time to consider this problem, the native provides performance analysis tools, can easily help us to find performance bottlenecks
Pprof Introduction
Golang's performance Analysis library is in the runtime/pprof
main, providing the following several interfaces
// 堆栈分析func WriteHeapProfile(w io.Writer) error// cpu分析func StartCPUProfile(w io.Writer) errorfunc StopCPUProfile()
Using the above is simple, just need to pass the file pointer to the corresponding function, performance data will be written to the file, and then you can use the Golang pprof tool to generate a visual map of Svg,pdf, and then you can visually see the main performance consumption from these diagrams
As an example,
First you need a program
First of all you need to inject PPROF code inside your program, here is a sample code, the complete code in: https://github.com/hatlonely/...
func main() { go doSomething1() go doSomething2() go doSomething3() if err := pprof.PPCmd("cpu 10s"); err != nil { panic(err) } if err := pprof.PPCmd("mem"); err != nil { panic(err) }}
Compile, run the above code will generate two pprof files, cpu.pprof.yyyymmddhhmmss
and mem.pprof.yyyymmddhhmmss
, compile and run the following methods:
cd $GOPATH/srcgit clone git@github.com:hatlonely/hellogolang.gitcd hellogolangglide installgo build cmd/pprof_runtime.go./pprof_runtime
Pprof file Analysis
pprof files are binary, not for people to read, need to translate, and Golang original to provide us with analysis tools, directly execute the following command, will generate a very intuitive SVG images, directly with chrome can open, of course, can also generate other formats (pdf,png Available), you can go tool pprof -h
view supported output types with commands
go tool pprof -svg ./pprof_runtime cpu.pprof.201801301415 > cpu.svg
Note that this tool relies on the Graphviz tool, available on Mac brew install graphviz
, and CentOS yum install graphviz
can
HTTP interface
net/http/pprof
There are runtime/pprof
some encapsulation, external HTTP interface, can be accessed directly through the browser, but only some string results, no visualization, the experience is not very good, with the go tool
access experience can be better
go tool pprof http://localhost:3000/debug/pprof/profilego tool pprof http://localhost:3000/debug/pprof/heap
Personal feeling this interface comparison of chicken, the first biggest problem is that the display is not intuitive, if you can directly on the Web site to display the visualization may be really convenient, there is a need to provide an additional HTTP port, and this interface also relies on net/http
This means that if your app uses other third-party HTTP libraries, it may also need to address compatibility issues; In fact, when I use this interface again, there will be an access timeout in a scenario where the server is under a lot of stress, and the performance of this stressful situation may be the real performance bottleneck.
Recommended in accordance with the needs of their own encapsulated runtime/pprof
interface, of course, it is easier to use the scene than the package above, and then in the service itself to provide a dedicated performance analysis interface (may be gprc,thrift, or other third-party HTTP framework)
Flame diagram
In addition to the SVG diagram generated above, you can also generate a flame diagram, a tool provided by Uber that may be more intuitive to display
The installation commands are as follows:
go get github.com/uber/go-torchgit clone git@github.com:brendangregg/FlameGraph.gitexport PATH=$PATH:/path/to/FlameGraph
Here's how to use it:
go-torch --binaryname=./pprof_runtime --binaryinput=cpu.pprof.201801301415
Reference links
- Package Pprof:https://golang.org/pkg/runtim ...
- Profiling Go Programs:https://blog.golang.org/profi ...
- Go Torch:https://github.com/uber/go-torch
Reprint please indicate the source
This article link: http://hatlonely.github.io/20 ...