This is a creation in Article, where the information may have evolved or changed.
Go has pprof packages for code performance monitoring in two places with packages:
net/http/pprofruntime/pprof
In fact, net/http/pprof just use the RUNTIME/PPROF package to encapsulate it and expose it on the HTTP port.
Pprof Bag
Web server
If your go program is a Web server that starts with an HTTP package, you want to see the status of your Web server. This is the time to choose Net/http/pprof. You just need to introduce the package _ "Net/http/pprof",
You can then use http://localhost:port/debug/pprof/directly in your browser to see the status of the current Web service, including CPU usage and memory use. You can read Godoc's instructions for specific use cases.
Service process
If your go program is not a Web server, but a service process, then you can also choose to use the Net/http/pprof package, also introduce the package net/http/pprof, and then open the other goroutine to open the port monitoring.
Like what:
go func () { log. Println (http. Listenandserve ("localhost:6060", Nil)} ()
Application
If your go program is just an application, such as calculating the Fabonacci sequence, then you cannot use the NET/HTTP/PPROF package and you need to use Runtime/pprof. The specific approach is to use the pprof. Startcpuprofile and Pprof.stopcpuprofile. For example, the following:
var cpuprofile = flag. String ("Cpuprofile", "", "Write CPU profile to file") Func main () { flag. Parse () if *cpuprofile! = "" { F, err:= os. Create (*cpuprofile) if err ! = nil { log. Fatal (Err) } pprof. Startcpuprofile (f) defer pprof. Stopcpuprofile () } ...
Add a--cpuprofile parameter when running the program, such as Fabonacci--cpuprofile=fabonacci.prof
The CPU information will be recorded in the xxx.prof when the program is running.
The next step is to use this prof information to make a performance analysis diagram (need to install Graphviz).
Use Go tool pprof (application) (application's Prof file)
When you enter Pprof, the SVG file is generated in/tmp using the Web command, which can be viewed under the browser. Look like this:
If your program is very simple, such as only println a statement, you use Pprof. Startcpuprofile doesn't print anything.
Example
Take Go-tour For example, this is a Web application, I added the code in the
_ "Net/http/pprof"
In the browser, I can read Prof information directly.
Generating a CPU Status analysis diagram
Below we want to generate the CPU state analysis diagram, call go tool pprof Http://localhost:3999/debug/pprof/profile
Will enter the 30-second profile collection time, in the event of a strong refresh click on the Go-tour browser page, as far as possible to let CPU consumption performance data generation.
(pprof) Top10
Total:3 samples
1 33.3% 33.3% 1 33.3% mheap_alloclocked
1 33.3% 66.7% 1 33.3% os/exec. (*cmd). closedescriptors
1 33.3% 100.0% 1 33.3% runtime.sigprocmask
0 0.0% 100.0% 1 33.3% mcentral_grow
0 0.0% 100.0% 2 66.7% main.compile
0 0.0% 100.0% 2 66.7% main.compile
0 0.0% 100.0% 2 66.7% Main.run
0 0.0% 100.0% 1 33.3% Makeslice1
0 0.0% 100.0% 2 66.7% net/http. (*servemux). Servehttp
0 0.0% 100.0% 2 66.7% net/http. (*conn). Serve
(pprof) Web
Reference
Http://blog.golang.org/2011/06/profiling-go-programs.html
This official article is very careful, but notice that the Gopprof tool is replaced with the Go Tools pprof.