Golang (GO) pprof in Beego

Luci Bro
3 min readApr 4, 2021

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

net/http/pprofruntime/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):
  • if PprofOn { BeeApp.RegisterController(`/debug/pprof`, &ProfController{}) BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{}) }
  • Designing ProfController
  • package beego import ( "net/http/pprof" ) type ProfController struct { Controller } func (this *ProfController) Get() { switch

--

--