无法使用Go Profile对代码进行分析。

huangapple go评论86阅读模式
英文:

Unable to Profile Code Using Go Profile

问题

我正在尝试通过按照Go博客中的示例来使用Go Pro filer。我不确定我做错了什么。但是我的分析生成的输出显示0个样本。这很奇怪。

以下是我的代码:

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("发生错误")
		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("发生错误")
         fmt.Printf("%s", err)
    	}
    	fmt.Printf("%s", out)

}

你可以在这里找到更多关于Go程序性能分析的信息。

英文:

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

得分: 3

您的程序运行时间不足以让分析器捕获任何分析样本。
基本上,分析器定期查看程序的状态(执行的代码是什么,是哪个函数等)。如果程序终止得比查找状态的例程更快,那么就不会采样状态,因此最终没有样本可供查看。

这就是您遇到的情况。

解决方法之一是将分析器的采样率设置为更高的值,另一种方法是让您的程序执行一些耗时的操作。例如:

 f, _ := os.Create("cpuprofile")

 pprof.StartCPUProfile(f)
 defer pprof.StopCPUProfile()

 for i := 0; i < 10; i++ {
      time.Sleep(1 * time.Second)
 }

或者,当尝试找出代码中的问题时,您可以编写一个基准测试并对该基准进行分析。

英文:

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(&quot;cpuprofile&quot;)

 pprof.StartCPUProfile(f)
 defer pprof.StopCPUProfile()

 for i := 0; i &lt; 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.

huangapple
  • 本文由 发表于 2013年12月8日 09:17:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/20448738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定