14.6 pprof

A great feature of Go’s standard library is its code performance monitoring tools. These packages exist in two places:

  1. net/http/pprof
  2. runtime/pprof

In fact, net/http/pprof simply exposes runtime profiling data from the runtime/pprof package on an HTTP port.

pprof support in Beego

The Beego framework currently supports pprof, however it is not turned on by default. If you need to test the performance of your application, (for instance by viewing the execution goroutine) such information from Go’s default package “net/http/pprof” already has this feature. Because beego has repackaged the ServHTTP function, you can not open the default feature included in pprof. This resulted in beego supporting pprof internally.

  • First in our beego.Run function, we choose whether or not to automatically load the performance pack according to our configuration variable (in this case, PprofOn):

    1. if PprofOn {
    2. BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
    3. BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
    4. }
  • Designing ProfController

    1. package beego
    2. import (
    3. "net/http/pprof"
    4. )
    5. type ProfController struct {
    6. Controller
    7. }
    8. func (this *ProfController) Get() {
    9. switch this.Ctx.Params[":pp"] {
    10. default:
    11. pprof.Index(this.Ctx.ResponseWriter, this.Ctx.Request)
    12. case "":
    13. pprof.Index(this.Ctx.ResponseWriter, this.Ctx.Request)
    14. case "cmdline":
    15. pprof.Cmdline(this.Ctx.ResponseWriter, this.Ctx.Request)
    16. case "profile":
    17. pprof.Profile(this.Ctx.ResponseWriter, this.Ctx.Request)
    18. case "symbol":
    19. pprof.Symbol(this.Ctx.ResponseWriter, this.Ctx.Request)
    20. }
    21. this.Ctx.ResponseWriter.WriteHeader(200)
    22. }

Getting started

From the above, we can see that enabling pprof is as simple as setting the PprofOn configuration variable to true:

  1. beego.PprofOn = true

You can then open the following URL in your browser to see the following interface:

14.6. pprof - 图1

Figure 14.7 current system goroutine, heap, thread information

By clicking on a goroutine, we can see a lot of detailed information:

14.6. pprof - 图2

Figure 14.8 shows the current goroutine details

Of course, we can also get more details from the command line:

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

This time, the program will begin profiling the application for a period of 30 seconds, during which time it will repeatedly refresh the page in the browser in an attempt to gather CPU usage and performance data.

  1. (pprof) top10
  2. Total: 3 samples
  3. 1 33.3% 33.3% 1 33.3% MHeap_AllocLocked
  4. 1 33.3% 66.7% 1 33.3% os/exec.(*Cmd).closeDescriptors
  5. 1 33.3% 100.0% 1 33.3% runtime.sigprocmask
  6. 0 0.0% 100.0% 1 33.3% MCentral_Grow
  7. 0 0.0% 100.0% 2 66.7% main.Compile
  8. 0 0.0% 100.0% 2 66.7% main.compile
  9. 0 0.0% 100.0% 2 66.7% main.run
  10. 0 0.0% 100.0% 1 33.3% makeslice1
  11. 0 0.0% 100.0% 2 66.7% net/http.(*ServeMux).ServeHTTP
  12. 0 0.0% 100.0% 2 66.7% net/http.(*conn).serve
  13. (pprof)web

14.6. pprof - 图3

Figure 14.9 shows the execution flow of information