当前位置: 首页 > news >正文

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分析

关键

// 开启 cpu 采集分析:
pprof.StartCPUProfile(w io.Writer)// 停止 cpu 采集分析:
pprof.StopCPUProfile()

demo

package mainimport ("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
}

内存分析

关键

pprof.WriteHeapProfile(w io.Writer)

demo

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"

package mainimport ("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函数

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

// 自己注册这几个函数
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

http://www.lryc.cn/news/462887.html

相关文章:

  • 扬帆出海!九章云极DataCanvas公司惊艳亮相迪拜GITEX Global 2024
  • Qt | 元对象+元枚举+Qt自带图标案例
  • linux升级cmake
  • ip a查看网卡接口信息
  • centos celery 日志管理
  • 深度学习(DL)实战——基本概念介绍
  • 较新(24.3)加速Diffusion模型推理的方法,附带参考文献
  • 硬件产品经理的开店冒险之旅(上篇)
  • 「C++」类和对象最终回
  • ELK:Elasticsearch、Logstash、Kibana Spring Cloud Sleuth和Spring Cloud Zipkin
  • 动态规划17:123. 买卖股票的最佳时机 III
  • 华为OD机试真题---预定酒店
  • 力扣242.有效的字母异位词
  • Android IP路由策略和防火墙
  • MySQL insert ... select 语句锁表导致数据写不进去
  • Android摄像头Camera2和Camera1的一些总结
  • 【Linux 从基础到进阶】Linux中的用户认证与授权
  • 用户界面设计:视觉美学与交互逻辑的融合
  • ZK集群搭建:详细步骤与注意事项
  • 如何将csdn文章导出为pdf
  • 【艾思科蓝】Imagen:重塑图像生成领域的革命性突破
  • java类和对象(下): 封装 static成员 内部类
  • 外包干了3周,技术退步太明显了。。。。。
  • VIVO算法题——数位之积
  • OPC Router快速打通设备层与influxDB数据通讯
  • 鸿蒙开发 四十四 ArkTs BuilderParam传递UI(二)
  • 同期数分析-留存率
  • Java前后端交互:构建现代Web应用
  • vue3中用axios请求怎么添加cookie
  • informer学习笔记