go 1.5 跟踪命令

The go 1.5 trace command

Go 1.5 发行说明说,

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

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

发现Rob Pike complained about it,请求"shortly after the 1.5 release, there should be a blog post about the feature."

如果谁有posted/spotted这样的博客,请在这里加一个link。或者,如果你喜欢在这里直接回答,也欢迎。

谢谢

您可以通过两种方式生成跟踪文件。

方法一

  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 tool trace myprogram 2015-08-21T115354.pprof

方法二

  1. 使用测试包编写测试函数。
  2. 运行 使用跟踪标志测试

    go test -trace trace.out

  3. 运行 带有生成的 .test 和 .out 文件的跟踪工具

    go tool trace pkg.test trace.out

在这两种情况下,您的浏览器都会打开类似这样的内容