go Ofpprof Can be used to test the performance of services, amongnet/http/pprof Package for detectionweb Analysis of the related performance of the server, Includegoroutine Quantity,heap The size of.

Simple use
package main import ( "fmt" "log" "net/http" _ "net/http/pprof" "time" ) func
main() {// openpprof 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, stayfor Continuous creation within the loopgo And callIO outputhello word.
Passing through the main functionhttp.ListenAndServe(“localhost:6060”, nil) Monitor6060 port, Wait for users to view relevant performance information.
Be sure to include_ “net/http/pprof” This bag..
The above code implements onepprof Code to monitor performance, We just need to execute in the browserhttp://localhost:6060/debug/pprof/
<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 timego The process is running.

Advanced

That's what we didpprof Simple use of, We can also usego tool pprof A more detailed performance analysis of this program.
The main analysis contents include:
aboutgolang Running environmentheap Information, Memorymem etc.
http://localhost:6060/debug/pprof/heap <http://localhost:6060/debug/pprof/heap>
aboutpprof andcpu Information about
http://localhost:6060/debug/pprof/profile
<http://localhost:6060/debug/pprof/profile>

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

1 Usecallgrind Command andqcachegrind Viewing performance

Viewcpu Information as an example:

(1) Start ourweb The server,
(2) staycmd Lower execution:go tool pprof http://localhost:6060/debug/pprof/profile
<http://localhost:6060/debug/pprof/profile> command, And wait for30s, Let it collect ourweb Server related performance
(3)
completion of enforcement, He reminds us to usehelp Command view related commands. Here we can usecallgrind Command willweb Service relatedCPU Information output as one.out file


Note that we need to installqcachegrind <https://pan.baidu.com/s/1nxcdoWL> Tools to view our output files.
Note that.out Files are stored in our current user directory, perhapscmd Directory..
stayqcachegrind lower, We can see the current service in detail, How much does that function costcpu And time, Find our performance bottleneck.


2 Useweb Command generationsvg chart

stay30s After waiting, We useweb Command generationsvg chart, View software processes and performance bottlenecks

Pay attention to generatingsvg It needs to be installed firstgraphviz-2.38 <https://pan.baidu.com/s/1bqh7ehD>
Tools for drawing. installgraphviz-2.38 Need to bebin Directory placedpath lower.. Finally, thesvg Open and view the picture with the browser.

3heap Related information

* go tool pprof -inuse_space http://127.0.0.1:6060/debug/pprof/heap
<http://127.0.0.1:6060/debug/pprof/heap>
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 http://127.0.0.1:8080/debug/pprof/heap
<http://127.0.0.1:8080/debug/pprof/heap>
Use the above command to view some information about memory object allocation, Thus learn, 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.