现在,我使用Go工具pprof分析go应用程序,如下所示:
go tool pprof http://localhost:8080/debug/pprof/profile我希望在任意Go进程上使用pprof工具,该进程正在未知端口上运行http服务器。我所掌握的关于这个过程的唯一信息是它的PID。我需要做两件事之一:
go tool pprof 10303。这两种方法都有用吗?
发布于 2015-07-08 00:54:49
服务发现被设计用来解决这样的问题。一个简单的解决方案是为每个PID创建一个tmp文件,将每个端口写入相应的文件。并在需要go tool pprof时读取端口。
http.ListenAndServe("localhost:" + PORT, nil)
tmpFilePath := filePath.Join(os.TempDir(), "myport_" + strconv.FormatInt(os.Getpid(), 10))
f, _ := os.OpenFile(tmpFilePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
f.Write(byte[](strconv.FormatInt(PORT, 10)))
f.Close()在go工具profile中: go工具http://localhost:`cat /tmp/myport_10303`/调试/pprof/profile
没有在实际中测试,可能是一些语法错误
更新:
另一种不更改go源的方法是,使用bash命令(如netstat/lsof )来查找go进程的侦听端口。像这样:
netstat -antp | grep 10303| grep LISTEN | awk '{ print $4 }' | awk -F: '{print $2}'我认为不是最好的bash脚本,仅供参考。
https://stackoverflow.com/questions/31281409
复制相似问题