首页 > 解决方案 > go tool pprof list -- 错过的源代码行

问题描述

在某些代码行中使用list命令go tool pprof有时会丢失。

有函数的开头:

func SlowSearch(out io.Writer) {

    file, err := os.Open(filePath)
    if err != nil {
        panic(err)
    }

    fileContents, err := ioutil.ReadAll(file)
    if err != nil {
        panic(err)
    }

    r := regexp.MustCompile("@")
    seenBrowsers := []string{}
    uniqueBrowsers := 0
    foundUsers := ""

这工作正常:

go test -bench . -cpuprofile bench_cpu.pprof
go tool pprof .\hw3.test.exe .\bench_cpu.pprof

结果:

(pprof) list SlowSearch
Total: 5.12s
ROUTINE ======================== hw3.SlowSearch in C:\Egor\MyProject\learn_mail.ru\3\99_hw\common.go
         0      910ms (flat, cum) 17.77% of Total
         .          .     18:   file, err := os.Open(filePath)
         .          .     19:   if err != nil {
         .          .     20:           panic(err)
         .          .     21:   }

仅缺少功能定义行。


这更糟糕:

go test -bench . -benchmem -cpuprofile bench_cpu.pprof -memprofile bench_mem.pprof -memprofilerate 1

结果:

(pprof) list SlowSearch
Total: 4.40s
ROUTINE ======================== hw3.SlowSearch in C:\Egor\MyProject\learn_mail.ru\3\99_hw\common.go
         0      1.67s (flat, cum) 37.95% of Total
         .          .     28:   r := regexp.MustCompile("@")
         .          .     29:   seenBrowsers := []string{}
         .          .     30:   uniqueBrowsers := 0
         .          .     31:   foundUsers := ""

错过了更多的源代码行。


Golang 版本:

go version go1.16.3 windows/amd64

我试图删除所有二进制文件并重建 - 结果相同。命令list, weblist, 带有参数的 web 界面-http给出相同的结果,但在源代码中缺少相同的行。


如何返回丢失的函数行?

标签: gopprof

解决方案


推荐阅读