質問

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
.

他のヒント

この記事では、ベンチマークをプロファイルする方法について説明します。 PPROFを使用したベンチマークプロファイリング

次のベンチマークは、一部のCPU作業をシミュレートします。

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ツールを使用します:

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を入力してください。

ベンチマークループの前の初期化コードもプロファイルされることに注意してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top