Files
build-web-application-with-…/ja/14.6.md
2014-12-14 23:27:42 +08:00

106 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 14.6 pprofのサポート
Go言語のとても良い設計は、標準ライブラリにコードの性能監視ツールを有することです。つのパッケージがあります
net/http/pprof
runtime/pprof
実はnet/http/pprofではruntime/pprofパッケージを使ってラップしているだけで、httpポートで現れるだけなのです。
## beegoはpprofをサポートしています
現在beegoフレームワークはpprofを追加しています。この特徴はデフォルトで起動しません。もし性能をテストしたり、対応するgoroutineの実行といった情報を確認したりする必要があれば、Goのデフォルトパッケージ"/net/http/pprof"にはすでにこの機能があります。例えばGoのデフォルトの方法でWebを実行すれば、デフォルトで使用することができます。しかしbeegoはServHTTP関数をラップしていますので、もしデフォルトのパッケージをそのまま使っているのであればこの機能を起動することはできません。そのため、beegoの内部で改造を施し、pprofをサポートさせる必要があります。
- まずbeego.Run関数において変数によって自動的に機能パッケージをロードするか決定します。
if PprofOn {
BeeApp.RegisterController(`/debug/pprof`, &ProfController{})
BeeApp.RegisterController(`/debug/pprof/:pp([\w]+)`, &ProfController{})
}
- 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)
}
## 使用方法
上の設計を通して、以下のようなコードによってpprofを起動することができます
beego.PprofOn = true
次に、ブラウザで以下のURLを開くと以下のようなインターフェースが現れます
![](images/14.6.pprof.png?raw=true)
図14.7 システムの現在のgoroutine、heap、threadの情報
goroutineをクリックすると詳細な情報を得ることができます
![](images/14.6.pprof2.png?raw=true)
図14.8 現在のgoroutineの詳細情報を表示
コマンドラインから更に多くの詳細な情報を得ることもできます
go tool pprof http://localhost:8080/debug/pprof/profile
この時、プログラムは30秒のprofile収集時間に入ります。この時間内に必死にブラウザ上のページをリロードし、なるべくcpuを専有させてデータを生成します。
(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
![](images/14.6.pprof3.png?raw=true)
図14.9 デモの実行プロセス情報
## links
* [目次](<preface.md>)
* 前へ: [多言語サポート](<14.5.md>)
* 次へ: [まとめ](<14.7.md>)