Golang Profile Usage

Source: Internet
Author: User
Tags map data structure
This is a creation in Article, where the information may have evolved or changed.

Reprinted from: Http://www.jianshu.com/p/162f44022eb7

Profile

Profile is timed sampling, collecting CPU, memory and other information, and then give performance optimization guidance, Golang official provided Golang own performance analysis tool usage, also gave the guidance, the official introduction

Environment

Golang Environment, Graphviz

Generate Profile method

Golang currently provides 3 profiles, CPU profiles, memery profiles, blocking profile, two ways to generate these profiles, one is to use net/http/pprof packages, One is the need to write your own code, the following separate introduction

1. Net/http/pprof method

This method is very very simple, just need to introduce the NET/HTTP/PPROF package on the page can be viewed

package mainimport (    "fmt"    "log"    "net/http"    _ "net/http/pprof")func main() {    go func() {        for {            fmt.Println("hello world")        }    }()    log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))}

Http://127.0.0.1:8080/debug/pprof View overall information
Http://127.0.0.1:8080/debug/pprof/profile can download CPU profile to observe the analysis

Enter profile from terminal for detailed analysis
Go Tool pprof Http://localhost:6060/debug/pprof/profile
Go Tool pprof Http://localhost:6060/debug/pprof/heap
Go Tool pprof Http://localhost:6060/debug/pprof/block

    1. How to write code
  func Main () {CPUF, err: = OS. Create ("Cpu_profile") if err! = Nil {log. Fatal (Err)} pprof. Startcpuprofile (CPUF) defer pprof. Stopcpuprofile () ctx, _: = Context. Withtimeout (context. Background (), time. second*5) test (CTX) time. Sleep (time. Second * 3) memf, err: = OS. Create ("Mem_profile") if err! = Nil {log. Fatal ("Could not create memory profile:", err)} If err: = Pprof. Writeheapprofile (MEMF); Err! = Nil {log. Fatal ("Could not write memory profile:", err)} memf. Close ()}func Test (c context.        Context) {i: = 0 J: = 0 Go func () {m: = map[int]int{} for {i++ m[i] = i    }} () go func () {m: = map[int]int{} for {j + + m[i] = i}} () Select {Case <-c.done (): FMT. Println ("Done, I", I, "J", J) Return}}  

Generates two profiles, one for the CPU and one for the memory. Enter the Proflie method

Go tool pprof Main profile
Main represents a binary file, which is a compiled executable file.
Profile is the profile generated above, can be cpu_profile, or it can be mem_profile

For Cpu_profile, it's time to start counting when the code starts.
Mem_profile part of the code if the code at the beginning of the location is not statistical, you need to find a better location

How to analyze Profile

1. The sample code is as follows

 Package Mainimport ("Flag" "FMT" "Log" "Math/rand" "OS" "Runtime/pprof" "Sort" "time") var n Um = 100000000var FindNum = 10000000var t = flag. String ("T", "map", "use Map") Func main () {CPUF, err: = OS. Create ("Cpu_profile") if err! = Nil {log. Fatal (Err)} pprof. Startcpuprofile (CPUF) defer pprof. Stopcpuprofile () flag. Parse () if *t = = "Map" {FMT. PRINTLN ("Map") Findmapmax ()} else {fmt. Println ("Slice") Findslicemax ()}}func Findmapmax () {m: = map[int]int{} for I: = 0; i < num; i++ { M[i] = i} for I: = 0; i < FindNum; i++ {tofind: = rand. INT31N (Int32 (num)) _ = M[int (tofind)]}}func Findslicemax () {m: = make ([]int, num) for I: = 0; i < num ; i++ {M[i] = i} for I: = 0; i < FindNum; i++ {tofind: = rand. INT31N (Int32 (num)) V: = sort. Searchints (M, int (tofind)) fmt. Println (v)}} 

The code is very simple, mainly to introduce how to analyze the profile, to achieve the effect, do not care about the details.
This code is to use map, slice two kinds of data structure, Sir into num elements, in from map, slice randomly find findnum elements, choose Map or slice can be specified by-T, this demo uses non-net/http/pp ROF Way

2. Preparatory work

Need to generate profile files and binaries
Build binaries: Go build main.go (the main binary is generated after executing the command)
Generate Profile:./main (do not specify-T, using the map data structure by default, generates Cpu_profile, this file is what we are profiling)

3. Analysis Profile

    • Now that the preparation is done, we currently generate the main binary executable, cpu_profile the profile required for performance analysis, and then we will formally enter the profile for analysis.
    • Go tool pprof main Cpu_profile executes this command into the profile, and we can begin to parse the code.
    • Enter Help, you can see what actions are supported, there are many commands to choose according to the need, we only introduce 4 I prefer to use the command Web, top, PEEK, list
      *web-----In the profile input web, will generate the page version of the call Analysis Diagram (need to install Graphviz) such as:
      Web
      After you enter the Web command, the browser automatically opens with the following content:
      Web2

This allows you to see how much time each step takes, and you can perform a rough analysis of the performance, but most of the time it's not something that we care about, such as what you see in this demo is a function that you don't know (it's actually a runtime operation of map).

    • Top-----Enter top in profile to list some of the most time-consuming operations


      Web2

Because the performance statistics are sampling operations, so not every time the statistics come out of the same, the most important thing is often statistics are the bottom of the operation, not our concern, and not everyone can understand, we need an intuitive approach, very intuitive to write their own code time to see out, Here is a very good way for me to think about myself.

    • Peek,list Magical
      Peek is used to query the name of a function (the name is the name that the list needs to use and is not exactly equal to the letter name)
      List is used to list the function time consumption.
      1) List Main.main


      Web2
    1. Peek Findmapmax (because according to 1 can be seen in the consumption of Findmapmax)


      Web2

      3) List Main.findmapmax (according to 2 can see the name is Main.findmapmax)


      Web2

The Magic Peek List directive can be very intuitive to see, our code problem in m[i] = i, which means that the map is the write operation consumes 38.75s, and 44 rows of read operation only 2.35s, for this demo, we want to optimize is m[i] = i, because this sentence operation has been Language level, we do not have the ability to optimize for him, so this time if the demand can only use map, then this program has little space to optimize, if the demand can use other data structure, then we will definitely change the map to slice, well-known map Each time you save a function hash calculation, and the number of saved to reach a certain amount, but also to re-allocate the map of the operation of space, so it must be time-consuming.

Summarize

    1. Go run Main.go-t=slice will use the data structure of slice, students can do their own analysis according to the method of the article, test how you master.
    2. executes the help command in profile, listing all commands, interested in studying
    3. memory is the same analysis method, the location of the mem_profile generated in the demo may need to be adjusted, I did not verify, Reducing memory usage can also significantly improve performance.
    4. There is also a blocking profile that is not given in handwriting, and is interested in being able to Google yourself
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.