Pprof usage of Go

Source: Internet
Author: User
Web Server

If your go program is a web server started with an http package, you want to view the status of your Web server. In this case, you can select net/HTTP/pprof. You only need to introduce package _ "net/HTTP/pprof", and then you can use http: // localhost: port/debug/pprof/directly shows the current web service status, including CPU usage and memory usage. For more information, see the description of godoc.

Add in code

_ "Net/HTTP/pprof"

In the browser, I can directly view the prof information.

Generate a CPU Status Analysis Chart

Below we want to generate a CPU status analysis chart,

Call Go tool pprof http: // localhost: 3999/debug/pprof/Profile

Or go tool pprof http: // localhost: 3999/debug/pprof/profile -- text

It will enter the profile collection time of 30 seconds. During this event, click Go-tour to refresh the page and try to generate data for CPU usage.

(Pprof) Top

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

Service Process

If your go program is not a web server but a service process, you can also choose to use the net/HTTP/pprof package and introduce the net/HTTP/pprof package, then, enable another goroutine to enable port listening.

For example:

go func() {        log.Println(http.ListenAndServe("localhost:8080", nil)) 

}()

Applications

import "runtime/pprof"
func main() {
    f, _ := os.Create("./profile.prof")
    pprof.StartCPUProfile(f)
Defer pprof. stopcpuprofile ()

.......
}


#./Geteandata run program to collect information

# Go tool pprof./geteandata./profile. Prof file generated by Analysis

Enter pprof
Top: You can use top to view the most time-consuming function.
The Web will generate an SVG file under/tmp. the SVG file can be viewed in the browser. Note: If sh: dot: Command not found is reported, graphviz needs to be installed.
# Yum install graphviz
# Dot-V check whether graphviz is successfully installed

 

The meanings of each field are as follows:

1. Number of times the sampling point falls into the function

2. Percentage of sampling points in the function

3. cumulative percentage of the previous item

4. The sampling point falls into the function and the total number of times it calls the function.

5. The sample points fall into the function and the total number of times in the function called by the function.

6. Function Name



Note: Some information in this article is transferred from http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html

Pprof usage of Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.