From 4416cabf6cd20a6f101d1996a5bdaa79511ce8c7 Mon Sep 17 00:00:00 2001 From: vCaesar Date: Sat, 10 Jun 2017 12:34:59 +0800 Subject: [PATCH] Format and remove 14.6.md spaces --- zh/14.6.md | 66 +++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/zh/14.6.md b/zh/14.6.md index 53843f4d..ff4ae7cd 100644 --- a/zh/14.6.md +++ b/zh/14.6.md @@ -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占用性能产生数据。