Go的pprof使用

go中有pprof包来做代码的性能监控,在两个地方有包:

net/http/pprof
runtime/pprof

其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来

pprof包

web 服务器

如果你的go程序是用http包启动的web服务器,你想查看自己的web服务器的状态。这个时候就可以选择net/http/pprof。你只需要引入包_"net/http/pprof",

然后就可以在浏览器中使用http://localhost:port/debug/pprof/直接看到当前web服务的状态,包括CPU占用情况和内存使用情况等。具体使用情况你可以看godoc的说明。

服务进程

如果你的go程序不是web服务器,而是一个服务进程,那么你也可以选择使用net/http/pprof包,同样引入包net/http/pprof,然后在开启另外一个goroutine来开启端口监听。

比如:

go func() {
        log.Println(http.ListenAndServe("localhost:6060", nil))
}()

应用程序

如果你的go程序只是一个应用程序,比如计算fabonacci数列,那么你就不能使用net/http/pprof包了,你就需要使用到runtime/pprof。具体做法就是用到pprof.StartCPUProfile和pprof.StopCPUProfile。比如下面的例子:

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }
…

运行程序的时候加一个--cpuprofile参数,比如fabonacci --cpuprofile=fabonacci.prof

这样程序运行的时候的cpu信息就会记录到XXX.prof中了。

下一步就可以使用这个prof信息做出性能分析图了(需要安装graphviz)。

使用go tool pprof (应用程序) (应用程序的prof文件)

进入到pprof,使用web命令就会在/tmp下生成svg文件,svg文件是可以在浏览器下看的。像这个样子:

如果你的程序非常简单,比如只有println一个语句,你用pprof.StartCPUProfile是打印不出任何东西的。

举例

下面拿go-tour举个例子,这是个web程序,我在代码中加入了

_ "net/http/pprof"

在浏览器中我就可以直接看prof信息了

生成CPU状态分析图

下面我们想要生成CPU状态分析图,调用go tool pprof http://localhost:3999/debug/pprof/profile

就会进入30秒的profile收集时间,在这段事件内猛刷新点击go-tour浏览器上的页面,尽量让cpu占用性能产生数据。

(pprof) top10

Total: 3 samples

1 33.3% 33.3% 1 33.3% MHeap_AllocLocked

1 33.3% 66.7% 1 33.3% os/exec.(*Cmd).closeDescriptors

1 33.3% 100.0% 1 33.3% runtime.sigprocmask

0 0.0% 100.0% 1 33.3% MCentral_Grow

0 0.0% 100.0% 2 66.7% main.Compile

0 0.0% 100.0% 2 66.7% main.compile

0 0.0% 100.0% 2 66.7% main.run

0 0.0% 100.0% 1 33.3% makeslice1

0 0.0% 100.0% 2 66.7% net/http.(*ServeMux).ServeHTTP

0 0.0% 100.0% 2 66.7% net/http.(*conn).serve

(pprof)web

参考

http://blog.golang.org/2011/06/profiling-go-programs.html

这篇官方文章说得很仔细了,但是要注意,将里面的gopprof工具换成go tool pprof就行了

时间: 2024-07-28 13:38:45

Go的pprof使用的相关文章

Go pprof性能监控

Go net/http/pprof包提供了一个在WEB项目中使用的性能监控的工具, 使用时只需要引用包: _"net/http/pprof" 然后就可以在浏览器中访问地址: http://localhost:port/debug/pprof/来查看性能信息. 最简单的示例: package main import ( "net/http" _ "net/http/pprof" ) func main() { http.ListenAndServe

go tool pprof

我们可以使用go tool pprof命令来交互式的访问概要文件的内容.命令将会分析指定的概要文件,并会根据我们的要求为我们提供高可读性的输出信息. 在Go语言中,我们可以通过标准库的代码包runtime和runtime/pprof中的程序来生成三种包含实时性数据的概要文件,分别是CPU概要文件.内存概要文件和程序阻塞概要文件.下面我们先来分别介绍用于生成这三种概要文件的API的用法. CPU概要文件 在介绍CPU概要文件的生成方法之前,我们先来简单了解一下CPU主频.CPU的主频,即CPU内核

Golang使用pprof和qcachegrind进行性能监控

Golang为我们提供了非常方便的性能测试工具pprof,使用pprof可以非常方便地对Go程序的运行效率进行监测.本文讲述如何使用pprof对Go程序进行性能测试,并使用qcachegrind查看性能测试的输出文件. 载入pprof模块 想要对一个Go程序进行pprof监测,第一步是在main函数所在的模块中添加 net/http/pprof 模块.import后面的“_”是一定要加上的. import _ "net/http/pprof" 运行HTTP服务器 如果你的程序不是一个W

【Go命令教程】12. go tool pprof

我们可以使用 go tool pprof 命令来交互式的访问概要文件的内容.命令将会分析指定的概要文件,并会根据我们的要求为我们提供高可读性的输出信息. 在 Go 语言中,我们可以通过标准库的代码包 runtime 和 runtime/pprof 中的程序来生成三种包含实时性数据的概要文件,分别是 CPU 概要文件.内存概要文件 和 程序阻塞概要文件.下面我们先来分别介绍用于生成这三种概要文件的 API 的用法. CPU 概要文件 在介绍 CPU 概要文件的生成方法之前,我们先来简单了解一下 C

为golang程序使用pprof远程查看httpserver运行堆栈,cpu耗时等信息

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 application

使用go tool pprof分析内存泄漏、CPU消耗

go中提供了pprof包来做代码的性能监控,在两个地方有包: net/http/pprof runtime/pprof 其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来. 使用 net/http/pprof 做WEB服务器的性能监控 如果你的go程序是用http包启动的web服务器,想要查看自己的web服务器的状态.这个时候就可以选择net/http/pprof.    import _ "net/http/pprof"

Golang pprof heap profile is empty

Q: When you use `go tool pprof` get heap data, profile is empty. A: The default sampling rate is 1 sample per 512KB of allocated memory. So If you application use little memory, the profiling can't sampling any data. You can change runtime.MemProfile

pprof 查看goroutine

package main import ( "net/http" "runtime/pprof" ) var quit chan struct{} = make(chan struct{}) func f() { <-quit } func handler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain&quo

golang pprof 性能分析工具

性能优化是个永恒的话题,而很多时候我们在作性能优化的时候,往往基于代码上面的直觉,把所有能想到的优化都优化了一遍,不错过任何小的优化点,结果整个代码的逻辑变得极其复杂,而性能上面并没有太大的提升.事实上,性能问题往往集中在某些小点,有时候很小的改动就能有巨大的提升,所以问题的关键是是怎么去找出这些优化点,幸运的是 golang 在设计的时候就考虑了这个问题,原生提供了性能分析的工具,可以很方便地帮我们找到性能瓶颈 pprof 简介 golang 的性能分析库在 runtime/pprof 里,主