我想要 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()
}如何在基准测试中创建概要文件?
发布于 2014-04-13 21:14:19
如标志中所述,您可以使用标志-cpuprofile指定配置文件。
例如
go test -cpuprofile cpu.out发布于 2014-04-13 21:13:39
使用-cpuprofile标志到go test,如标志中所记录的
发布于 2018-03-27 09:54:52
这篇文章解释了如何用一个例子来分析基准测试:用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以获得更多选项。
注意,基准测试循环之前的任何初始化代码也将被分析。
https://stackoverflow.com/questions/23048455
复制相似问题