Skip to content

Latest commit

 

History

History
110 lines (66 loc) · 3.12 KB

File metadata and controls

110 lines (66 loc) · 3.12 KB

14.6 pprof

Go language has a great design is the standard library with code performance monitoring tools, there are packages in two places:

net/http/pprof

runtime/pprof

In fact, net/http/pprof in just using runtime/pprof package for packaging a bit, and exposed on the http port

Beego support pprof

Currently beego framework adds pprof, this feature is not turned on by default, if you need to test performance, view the execution goroutine such information, in fact, Go's default package "net/http/pprof" already has this feature, and if Go manner in accordance with the default Web, you can use the default, but because beego repackaged ServHTTP function, so if you can not open the default includes this feature, so the need for internal reform beego support pprof.

  • First in beego.Run function automatically according to whether the variable load performance pack

      if PprofOn {
      	BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
      	BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
      }
  • Design ProfConterller

      package beego
    
      import (
      	"net/http/pprof"
      )
      
      type ProfController struct {
      	Controller
      }
      
      func (this *ProfController) Get() {
      	switch this.Ctx.Params[":pp"] {
      	default:
      		pprof.Index(this.Ctx.ResponseWriter, this.Ctx.Request)
      	case "":
      		pprof.Index(this.Ctx.ResponseWriter, this.Ctx.Request)
      	case "cmdline":
      		pprof.Cmdline(this.Ctx.ResponseWriter, this.Ctx.Request)
      	case "profile":
      		pprof.Profile(this.Ctx.ResponseWriter, this.Ctx.Request)
      	case "symbol":
      		pprof.Symbol(this.Ctx.ResponseWriter, this.Ctx.Request)
      	}
      	this.Ctx.ResponseWriter.WriteHeader(200)
      }

Getting started

Through the above design, you can use the following code to open pprof:

beego.PprofOn = true

Then you can open in a browser the following URL to see the following interface:

Figure 14.7 current system goroutine, heap, thread information

Click goroutine we can see a lot of detailed information:

Figure 14.8 shows the current goroutine details

We can also get more details from the command line information

go tool pprof http://localhost:8080/debug/pprof/profile

This time the program will enter the profile collection time of 30 seconds, during which time desperately to refresh the page on the browser, try to make cpu usage performance data.

(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

Figure 14.9 shows the execution flow of information

Links