go 1.5的trace命令

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

The go 1.5 trace command

问题

Go 1.5发布说明中提到:

> 新的"go tool trace"命令可以可视化运行时生成的程序跟踪信息。

这真的很令人兴奋,我想了解更多。但是官方文档https://golang.org/cmd/trace/非常枯燥。

发现Rob Pike对此提出了抱怨,要求在"1.5发布后不久应该有一篇关于该功能的博文"。

如果有人发布/发现了这样的博文,请在这里添加链接。或者,如果你愿意在这里直接回答,也非常欢迎。

谢谢

英文:

The Go 1.5 release note says,

> The new "go tool trace" command enables the visualisation of program traces generated by new tracing infrastructure in the runtime.

This is really exciting, and I want to know more about it. But its official document at https://golang.org/cmd/trace/ is really dry.

Found that Rob Pike complained about it, requesting that "shortly after the 1.5 release, there should be a blog post about the feature."

If anyone has posted/spotted such a blog, please add a link here. Or, if you like to answer directly here, it is also welcome as well.

Thanks

答案1

得分: 16

有两种方法可以生成跟踪文件。

方法1

  1. 在程序的开头添加以下代码:

     f, err := os.Create(time.Now().Format("2006-01-02T150405.pprof"))
     if err != nil {
         panic(err)
     }
     defer f.Close()
    
     if err := trace.Start(f); err != nil {
         panic(err)
     }
     defer trace.Stop()
    
  2. 构建程序

    go build

  3. 运行你的程序(例如 ./myprogram

  4. 运行go trace。

    go tool trace myprogram 2015-08-21T115354.pprof

方法2

  1. 编写一个利用testing包的测试函数。

  2. 使用跟踪标志运行测试。

    go test -trace trace.out

  3. 使用生成的.test和.out文件运行跟踪工具。

    go tool trace pkg.test trace.out

无论哪种方法,你的浏览器都会打开类似下面的页面

go 1.5的trace命令
go 1.5的trace命令
go 1.5的trace命令

英文:

There are two ways you can generate trace files.

Method 1

  1. Add following line at the start of your program

     f, err := os.Create(time.Now().Format("2006-01-02T150405.pprof"))
     if err != nil {
     	panic(err)
     }
     defer f.Close()
    
     if err := trace.Start(f); err != nil {
     	panic(err)
     }
     defer trace.Stop()
    
  2. build the program
    > go build

  3. Run your program (for example ./myprogram)

  4. Run go trace.
    > go tool trace myprogram 2015-08-21T115354.pprof

Method 2

  1. Write a testing function utilizing testing package.

  2. Run Test with trace flag
    > go test -trace trace.out

  3. Run trace tool with generated .test and .out file
    > go tool trace pkg.test trace.out

In both cases your browser will open something like this

go 1.5的trace命令
go 1.5的trace命令
go 1.5的trace命令

huangapple
  • 本文由 发表于 2015年8月21日 10:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/32131339.html
匿名

发表评论

匿名网友

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

确定