编程笔记

lifelong learning & practice makes perfect

go-pprof

What

How

Display

  1. go tool pprof -http=:9999 cpu.pprof

Benchmark & pprof

  1. gen

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    func fib(n int) int {
    if n == 0 || n == 1 {
    return n
    }
    return fib(n-2) + fib(n-1)
    }

    func BenchmarkFib(b *testing.B) {
    for n := 0; n < b.N; n++ {
    fib(30) // run fib(30) b.N times
    }
    }
    只需要在 go test 添加 -cpuprofile 参数即可生成 BenchmarkFib 对应的 CPU profile 文件:
    $ go test -bench="Fib$" -cpuprofile=cpu.pprof .
    goos: linux
    goarch: amd64
    pkg: example
    BenchmarkFib-8 196 6071636 ns/op
    PASS
    ok example 2.046s

References

  1. 极客兔兔https://geektutu.com/post/hpg-pprof.html

欢迎关注我的其它发布渠道