Format and remove 14.6.md spaces

This commit is contained in:
vCaesar
2017-06-10 12:34:59 +08:00
parent 5347abb6e6
commit 4416cabf6c

View File

@@ -2,9 +2,9 @@
Go语言有一个非常棒的设计就是标准库里面带有代码的性能监控工具在两个地方有包
```Go
net/http/pprof
runtime/pprof
net/http/pprof
runtime/pprof
```
其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下并在http端口上暴露出来
@@ -14,47 +14,47 @@ Go语言有一个非常棒的设计就是标准库里面带有代码的性能监
- 首先在beego.Run函数中根据变量是否自动加载性能包
```Go
if PprofOn {
BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
}
if PprofOn {
BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
}
```
- 设计ProfConterller
```Go
package beego
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)
}
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)
}
```
## 使用入门
通过上面的设计你可以通过如下代码开启pprof
```Go
beego.PprofOn = true
beego.PprofOn = true
```
然后你就可以在浏览器中打开如下URL就看到如下界面
![](images/14.6.pprof.png?raw=true)
@@ -70,7 +70,7 @@ Go语言有一个非常棒的设计就是标准库里面带有代码的性能监
我们还可以通过命令行获取更多详细的信息
```Go
go tool pprof http://localhost:8080/debug/pprof/profile
go tool pprof http://localhost:8080/debug/pprof/profile
```
这时候程序就会进入30秒的profile收集时间在这段时间内拼命刷新浏览器上的页面尽量让cpu占用性能产生数据。