pprof简介

pprof是什么

pprof 是一个强大的性能分析工具,可以捕捉到多维度的运行状态的数据,下面简单介绍一下pprof的用法。
golang在语言层面集成了profile采样工具,在程序运行过程中可以获取cpu、heap、block、traces等执行信息,这些会涉及到runtime/pprofnet/http/pprofruntime/trace等package。

pprof的使用

一般情况下,获取profile数据最有两种形式:web形式与profile文件生成形式。

Web方式

引入 “net/http/pprof"包,并在主函数中添加一下代码:

package main

import (
	"log"
	"net/http"
	_ "net/http/pprof"
)

func main() {
	// 性能分析
	go func() {
		log.Println(http.ListenAndServe(":8080", nil))
	}()

	// 实际业务代码
	for {
		Add("test")
	}
}

func Add(str string) string {
	data := []byte(str)
	sData := string(data)
	var sum = 0
	for i := 0; i < 10000; i++ {
		sum += i
	}
	return sData
}

其中,net/http/pprof包的init函数中注册了以下路由到http服务中,用浏览器打开http://localhost:8080/debug/pprof/,即可使用pprof提供的功能。

在这里插入图片描述

allocs

allocs报告过去的内存申请情况。
在这里插入图片描述

block

报告协程阻塞的情况,可以用来分析和查找死锁等性能瓶颈,默认不开启, 需要调用runtime.SetBlockProfileRate开启。下面看个例子,访问http://ip:port/debug/pprof/block?debug=1。
还可以通过go tool pprof http://ip:port/debug/pprof/block进入交互模式进行查看。

cmdline

这个接口功能很简单,主要是调用
os.Args,获取程序启动时的命令及参数。

goroutine

报告协程相关信息,可以用来查看有哪些协程正在运行、有多少协程在运行等。访问http://ip:port/debug/pprof/goroutine?debug=1,如下:在这里插入图片描述

heap

heap:查看堆相关信息,包括一些GC的信息。访问http://ip:port/debug/pprof/heap?debug=1
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
上图中包含了堆分配的细节,同时在底部还包含了整个堆的统计信息、GC的统计信息等。同样,也可以使用go tool pprof http://ip:port/debug/pprof/heap进入交互模式进行查看。可以指定以下4个选项,alloc_objects、alloc_space、inuse_objects、inuse_space默认为alloc_space。

mutex

查看互斥的争用情况,默认不开启, 需要调用需要在程序中调用runtime.SetMutexProfileFraction。

profile

此接口返回CPU的profile,调用了runtime/pprof包的StartCPUProfile,采样频率为100Hz。接口默认是返回30秒的数据,可以通过seconds参数设定需要获取的数据时长。比如获取10秒钟的cpu profile数据,有两种方式进行数据获取与分析:

  1. go tool pprof http://ip:port/debug/pprof/profile?seconds=10,然后会进入交互模式,直接就可以查看数据,如图:
    在这里插入图片描述

  2. 先获取profile文件,然后使用go tool pprof进行分析:

    wget http://localhost:8080/debug/pprof/profile?seconds=10 -O testfile
    

    然后进行分析:

    go tool pprof testfile
    

其实第一种方式只是把获取文件和交互操作模式放到一起执行而已,profile数据已经被保存到了/tmp目录下。

threadcreate

查看系统线程创建信息

trace

trace可以向你揭示:Go程序运行中的所有的运行时事件。 这种工具是Go生态系统中用于诊断性能问题时(如延迟,并行化和竞争异常)最有用的工具之一。
本地测试是不能使用web(不知道为啥),但可以先用web生成trace,然后使用go tool trace 来分析。

profile文件方式

通过生成profile文件的形式来获取数据,需要在代码中添加如下代码。

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

func main() {
	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 ...

	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()
	}
}

然后使用go tool分析文件。或者使用test的功能获取profile文件:go test -cpuprofile cpu.prof -memprofile mem.prof -bench .
CPU分析:
执行

go run main.go --cpuprofile=cpu.prof

会在当前路径下生成cpu.prof 文件,然后执行

go tool pprof main.go cpu.prof

进入cpu分析模块。
heap分析:
执行

go run main.go --memprofile=mem.prof
go tool pprof main.go mem.prof
top10
web

除了以上3种形式,我们也可以在程序直接调用runtime中的功能进行profile数据获取,runtime包中提供了相关的接口。如果想进行更深入的研究,可以查看https://blog.golang.org/2011/06/profiling-go-programs.html

关于trace:
https://www.itcodemonkey.com/article/5419.html
https://studygolang.com/articles/9693

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值