代码之家  ›  专栏  ›  技术社区  ›  Geo

在启用pkg/profile的情况下运行go时如何获取示例?

  •  0
  • Geo  · 技术社区  · 6 年前

    在我的主要版块中有以下内容:

    func main() {
        defer profile.Start().Stop()
    
        fmt.Println("running version", version, "built on", date)
        fmt.Println()
        cmd.Execute()
    
        time.Sleep(2 * time.Second)
    }
    

    哪里 cmd 是一个cobra子命令。我做了一个go构建,然后运行二进制文件。我可以看到它产生了 pprof 文件:

    2018/09/13 18:43:26 profile: cpu profiling enabled, /tmp/profile705487093/cpu.pprof
    ... output deleted ...
    2018/09/13 18:43:31 profile: cpu profiling disabled, /tmp/profile705487093/cpu.pprof
    

    go tool pprof /root/code/debug/evented /tmp/profile705487093/cpu.pprof
    

    File: evented
    Type: cpu
    Time: Sep 13, 2018 at 6:43pm (UTC)
    Duration: 5.49s, Total samples = 0
    

    go version go1.11 linux/amd64 Ubuntu 16.04.5 LTS . 我不确定这是否重要,但我正试图检查数字海洋水滴的输出。

    我做错什么了吗?谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Geo    6 年前

    在浏览了一下profile pkg的注释之后,我通过以下操作获得了一些示例:

    runtime.SetCPUProfileRate(5000)
    

    打电话之前 defer profile.Start().Stop() 线路。