go Of pprof Can be used to test the performance of services , among net/http/pprof Package for detection web Analysis of the related performance of the server , include goroutine Number of ,heap The size of .

Easy to use
package main import ( "fmt" "log" "net/http" _ "net/http/pprof" "time" ) func
main() {// open pprof go func() { log.Println(http.ListenAndServe("localhost:6060",
nil)) }() go hello() select {} } func hello() { for { go func() { fmt.Println(
"hello word") }() time.Sleep(time.Millisecond * 1) } }
As shown above , A simple function is implemented , stay for Continuous creation within the loop go And call IO output hello word.
Passing through the main function http.ListenAndServe(“localhost:6060”, nil) monitor 6060 port , Wait for users to view relevant performance information .
Be sure to include _ “net/http/pprof” This bag ..
The above code implements one pprof Code to monitor performance , We just need to execute in the browser http://localhost:6060/debug/pprof/
You will get the information of the corresponding program .
give the result as follows
thus it can be seen , We have six programs at this time go The process is running .


That's what we did pprof Simple use of , We can also use the go tool pprof A more detailed performance analysis of this program .
The main analysis contents include :
about golang Operating environment heap Information about , Memory mem etc.
http://localhost:6060/debug/pprof/heap <http://localhost:6060/debug/pprof/heap>
about pprof and cpu Information about

We use these two commands , In our cmd Execute to get relevant information .

1 use callgrind Command and qcachegrind View performance

To view cpu Information as an example :

(1) Start our web The server ,
(2) stay cmd Next execution :go tool pprof http://localhost:6060/debug/pprof/profile
<http://localhost:6060/debug/pprof/profile> command , And wait 30s, Let it collect our web Server related performance
completion of enforcement , He reminds us to use help Command view related commands . Here we can use callgrind The command will web Service related CPU Information output as one .out file

Note that we need to install qcachegrind <https://pan.baidu.com/s/1nxcdoWL> Tools to view our output files .
Pay attention to the .out Files are stored in our current user directory , perhaps cmd Directory ..
stay qcachegrind lower , We can see the current service in detail , How much does that function cost cpu And time , Find our performance bottleneck .

2 use web Command generation svg chart

stay 30s After waiting , We use web Command generation svg chart , View software processes and performance bottlenecks

Pay attention to generating svg It needs to be installed first graphviz-2.38 <https://pan.baidu.com/s/1bqh7ehD>
Tools for drawing . install graphviz-2.38 You need to bin Directory to path lower .. Finally, the svg Open and view the picture with the browser .

3heap Relevant information

* go tool pprof -inuse_space
We can use the above command , View some memory information being used in memory , You can see what are the main memory consuming functions .

* go tool pprof -alloc_space
Use the above command to view some information about memory object allocation , So we know , Where to create objects and consume memory .

* top -cum
By means of the above order , Be able to define the memory consumption during function call .