加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[golang]pprof性能分析工具

发布时间:2020-12-16 18:07:39 所属栏目:大数据 来源:网络整理
导读:1. 关于pprof pprof是golang程序一个性能分析的工具,可以查看堆栈、cpu信息等。 2. 源码示例 package main import ( "flag" "log" "net/http" _ "net/http/pprof" "sync" "time" ) func Counter(wg *sync.WaitGroup) { time.Sleep(time.Second) var counter

1. 关于pprof

pprof是golang程序一个性能分析的工具,可以查看堆栈、cpu信息等。

2. 源码示例

package main

import (
    "flag"
    "log"
    "net/http"
    _ "net/http/pprof"
    "sync"
    "time"
)

func Counter(wg *sync.WaitGroup) {
    time.Sleep(time.Second)

    var counter int
    for i := 0; i < 1000000; i++ {
        time.Sleep(time.Millisecond * 200)
        counter++
    }
    wg.Done()
}

func main() {
    flag.Parse()

    //远程获取pprof数据
    go func() {
        log.Println(http.ListenAndServe("localhost:8080",nil))
    }()

    var wg sync.WaitGroup
    wg.Add(10)
    for i := 0; i < 10; i++ {
        go Counter(&wg)
    }
    wg.Wait()

    // sleep 10mins,在程序退出之前可以查看性能参数.
    time.Sleep(60 * time.Second)
}

3. 编译运行

go run test_pprof.go

4. 通过网页查看overview

5. 通过终端命令查看各参数

查看堆栈信息

root@XIAOMO:~# go tool pprof http://localhost:8080/debug/pprof/heap
Fetching profile from http://localhost:8080/debug/pprof/heap
Saved profile in /root/pprof/pprof.localhost:8080.inuse_objects.inuse_space.003.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) top10
1069.45kB of 1069.45kB total (  100%)
Showing top 10 nodes out of 11 (cum >= 512.19kB)
      flat  flat%   sum%        cum   cum%
  557.26kB 52.11% 52.11%   557.26kB 52.11%  html.init
  512.19kB 47.89%   100%   512.19kB 47.89%  runtime.malg
         0     0%   100%   557.26kB 52.11%  html/template.init
         0     0%   100%   557.26kB 52.11%  main.init
         0     0%   100%   557.26kB 52.11%  net/http/pprof.init
         0     0%   100%   557.26kB 52.11%  runtime.goexit
         0     0%   100%   557.26kB 52.11%  runtime.main
         0     0%   100%   512.19kB 47.89%  runtime.mcommoninit
         0     0%   100%   512.19kB 47.89%  runtime.mpreinit
         0     0%   100%   512.19kB 47.89%  runtime.rt0_go
(pprof) web

top10命令查看了堆栈空间最大的10个函数调用;
web明令则生成了很详细的图,如下:


查看cpu性能信息

root@xiaomo:~$ go tool pprof http://localhost:8080/debug/pprof/profile
Fetching profile from http://localhost:8080/debug/pprof/profile
Please wait... (30s)
Saved profile in /home/xiaomo/pprof/pprof.pprof.localhost:8080.samples.cpu.001.pb.gz
Entering interactive mode (type "help" for commands)
(pprof) web

输入命令web后生成详细结果,如下图所示:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读