MDGSF Software Engineer

[GO] pprof profile 测试CPU和内存

2018-05-01
GO

两种方法

  1. /usr/local/go/src/runtime/pprof/pprof.go
  2. /usr/local/go/src/net/http/pprof/pprof.go

其实第二个就只是用 HTTP 封装的一个接口,使用起来更方便。

方法一

package main

import (
    "flag"
    "os"
    "runtime"
    "runtime/pprof"

    "github.com/MDGSF/utils/log"
)

var cpuprofile = flag.String("cpuprofile", "cpu.prof", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "mem.prof", "write memory profile to `file`")

func main() {
    log.Println("demo start")
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal("could not create CPU profile: ", err)
        }
        if err := pprof.StartCPUProfile(f); err != nil {
            log.Fatal("could not start CPU profile: ", err)
        }
        defer pprof.StopCPUProfile()
    }

    // ... rest of the program ...
    f1()

    if *memprofile != "" {
        f, err := os.Create(*memprofile)
        if err != nil {
            log.Fatal("could not create memory profile: ", err)
        }
        runtime.GC() // get up-to-date statistics
        if err := pprof.WriteHeapProfile(f); err != nil {
            log.Fatal("could not write memory profile: ", err)
        }
        f.Close()
    }
}

func f1() {
    for i := 0; i < 10000; i++ {
        log.Println("f1 loop")
    }
}

go build test.go

执行程序 ./test 会生成 cpu.prof 和 mem.prof 两个文件。

go tool pprof test cpu.profgo tool pprof test mem.prof 会进入 pprof 的交互界面

help 帮助信息 web 打开浏览器 top

方法二

Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool.

The package is typically only imported for the side effect of registering its HTTP handlers. The handled paths all begin with /debug/pprof/.

To use pprof, link this package into your program: import _ “net/http/pprof”

If your application is not already running an http server, you need to start one. Add “net/http” and “log” to your imports and the following code to your main function:

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

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, after calling runtime.SetBlockProfileRate in your program:

go tool pprof http://localhost:6060/debug/pprof/block

Or to collect a 5-second execution trace:

wget http://localhost:6060/debug/pprof/trace?seconds=5

Or to look at the holders of contended mutexes, after calling runtime.SetMutexProfileFraction in your program:

go tool pprof http://localhost:6060/debug/pprof/mutex

To view all available profiles, open http://localhost:6060/debug/pprof/ in your browser.

For a study of the facility in action, visit

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

翻译:

就是说想要使用 pprof,只要把

import _ "net/http/pprof"

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

这段代码加入到你的程序中去就可以了。然后你就可以运行你的程序了。在你的程序的运行过程中,执行下面这些命令:

go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/profile
go tool pprof http://localhost:6060/debug/pprof/block
wget http://localhost:6060/debug/pprof/trace?seconds=5
go tool pprof http://localhost:6060/debug/pprof/mutex

http://localhost:6060/debug/pprof/ 这个是在浏览器中打开的。

用 ssh 把服务器端口映射到本地电脑,然后在浏览器打开

http://localhost:6060/debug/pprof/

就可以看到当前的协程数据

go-torch

https://github.com/uber/go-torch

# This will listen on :8081 and open a browser.
# Change :8081 to a port of your choice.
$ go tool pprof -http=":8081" [binary] [profile]

https://studygolang.com/articles/9940

http://www.ruanyifeng.com/blog/2017/09/flame-graph.html


weixingongzhonghao

Comments

Content