简介

Go 运行时的分析接口存在于 runtime/pprof 包中。 runtime/pprof 是一个非常低级的工具,由于历史原因,不同类型 profile 的接口并不统一,而且使用起来还不是太方便,例如:输出数据使用 io.Writer 接口,虽然扩展性很强,但是对于实际使用不够方便,不支持写入文件,默认配置项较为复杂。

引入 github.com/pkg/profile 包,在需要测试的程序代码前面加入下面代码:

1
2
3
4
5
6
7
8
9
stopper := profile.Start(profile.CPUProfile, profile.ProfilePath("."))    // 开始性能分析, 返回一个停止接口

defer stopper.Stop()    // 在被测试程序结束时停止性能分析

// 分析的核心逻辑代码

...

time.Sleep(time.Second)    // 让程序至少运行1秒

使用 profile.Start 调用 github.com/pkg/profile 包的开启性能分析接口。

这个 Start 函数的参数都是可选项,这里需要指定的分析项目是 profile.CPUProfile,也就是 CPU 耗用。如果分析内存分配情况,则所传参数改为 profile.MemProfile。

profile.ProfilePath(".") 指定输出的分析文件路径,这里指定为当前文件夹。profile.Start() 函数会返回一个 Stop 接口,方便在程序结束时结束性能分析。为了保证性能分析数据的合理性,分析的最短时间是 1 秒,使用 time.Sleep() 在程序结束前等待 1 秒。如果你的程序默认可以运行 1 秒以上,这个等待可以去掉。

相关API

Constants

1
const DefaultMemProfileRate = 4096

DefaultMemProfileRate是默认的内存分析速率.

pprof每分配 runtime.MemProfileRate(默认为512K) 个字节进行一次数据采样。

func BlockProfile

1
func BlockProfile(p *Profile)

BlockProfile启用锁(争用)性能分析。它禁用任何以前的性能分析设置。

func CPUProfile

1
func CPUProfile(p *Profile)

CPUProfile启用cpu分析。它禁用任何以前的性能分析设置。

Example

Code:

1
2
3
// CPU profiling is the default profiling mode, but you can specify it
// explicitly for completeness.
defer profile.Start(profile.CPUProfile).Stop()

func GoroutineProfile

1
func GoroutineProfile(p *Profile)

GoroutineProfile启用goroutine分析。它禁用任何以前的性能分析设置。

func MemProfile

1
func MemProfile(p *Profile)

MemProfile启用内存分析。它禁用任何以前的性能分析设置。

Example

Code:

1
2
// use memory profiling, rather than the default cpu profiling.
defer profile.Start(profile.MemProfile).Stop()

func MemProfileRate

1
func MemProfileRate(rate int) func(*Profile)

MemProfileRate以指定速率启用内存分析。它禁用任何以前的性能分析设置。

Example

Code:

1
2
// use memory profiling with custom rate.
defer profile.Start(profile.MemProfileRate(2048)).Stop()

func MutexProfile

1
func MutexProfile(p *Profile)

MutexProfile启用互斥分析。它禁用任何以前的性能分析设置。

func NoShutdownHook

1
func NoShutdownHook(p *Profile)

NoShutdownHook控制配置文件包是否应挂接SIGINT以干净地编写配置文件。具有更复杂信号处理能力的程序应将其设置为true,并确保在关机期间调用从Start()返回的Stop()函数。

Example

Code:

1
2
// disable the automatic shutdown hook.
defer profile.Start(profile.NoShutdownHook).Stop()

func ProfilePath

1
func ProfilePath(path string) func(*Profile)

ProfilePath控制着写入各种性能分析文件的基本路径。如果为空,则基本路径将由ioutil.TempDir生成。

Example

Code:

1
2
// set the location that the profile will be written to
defer profile.Start(profile.ProfilePath(os.Getenv("HOME"))).Stop()

func Quiet

1
func Quiet(p *Profile)

Quiet禁止在分析过程中提供参考消息。

func Start

1
2
3
func Start(options ...func(*Profile)) interface {
Stop()
}

Start将启动一个新的性能分析会话。调用方应在返回的值上调用Stop方法以完全停止分析。

Example

Code:

1
2
3
// start a simple CPU profile and register
// a defer to Stop (flush) the profiling data.
defer profile.Start().Stop()

Example (WithFlags)

Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// use the flags package to selectively enable profiling.
mode := flag.String("profile.mode", "", "enable profiling mode, one of [cpu, mem, mutex, block]")
flag.Parse()
switch *mode {
case "cpu":
    defer profile.Start(profile.CPUProfile).Stop()
case "mem":
    defer profile.Start(profile.MemProfile).Stop()
case "mutex":
    defer profile.Start(profile.MutexProfile).Stop()
case "block":
    defer profile.Start(profile.BlockProfile).Stop()
default:
    // do nothing
}

func ThreadcreationProfile

1
func ThreadcreationProfile(p *Profile)

ThreadcreationProfile启用线程创建性能分析。它禁用任何以前的性能分析设置。

func TraceProfile

1
func TraceProfile(p *Profile)

跟踪配置文件启用执行跟踪。它禁用任何以前的性能分析设置。

Example

Code:

1
2
// use execution tracing, rather than the default cpu profiling.
defer profile.Start(profile.TraceProfile).Stop()

type Profile

1
2
3
type Profile struct {
// contains filtered or unexported fields
}

概要文件表示活动的概要分析会话

func (*Profile) Stop

1
func (p *Profile) Stop()

Stop将停止配置文件并刷新所有未写入的数据。