Go分析个人资料为空

Gat*_*ito 6 memory go

我正在按照Go分析中的本教程进行操作,并按照建议进行了操作:

flag.Parse()
if *cpuprofile != "" {
    f, err := os.Create(*cpuprofile)
    if err != nil {
        log.Fatal(err)
    }
    pprof.StartCPUProfile(f)
    defer pprof.StopCPUProfile()
}
Run Code Online (Sandbox Code Playgroud)

然后,我从标志开始代码,-cpuprofile=myprogram.prof并创建了文件。然后,我开始了pprof tool

go tool pprof myprogram myprogram.prof
Run Code Online (Sandbox Code Playgroud)

好吧,myprogram读取一个大的json文件并将其映射到一个大的map [string] string,所以程序中发生了很多事情,但是当我喜欢top10in时pprof,会得到:

Entering interactive mode (type "help" for commands)
(pprof) top10
profile is empty
Run Code Online (Sandbox Code Playgroud)

Woj*_*rek 5

很可能您的代码执行得太快,即使您认为它做了很多事情。我身上发生过好几次。

您可以通过runtime.SetCPUProfileRate更改采样率。- 将其设置为默认值100以上,单位为Hz。请注意,Go 作者不建议使用高于 500 的值 -请参阅解释

就在之前做pprof.StartCPUProfile。您还会看到警告runtime: cannot set cpu profile rate until previous profile has finished- 请参阅此答案以获取解释。

华泰