annidy / notes

0 stars 0 forks source link

pprof #284

Open annidy opened 4 months ago

annidy commented 4 months ago

1. 生成pprof

生成方式

  1. 使用net/http/pprof包,这也最简单
  2. 在运行test时,顺便生成pprof。go test -cpuprofile cpu.prof -memprofile mem.prof -bench .

    go help test输出不完整,建议查看官网文档Testing flags文档

  3. 手动调用runtime/pprof。定制性最高,侵入性也大
    func main() {
    defer profile.Start(profile.CPUProfile,profile.ProfilePath(".")).Stop()
    // xxx
    }

2. 分析pprof

使用go工具链分析

go tool pprof -h
usage:

Produce output in the specified format.

   pprof <format> [options] [binary] <source> ...

Omit the format to get an interactive shell whose commands can be used
to generate various views of a profile

   pprof [options] [binary] <source> ...

Omit the format and provide the "-http" flag to get an interactive web
interface at the specified host:port that can be used to navigate through
various views of a profile.

   pprof -http [host]:[port] [options] [binary] <source> ...

source是必选的。如果使用方式1生成,source是地址+时间,

或者: pprof -seconds 60 http://localhost:6060/debug/pprof/heap

分析步骤

golang pprof 实战


官方文档: Profiling a Go program