Come profilare i benchmark usando lo strumento PPROF?
-
21-12-2019 - |
Domanda
Voglio Profilo I miei benchmark generati da go test -c
, ma il go tool pprof
ha bisogno di un file del profilo solitamente generato all'interno della funzione principale come Questo :
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
.
Come posso creare un file di profilo all'interno dei miei benchmark?
Soluzione
Come descritto in http://golang.org/cmd/go/#hdr-description_of_testing_flags può specificare il file del profilo utilizzando il flag -cpuprofile
.
Ad esempio
go test -cpuprofile cpu.out
. Altri suggerimenti
Utilizzare il flag -cpuprofile
in go test
come documentato su http://golang.org/cmd/Vai / # hdr-description_of_testing_flags
Questo post spiega come profilare i parametri di riferimento con un esempio: PROFIGGIO DI PRINCIPARE CON PPROF .
Il seguente benchmark simula un lavoro della CPU.
package main
import (
"math/rand"
"testing"
)
func BenchmarkRand(b *testing.B) {
for n := 0; n < b.N; n++ {
rand.Int63()
}
}
.
Per generare un profilo CPU per il test di riferimento, eseguire:
go test -bench=BenchmarkRand -benchmem -cpuprofile profile.out
.
Per analizzare il profilo Utilizzare lo strumento 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
.
Nota, che qualsiasi codice di inizializzazione prima del ciclo di riferimento sarà profilato.