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

相关推荐
大侠归来9 小时前
C 与 Go 语言对比:从设计哲学到工程实践
java·c语言·golang
小小龙学IT10 小时前
Go 语言内存管理与垃圾回收(GC)深度解析:从逃逸分析到性能调优适
golang·go1.19
HEJOO912 小时前
Go 语言结构体定义与实例详解
开发语言·算法·golang
墨鱼老师13 小时前
Go+Gin+Vue 毕设项目:Gin 框架搭建后端基础接口实战
vue.js·golang·go·gin·前后端分离·计算机毕业设计·go 后端
JWASX1 天前
Java 转 go 学习 - 类型转换
学习·golang
小小龙学IT1 天前
Go 语言 encoding/json 标准库深度解析:从 Tag 反射到流式处理
golang·json
JWASX1 天前
Java 转 go 学习 - 函数(1)
学习·golang
看浪的路人1 天前
第7讲:实时告警与自动化响应
开发语言·后端·golang
小小龙学IT1 天前
Go 语言 database/sql 标准库深度解析:从连接池到驱动抽象
数据库·sql·golang
wdfk_prog2 天前
Wi-Fi Direct 源码分析(09):从 P2P_CONNECT 到 GO Negotiation 完成
运维·服务器·ubuntu·golang·asp.net·p2p·wifi-direct