記事のディレクトリ
モジュールpprof
主に以下の分野でのパフォーマンスの最適化のGo言語プロジェクト:
- CPUプロファイル:CPU使用状況レポートプログラム、CPUレジスタ上で収集したデータやアプリケーションプログラムに特定の周波数に応じて
- メモリ使用量の報告プロセス:メモリプロファイル(ヒーププロファイル)
- ブロックプロファイリング:レポートが実行されていないゴルーチン、および分析し、パフォーマンスのボトルネックのデッドロックを見つけるために使用することができます
- ゴルーチンプロファイリング:ゴルーチン、ゴルーチンの使用に関する報告書は、彼らの関係は、呼び出しのようなものです
ゴー言語は、組み込みの次の二つの標準ライブラリを含むプログラムの運用データを取得するためのツール:
runtime/pprof
:収集ツールを実行しているアプリケーションは、データを分析しますnet/http/pprof
:データ収集サービスアプリケーションランタイムの分析
、これらのデータサンプルを分析パフォーマンス分析レポートを形成することにより、最終的に、オープンpprof後、(10ミリ秒)の関数CPUとメモリリソースが占有フォーマット取得現在のスタック情報に一定の間隔で収集されます。
パフォーマンステストするとき、我々はコードのみでpprofまで導入されるべきであることに注意してください。
準備ツール
グラフィカルなツール
- インストールキットpprof
go get -u github.com/google/pprof
通常の使用の分析go tool pprof
このパッケージをインストールする必要はありません。あなたは炎のチャートが必要な場合でも、それはまだあるパッケージジョブpprofインストールされています。
- Graphvizのインストールは、サポートするために、炎図
サーバー側でこのインストールを、各オペレーティング・システムは、パッケージ管理ツールを使用するか、ソースのインストールを選択することができます。
brew install graphviz # for macos
apt-get install graphviz # for ubuntu
yum install graphviz # for centos
あなたは、サーバーをインストールするように要求されているのはなぜ
サーバとクライアントが同じマシンでこの問題に存在しない場合は
、しかし、サービスとクライアントが同じマシン上にない場合、あなたはgraphvizのインストール場所を知っている必要があります
圧力測定ツール
ない、必要であれば、あなたが使用することができ、詳細に説明し
https://github.com/wg/wrkまたはhttps://github.com/adjust/go-wrk
ゴートーチ
ユーバーゴートーチは、主炎、図のために、オープンソースツールです。公式住所:https://github.com/uber/go-torch
しかしゴー1.11、難グラフィックの可視化からgo tool pprof
直接入手できます!だから、行く-トーチは放棄されていると推奨アーカイブがgo tool pprof
ツールアプリケーション
あなたのアプリケーションは、出射端種類にいくつかの時間のために実行されている場合。アプリケーションの終了は、分析用のファイルにプロファイリングレポートを保存するときの最良の方法であるので。このケースでは、ランタイム/ pprofライブラリを使用することができます。最初は、コード内でランタイム/ pprofツールを導入しました:
import "runtime/pprof"
オンラインのサンプルコード:
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
)
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file.")
)
func main() {
log.Println("begin")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f) // 开启CPU性能分析
defer pprof.StopCPUProfile() // 停止CPU性能分析
}
for i := 0; i < 30; i++ {
nums := fibonacci(i)
fmt.Println(nums)
}
}
func fibonacci(num int) int {
if num < 2 {
return 1
}
return fibonacci(num-1) + fibonacci(num-2)
}
これら二つは、CPUのパフォーマンスを分析することに注意してください。
pprof.StartCPUProfile(f) // 开启CPU性能分析
defer pprof.StopCPUProfile() // 停止CPU性能分析
外出先test.goという名前のファイルを作成し、上記のコードを記述し、コマンドを実行しますgo build test.go
。プログラムを実行している場合は追加--cpuprofile
たとえば、パラメータを./test --cpuprofile=cpu.prof
。
このようなプログラムは、CPUプロファイリングデータを実行しているときに記録されますcpu.prof
で、その後、あなたは使用することができるgo tool pprof
ツールにCPUのパフォーマンス分析を。
メモリ性能の使用を最適化:
pprof.WriteHeapProfile(w io.Writer)
サービスアプリケーション
アプリケーションが常に実行されている場合は、そのようなWebアプリケーションとして、あなたが使用できるnet/http/pprof
ライブラリを、それがHTTPを提供するサービスで分析することができます。
デフォルトの場合http.DefaultServeMux
(通常のコードは、直接http.ListenAndServe(“0.0.0.0:8000”, nil)
、ちょうどインポートなどのウェブサーバー側のコードに次のnet/http/pprof
import _ "net/http/pprof" //注意下划线
オンラインのサンプルコード:
package main
import (
_ "net/http/pprof"
"net/http"
)
func main() {
http.ListenAndServe("0.0.0.0:8000", nil)
}
非常に単純な、ファイル名はtest.go
、直接実行します。
go run test.go
あなたのサービスは、余分なHTTPのでしょう/debug/pprof
エンドポイント、その後、ブラウザでアクセスし、localhost
独自のサーバーのIPに置き換えることができます。
http://localhost:8082/debug/pprof/
このパスの下にいくつかのサブページがあります。
タイプ | 説明 | リマーク |
---|---|---|
その代わり | のサンプリング情報メモリの割り当て | 開いているが、高くはない可読性にブラウザを使用することができます |
ブロック | 動作条件を遮断するサンプリング情報 | 開いているが、高くはない可読性にブラウザを使用することができます |
CMDLINE | ディスプレイの起動コマンドとパラメータ | あなたは、ブラウザを開くことができます |
ゴルーチン | すべてのコルーチンの現在のスタック情報 | 開いているが、高くはない可読性にブラウザを使用することができます |
ヒープ | サンプル情報のヒープメモリ使用量 | 開いているが、高くはない可読性にブラウザを使用することができます |
ミューテックス | ロック競合の状況と情報をサンプリング | 開いているが、高くはない可読性にブラウザを使用することができます |
プロフィール | CPUの使用状況をサンプリング、30代を続け、 | ブラウザを開いてファイルをダウンロードします |
threadcreate | サンプリング情報システムスレッド作成状況 | 開いているが、高くはない可読性にブラウザを使用することができます |
痕跡 | 実行トレース情報 | ブラウザを開いてファイルをダウンロードします |
ジンフレームワークは、pprofを使用しています
使用できる2つのパッケージがあります。
package main
import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
pprof.Register(router)
router.Run(":8080")
}
package main
import (
"github.com/gin-gonic/gin"
"github.com/DeanThompson/ginpprof"
)
func main() {
router := gin.Default()
ginpprof.Wrap(router)
router.Run(":8080")
}
注:
あなたは方法はコメントに必要な、またはブラウザがページのダウンロードをトリガーする開きrouter.Use(gzip.Gzip(gzip.DefaultCompression))および他の同様の機能を使用する場合
そして、パフォーマンスが結合pprofについて試験しました
go test
コマンドは、2つのパラメータを取り、関連pprof、それらはCPUとメモリが保存されたファイルをプロファイリング生成するように指定されています。
- cpuprofile:CPUプロファイリングデータファイルを保存するアドレス
- memprofile:メッセージファイルアドレスへのメモリプロファイリングデータを
我々はまた、例えば、pprofとパフォーマンステストを組み合わせるために選択することができます。
次のテストを実行している間、CPUプロファイリングを実行し、cpu.profファイルに結果を格納します。
go test -bench . -cpuprofile=cpu.prof
テストを行いながら、以下、Memのプロファイリングを行い、cpu.profファイルに結果を格納します。
go test -bench . -memprofile=./mem.prof
上側の2つの発生の例.prof
文書がすることであるgo tool pprof
使用することができます。
プロファイリングおよびパフォーマンステストで一般的に使用して、プロファイル内のアプリケーションが唯一の高負荷条件下で理にかなって、注意してください。
コマンドpprofツールを行きます
入門
以前の論文のコレクションを生成した大量のデータを書きましたが、このデータファイルを使用する方法を書いていません。
このファイルは、直接開いていない、あなたが必要とすることができ、バイナリファイルでgo tool pprof
解析するためのコマンド。
塗布後、楽器やサービスのアプリケーションかどうか、私たちはデータを取得するために、適切なpprofライブラリを使用して、次のステップは、我々が使用することができ、これらのデータを分析することであるべきgo tool pprof
コマンドラインツールを。
go tool pprof
最も簡単な方法は、使用することです。
go tool pprof [binary] [source]
どこで:
- アプリケーションのバイナリは、シンボルを解決するために、バイナリファイルです。
- ソースは、プロファイルデータのソースを表し、ローカルファイルをすることができ、それは、httpアドレスにすることができます。
注:データ収集をプロファイリングは、アプリケーションが(アクセスアナログ圧力にサービスが実行中に生成されるような、または他の手段によって)大きな負荷であることを保証するために、有効なデータを得るために、動的です。アプリケーションがアイドル状態の場合はそうでなければ、結果は何の意味も持たない可能性があります。
グラフィカル表示
私は今使用して、Webサービスのフレームワークを有効ジン使用していますgithub.com/gin-contrib/pprof
パケット解析を。
このセクションでは、外出先のツールpprofの使用に焦点を当て、そしてより多くのプロジェクトコード、コードが掲載されていません。あなたがテストに合わせて、自分の目の前で話すようにしたい場合は行うことができます。
$ go run main.go
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /debug/pprof/ --> github.com/DeanThompson/ginpprof.IndexHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/heap --> github.com/DeanThompson/ginpprof.HeapHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/goroutine --> github.com/DeanThompson/ginpprof.GoroutineHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/allocs --> github.com/DeanThompson/ginpprof.AllocsHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/block --> github.com/DeanThompson/ginpprof.BlockHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/threadcreate --> github.com/DeanThompson/ginpprof.ThreadCreateHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/cmdline --> github.com/DeanThompson/ginpprof.CmdlineHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/profile --> github.com/DeanThompson/ginpprof.ProfileHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers)
[GIN-debug] POST /debug/pprof/symbol --> github.com/DeanThompson/ginpprof.SymbolHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/trace --> github.com/DeanThompson/ginpprof.TraceHandler.func1 (3 handlers)
[GIN-debug] GET /debug/pprof/mutex --> github.com/DeanThompson/ginpprof.MutexHandler.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
私はあなたのシステムが自動的に多くのAPIを/デバッグ/ pprofを追加することを確認することができ、サービスのデバッグモードが有効にされて開発するために使用されます。
次に、あなたのブラウザーで開く/debug/pprof
:
このページはあなたに不慣れであってはなりません。はい、これは典型的なサービス指向アプリケーションの分析です。
以下は、他の用途について説明します。
私たちは、新しいターミナルウィンドウを作成し、すべてのコルーチンのために、次のコマンドは、現在のスタック情報を入力します。
go tool pprof --seconds 20 http://localhost:8080/debug/pprof/goroutine
この手段は、データの取得と、CPUが20代のための
別の方法を使用することができます。
go tool pprof http://localhost:8080/debug/pprof/goroutine?second=20
結果を見てください:
$ go tool pprof --seconds 20 http://localhost:8080/debug/pprof/goroutine
Fetching profile over HTTP from http://localhost:8080/debug/pprof/goroutine?seconds=20
Please wait... (20s)
Saved profile in /home/server/pprof/pprof.main.goroutine.001.pb.gz
File: main
Type: goroutine
Time: Dec 18, 2019 at 5:48pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
実際には、我々は今、コマンドラインを入力して、最後の行(pprof)
の入力がweb
自動的にローカルブラウザを開いて、ページを訪問し、当然のことながら、このようなので、上のMacOSのは、Ubuntu、Windowsとなどのグラフィカル・ユーザー・インターフェース・システムに限定されています。お使いのオペレーティングシステムがない場合は、以下の結果である可能性があります。
(pprof) web
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
(pprof)
そして、4行目の先頭に、してください注意を払う、それを行う方法をSaved profile
、後ろの/home/server/pprof/pprof.main.goroutine.001.pb.gz
私たちに必要なファイルです。このマニュアルを使用するには?
私たちは、新しいターミナルウィンドウを作成し、次のコマンドを入力します。
go tool pprof -http=0.0.0.0:8081 /home/server/pprof/pprof.main.goroutine.001.pb.gz
この時点で新しいポート(8081)を必要とし、同じポートが(8080)の前にすることはできません。
次に、あなたのブラウザで直接アクセスするhttp://localhost:8081/
IP変更するために、有料の注意を:
あなたがインストールしていない場合はgraphviz
、存在しますCould not execute dot; may need to install graphviz
。
この時点では、グラフィカル関数呼び出しスタック情報を参照してください。右上のVIEW
バーいくつかのオプションがあり、あなたがオープンに各ポイントを見てみることができ、私は一つ一つを紹介しますが、第三はありませんFlame Graph
、それはあるので、あなたがそれを好きすべき難チャート:
コマンドラインインタフェース
次に、簡単な紹介go tool pprof
、コマンドライン、我々はあるコマンドを、遭遇したものを使用しましたweb
。このセクションでは、いくつかの一般的に使用されるコマンドを紹介します。
上
トップデフォルトのビューアは、最初の10ビットCPUの機能を占めている
三つのプログラム占有する前にCPUの機能を見ることができますTOP3を:
(pprof) top
Showing nodes accounting for 5, 100% of 5 total
Showing top 10 nodes out of 53
flat flat% sum% cum cum%
3 60.00% 60.00% 3 60.00% runtime.gopark
1 20.00% 80.00% 1 20.00% runtime/pprof.writeRuntimeProfile
1 20.00% 100% 1 20.00% syscall.Syscall
0 0% 100% 1 20.00% bufio.(*Reader).ReadLine
0 0% 100% 1 20.00% bufio.(*Reader).ReadSlice
0 0% 100% 1 20.00% bufio.(*Reader).fill
0 0% 100% 1 20.00% github.com/DeanThompson/ginpprof.GoroutineHandler.func1
0 0% 100% 1 20.00% github.com/gin-gonic/gin.(*Context).Next
0 0% 100% 1 20.00% github.com/gin-gonic/gin.(*Engine).Run
0 0% 100% 1 20.00% github.com/gin-gonic/gin.(*Engine).ServeHTTP
(pprof) top3
Showing nodes accounting for 5, 100% of 5 total
Showing top 3 nodes out of 53
flat flat% sum% cum cum%
3 60.00% 60.00% 3 60.00% runtime.gopark
1 20.00% 80.00% 1 20.00% runtime/pprof.writeRuntimeProfile
1 20.00% 100% 1 20.00% syscall.Syscall
どこで:
- フラット:現在の関数のCPUを消費します
- 現在の関数のフラット::消費するCPU-割合
- 日%:占有の累積百分率CPUの時間のかかる機能
- 兼:一緒にCPU-合計時間の現在の関数と呼ばれ、現在の関数
- 合計時間を加えたCPU-の現在の関数と呼ばれる現在の関数の割合:兼%
- 最後の1:関数名
ほとんどの場合、我々はこれらの5つのアプリケーション操作の結果、およびプログラムの最適化を分析することができます。
リスト
また、使用することができlist
、このような実行など、ビューの特定の解析関数に関数名のコマンドをlist gopark
我々は書き機能の詳細な分析を参照してください。
(pprof) list gopark
Total: 5
ROUTINE ======================== runtime.gopark in /usr/local/go/src/runtime/proc.go
3 3 (flat, cum) 60.00% of Total
. . 297: gp.waitreason = reason
. . 298: mp.waittraceev = traceEv
. . 299: mp.waittraceskip = traceskip
. . 300: releasem(mp)
. . 301: // can't do anything that might move the G between Ms here.
3 3 302: mcall(park_m)
. . 303:}
. . 304:
. . 305:// Puts the current goroutine into a waiting state and unlocks the lock.
. . 306:// The goroutine can be made runnable again by calling goready(gp).
. . 307:func goparkunlock(lock *mutex, reason waitReason, traceEv byte, traceskip int) {
ROUTINE ======================== runtime.goparkunlock in /usr/local/go/src/runtime/proc.go
0 1 (flat, cum) 20.00% of Total
. . 303:}
. . 304:
. . 305:// Puts the current goroutine into a waiting state and unlocks the lock.
. . 306:// The goroutine can be made runnable again by calling goready(gp).
. . 307:func goparkunlock(lock *mutex, reason waitReason, traceEv byte, traceskip int) {
. 1 308: gopark(parkunlock_c, unsafe.Pointer(lock), reason, traceEv, traceskip)
. . 309:}
. . 310:
. . 311:func goready(gp *g, traceskip int) {
. . 312: systemstack(func() {
. . 313: ready(gp, traceskip, true)
(pprof)
PDFファイルPDFファイルの可視化が生成されることがあります。
(pprof) pdf
Generating report in profile001.pdf
(pprof)
profile001.pdf
ファイルがカレントディレクトリにあります
助けて
Pprofヘルプは、他のファイル形式を実行するコマンドの説明を参照してください。
(pprof) help
Commands:
callgrind Outputs a graph in callgrind format
comments Output all profile comments
disasm Output assembly listings annotated with samples
dot Outputs a graph in DOT format
eog Visualize graph through eog
evince Visualize graph through evince
gif Outputs a graph image in GIF format
gv Visualize graph through gv
kcachegrind Visualize report in KCachegrind
list Output annotated source for functions matching regexp
pdf Outputs a graph in PDF format
peek Output callers/callees of functions matching regexp
png Outputs a graph image in PNG format
proto Outputs the profile in compressed protobuf format
ps Outputs a graph in PS format
raw Outputs a text representation of the raw profile
svg Outputs a graph in SVG format
tags Outputs all tags in the profile
text Outputs top entries in text form
top Outputs top entries in text form
topproto Outputs top entries in compressed protobuf format
traces Outputs all profile samples in text form
tree Outputs a text rendering of call graph
web Visualize graph through web browser
weblist Display annotated source in a web browser
o/options List options and their current values
quit/exit/^D Exit pprof
参照
pprofチューニング行く
golang pprof戦闘
pprof(golangのパフォーマンスの監視と分析)
golangチューニング(ゴートーチを、ツールpprofを行きます )