dphphvs496524 2013-12-08 01:17
浏览 43

无法使用Go Profile解析代码

I am trying to get a hang of Go Pro filer by following the example in go blog . I am not sure what I am doing wrong. But my profiled generated output shows 0 samples. Its weird.

rahul@g3ck0:~/programs/go$ go tool pprof parallel cpuprofile 
Welcome to pprof!  For help, type 'help'.
(pprof) top5  
Total: 0 samples

The following is my code :

package main

import (
    "fmt"
    "os/exec"
    "sync"
    "strings"
    "runtime/pprof"
    "os"
)

func exe_cmd(cmd string, wg *sync.WaitGroup) {

    out, err := exec.Command(cmd).Output()
    if err != nil {
        fmt.Println("error occured")
        fmt.Printf("%s", err)
    }
    fmt.Printf("%s", out)

    wg.Done()
}

func main() {
     f, _ := os.Create("cpuprofile")
     pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    cmd := "echo newline >> blah.txt"
    parts := strings.Fields(cmd)
    head := parts[0]
    parts = parts[1:len(parts)]
    out, err := exec.Command(head,parts...).Output()
    if err != nil {
         fmt.Println("error occured")
         fmt.Printf("%s", err)
        }
        fmt.Printf("%s", out)

}
  • 写回答

1条回答 默认 最新

  • douleng3463 2013-12-08 04:29
    关注

    Your profiled program runs not long enough for the profiler to pick up any profiling sample. Basically the profiler looks periodically at the state of your program (which code is executed, what function is that, ...). If the program terminates faster than the routine that looks for a status then no status is sampled and, thus, there are no samples to look at in the end.

    This is what happens for you.

    One solution is set the sample rate of the profiler to a higher value, the other way is to have your program actually do something that takes longer. For example:

     f, _ := os.Create("cpuprofile")
    
     pprof.StartCPUProfile(f)
     defer pprof.StopCPUProfile()
    
     for i := 0; i < 10; i++ {
          time.Sleep(1 * time.Second)
     }
    

    Alternatively, when trying to figure out what is wrong with a isolated portion of your code, you can write a benchmark and profile that benchmark.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算