PPROFツールを使用してベンチマークをプロファイルする方法
-
21-12-2019 - |
質問
profile> go test -c
によって生成された私のベンチマークを望みますが、go tool pprof
には通常、この:
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
.
ベンチマーク内にプロファイルファイルを作成する方法は?
解決
http://golang.org/cmd/go/#hdr-description_of_testing_flags フラグ
-cpuprofile
を使用してプロファイルファイルを指定できます。
例えば
ですgo test -cpuprofile cpu.out
. 他のヒント
-cpuprofile
フラグを http://golang.org/cmd/GO /#hdr-description_of_testing_flags
この記事では、ベンチマークをプロファイルする方法について説明します。 PPROFを使用したベンチマークプロファイリング。
package main
import (
"math/rand"
"testing"
)
func BenchmarkRand(b *testing.B) {
for n := 0; n < b.N; n++ {
rand.Int63()
}
}
.
ベンチマークテストのCPUプロファイルを生成するには、実行します。
go test -bench=BenchmarkRand -benchmem -cpuprofile profile.out
.
-memprofile
と-blockprofile
フラグを使用して、メモリ割り当てとコールプロファイルのブロックを生成できます。
go tool pprof profile.out
(pprof) top
Showing nodes accounting for 1.16s, 100% of 1.16s total
Showing top 10 nodes out of 22
flat flat% sum% cum cum%
0.41s 35.34% 35.34% 0.41s 35.34% sync.(*Mutex).Unlock
0.37s 31.90% 67.24% 0.37s 31.90% sync.(*Mutex).Lock
0.12s 10.34% 77.59% 1.03s 88.79% math/rand.(*lockedSource).Int63
0.08s 6.90% 84.48% 0.08s 6.90% math/rand.(*rngSource).Uint64 (inline)
0.06s 5.17% 89.66% 1.11s 95.69% math/rand.Int63
0.05s 4.31% 93.97% 0.13s 11.21% math/rand.(*rngSource).Int63
0.04s 3.45% 97.41% 1.15s 99.14% benchtest.BenchmarkRand
0.02s 1.72% 99.14% 1.05s 90.52% math/rand.(*Rand).Int63
0.01s 0.86% 100% 0.01s 0.86% runtime.futex
0 0% 100% 0.01s 0.86% runtime.allocm
.
この場合のボトルネックは、Math / Randのデフォルトのソースが同期されていることによって発生したミューテックスです。
他のプロファイルのプレゼンテーションと出力フォーマットも可能です。tree
。より多くのオプションのためにhelp
を入力してください。
所属していません StackOverflow