Go pprof性能分析

pprof是Go语言内置的性能分析工具,它可以帮助我们分析程序的CPU使用情况、内存分配等。

pprof 包含两部分:

  • Go语言内置的两个包

    1. net/http/pprof

      runtime/pprof 的二次封装,一般是服务型应用。比如 web server ,它一直运行。这个包对提供的 http 服务进行数据采集分析。

    2. runtime/pprof

      采集程序运行数据进行性能分析,一般用于后台工具型应用,这种应用运行一段时间就结束。

  • 性能剖析工具 go tool pprof

性能分析指标

  1. CPU性能分析
  2. 内存(Memory)性能分析
  3. 阻塞(block)性能分析
  4. 锁(mutex)性能分析

内置包应用

runtime/pprof

cpu分析

关键

go 复制代码
// 开启 cpu 采集分析:
pprof.StartCPUProfile(w io.Writer)

// 停止 cpu 采集分析:
pprof.StopCPUProfile()

demo

go 复制代码
package main

import (
	"log"
	"os"
	"runtime/pprof"
)

func main() {
	f, err := os.OpenFile("cpu.pprof", os.O_CREATE|os.O_RDWR, 0644)
	if err != nil {
		log.Fatalf("Failed to open profile file: %v", err)
		return
	}
	if err = pprof.StartCPUProfile(f); err != nil {
		log.Fatalf("Failed to start CPU profiling: %v", err)
		return
	}
	defer pprof.StopCPUProfile()
	// do something
}

内存分析

关键

go 复制代码
pprof.WriteHeapProfile(w io.Writer)

demo

go 复制代码
func captureHeapProfile(filename string) error {
	// 打开文件,用于存储堆内存信息
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	// 写入堆内存信息
	if err = pprof.WriteHeapProfile(file); err != nil {
		return err
	}

	return nil
}
net/http/pprof

对于服务类型的应用,主要在服务内部匿名引入net/http/pprof包,然后通过HTTP访问pprof页面。

匿名引入方式为:import _ "net/http/pprof"

go 复制代码
package main

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

func main() {
	http.HandleFunc("/", hello)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println("ListenAndServe Err:", err.Error())
		return
	}
}

func hello(resp http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(resp, "Hello World, Are You OK?")
}

浏览器打开 http://localhost:8080/debug/pprof/

如果HTTP服务不是通过http.ListenAndServe(":8080", nil)启动的,需要自己注册pprof路由

net/http/pprof包中,有init函数

go 复制代码
func init() {
   http.HandleFunc("/debug/pprof/", Index)
   http.HandleFunc("/debug/pprof/cmdline", Cmdline)
   http.HandleFunc("/debug/pprof/profile", Profile)
   http.HandleFunc("/debug/pprof/symbol", Symbol)
   http.HandleFunc("/debug/pprof/trace", Trace)
}

如果使用自定义的ServeMux,则需要增加注册后,才能获取到pprof

go 复制代码
// 自己注册这几个函数
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)

go tool pprof

命令格式:

复制代码
pprof <format> [options] [binary] <source>
pprof [options] [binary] <source>
pprof -http [host]:[port] [options] [binary] <source>

想在浏览器查看,需要安装graphviz

web页面模式

go tool pprof -http=:6666 [file_pprof|url_pprof]

复制代码
go tool pprof -http=:9999 cpu.pprof
go tool pprof -http=:9999 mem.pprof
命令行模式

go tool pprof [file_pprof|url_pprof]

常用:

复制代码
go tool pprof -http=:9999 cpu.pprof
go tool pprof -http=:9999 /tmp/profile215959616/mem.pprof
go test -bench="Fib$" -cpuprofile=cpu.pprof .
go tool pprof -text cpu.pprof

https://blog.csdn.net/sinat_24985411/article/details/128816228

https://www.cnblogs.com/jiujuan/p/14588185.html

https://github.com/google/pprof/blob/main/doc/README.md

https://pkg.go.dev/runtime/pprof#hdr-Profiling_a_Go_program

https://www.cnblogs.com/jiujuan/p/14588185.html

相关推荐
roman_日积跬步-终至千里2 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
海奥华211 小时前
go中的接口返回设计思想
开发语言·后端·golang
飞川撸码15 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
roman_日积跬步-终至千里1 天前
【Go语言基础【14】】defer与异常处理(panic、recover)
golang
孔令飞1 天前
Kubernetes 节点自动伸缩(Cluster Autoscaler)原理与实践
ai·云原生·容器·golang·kubernetes
我的golang之路果然有问题1 天前
云服务器部署Gin+gorm 项目 demo
运维·服务器·后端·学习·golang·gin
孔令飞1 天前
Go 为何天生适合云原生?
ai·云原生·容器·golang·kubernetes
YGGP1 天前
吃透 Golang 基础:数据结构之 Map
开发语言·数据结构·golang
march of Time1 天前
go工具库:hertz api框架 hertz client的使用
开发语言·golang·iphone