pprof 도구를 사용하여 벤치마크를 프로파일링하는 방법은 무엇입니까?
-
21-12-2019 - |
문제
나는하고 싶다 프로필 내 벤치마크는 다음에서 생성되었습니다. 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 당신을FLAG
-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
이 경우 병목 현상은 수학/랜드의 기본 소스가 동기화되어 발생하는 뮤텍스입니다.
다른 프로필 프리젠테이션 및 출력 형식도 가능합니다. tree
.유형 help
더 많은 옵션을 확인하세요.
벤치마크 루프 이전의 모든 초기화 코드도 프로파일링됩니다.
제휴하지 않습니다 StackOverflow