为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
2017-10-18 10:45
411 查看
pprof是个神马玩意儿?
pprof - manual page for pprof (part of gperftools)
是gperftools工具的一部分
gperftools又是啥?
These tools are for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.
一个性能分析的工具,可以查看堆栈、cpu信息等等。
在golang中如何使用呢?下面就来看看。
1. 在一个httpserver中,只需要一行代码
2.编译运行这个程序
go build trace_example.go
./trace_example
3. 通过浏览器访问
http://localhost:6060/debug/pprof/
能够查看到程序的overview
4.你也可以通过终端命令查看
Then use the pprof tool to look at the heap profile:
Or to look at a 30-second CPU profile:
Or to look at the goroutine blocking profile:
-------------------------------------
2016.11.14 updated
基于uninx domain socket 的agent,可以不用走http
https://github.com/google/gops
pprof - manual page for pprof (part of gperftools)
是gperftools工具的一部分
gperftools又是啥?
These tools are for use by developers so that they can create more robust applications. Especially of use to those developing multi-threaded applications in C++ with templates. Includes TCMalloc, heap-checker, heap-profiler and cpu-profiler.
一个性能分析的工具,可以查看堆栈、cpu信息等等。
在golang中如何使用呢?下面就来看看。
1. 在一个httpserver中,只需要一行代码
package main import ( "flag" "log" "net/http" _ "net/http/pprof" "sync" "time" ) func main() { flag.Parse() //这里实现了远程获取pprof数据的接口 go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() var wg sync.WaitGroup wg.Add(10) for i := 0; i < 10; i++ { go work(&wg) } wg.Wait() // Wait to see the global run queue deplete. time.Sleep(3 * time.Second) } func work(wg *sync.WaitGroup) { time.Sleep(time.Second) var counter int for i := 0; i < 1e10; i++ { time.Sleep(time.Millisecond * 100) counter++ } wg.Done() }
2.编译运行这个程序
go build trace_example.go
./trace_example
3. 通过浏览器访问
http://localhost:6060/debug/pprof/
能够查看到程序的overview
4.你也可以通过终端命令查看
Then use the pprof tool to look at the heap profile:
go tool pprof http://localhost:6060/debug/pprof/heap
Or to look at a 30-second CPU profile:
go tool pprof http://localhost:6060/debug/pprof/profile
Or to look at the goroutine blocking profile:
go tool pprof http://localhost:6060/debug/pprof/block
qpzhang@qpzhangdeMac-mini:~/gocode $go tool pprof http://localhost:6060/debug/pprof/profile Read http://localhost:6060/debug/pprof/symbol Gathering CPU profile from http://localhost:6060/debug/pprof/profile?seconds=30 for 30 seconds to /var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/mmBnpdkv0D Be patient... Wrote profile to /var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/mmBnpdkv0D Welcome to pprof! For help, type 'help'. (pprof) top10 Total: 9 samples 6 66.7% 66.7% 6 66.7% runtime.mach_semaphore_wait 1 11.1% 77.8% 1 11.1% runtime.kevent 1 11.1% 88.9% 1 11.1% runtime.mach_semaphore_signal 1 11.1% 100.0% 1 11.1% runtime.mach_semaphore_timedwait 0 0.0% 100.0% 7 77.8% System 0 0.0% 100.0% 1 11.1% notetsleep 0 0.0% 100.0% 1 11.1% ready 0 0.0% 100.0% 2 22.2% runtime.gosched0 0 0.0% 100.0% 1 11.1% runtime.mach_semrelease 0 0.0% 100.0% 1 11.1% runtime.notetsleepg (pprof) web Total: 9 samples Loading web page file:////var/folders/2l/195zcc1n0sn2wjfjwf9hl9d80000gn/T/SlFUZhzBDB.0.svg
web命令生成了很cool的svg图片,在浏览器中打开。
除了远程拉取,你还可以自己编写代码,把这些数据写入文件(实现 net/http/pprof 中的几个路由,开启或关闭追踪信息)。 具体可参考:http://blog.golang.org/profiling-go-programs https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md 期间:web命令依赖一个svg生成包,在mac下面,你可以通过http://www.graphviz.org/Download_macos.php进行下载。
-------------------------------------
2016.11.14 updated
基于uninx domain socket 的agent,可以不用走http
https://github.com/google/gops
相关文章推荐
- 为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
- 为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息
- pprof远程查看httpserver运行堆栈,cpu耗时等信息
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
- HDFS设计思路,HDFS使用,查看集群状态,HDFS,HDFS上传文件,HDFS下载文件,yarn web管理界面信息查看,运行一个mapreduce程序,mapreduce的demo
- 查看程序运行时间,gprof使用详细介绍
- 使用nohup让程序在远程主机后台运行
- C#实现测量程序运行时间及cpu使用时间(转载)
- 使用profile来得到程序运行信息
- 修改桌面堆栈大小,解决运行大量程序时出现”Out of Memory”(内存不足)错误信息的问题
- at 命令来安排命令、脚本或程序在指定的日期和时间运行。您也可以使用此命令查看现有的计划任务。
- 服务器上出现应用程序错误。此应用程序的当前自定义错误设置禁止远程查看应用程序错误的详细信息(出于安全原因)。但可以通过在本地服务器计算机上运行的浏览器查看。
- C#实现测量程序运行时间及cpu使用时间
- 以字符串形式得到程序运行堆栈信息
- C#实现测量程序运行时间及cpu使用时间
- 修改桌面堆栈大小,解决运行大量程序时出现”Out of Memory”(内存不足)错误信息的问题
- 使用nohup让程序在远程主机后台运行
- C#测量程序运行时间及cpu使用时间
- delphi下使用jclDebug,在运行时显示详细的调试堆栈信息的范例
- C#实现测量程序运行时间及cpu使用时间