NOTE

4.5 trace

Runtime/trace 可以记录 Go 程序的运行轨迹。笔记从引入包和采集数据开始,整理跟踪与查看结果的步骤。

Go创建于 更新于 约 1 分钟读完historical

这是历史学习笔记,可能存在过时或不完整的理解。

1. 使用

步骤如下:

  1. 导入包:import runtime/trace
  2. 开启分析:trace.Start(file) 停止分析:trace.Stop()
package main

import (
	"fmt"
	"os"
	"runtime/trace"
)

func main() {
	file, err := os.Create("trace.out")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	err = trace.Start(file)
	if err != nil {
		panic(err)
	}

	fmt.Println("Hello World")

	trace.Stop()
}
  1. 命令行分析
    • go tool trace trace.out

2. 参考

Go execution tracer | Gopher Academy Blog Go 执行追踪器(execution tracer) - Go语言中文网 - Golang中文社区 An Introduction to go tool trace How to analyze the performance of your go application in production | Developer’s life Go code refactoring : the 23x performance hunt | by Val Deleplace | Medium

讨论

使用 GitHub 账号参与讨论,评论会保存在 GitHub Issues 中。在 GitHub 查看