GO语言性能分析

Go语言基准测试与pprof工具性能分析详解

在现代软件开发中,性能优化是一个重要的环节。Go语言提供了强大的工具来进行基准测试和性能分析,其中 testing 包用于基准测试,而 pprof 工具用于性能分析。本文将详细讲解如何使用这些工具来进行性能优化。

参考文档:Go 语言中的大杀器

一、基准测试

基准测试(Benchmarking)是评估代码性能的常用方法。Go语言的 testing 包提供了便捷的基准测试功能。

1. 基准测试函数

基准测试函数的命名必须以 Benchmark 开头,并接受一个 *testing.B 类型的参数。以下是一个简单的基准测试示例:

go 复制代码
package main

import (
	"fmt"
	"strings"
	"testing"
)

func BenchmarkStringBuilder(b *testing.B) {
	strs := []string{"Hello", "World", "Golang", "is", "awesome", "and", "powerful", "for", "string", "concatenation"}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		var builder strings.Builder
		for _, str := range strs {
			builder.WriteString(str)
		}
		_ = builder.String()
	}
}

2. 运行基准测试

你可以使用以下命令来生成不同类型的性能分析数据文件:

1. 生成 CPU 性能分析数据文件
sh 复制代码
go test -bench=. -cpuprofile=cpu.profile
2. 生成内存分配的性能分析数据文件
sh 复制代码
go test -bench=. -memprofile=mem.profile
3. 生成阻塞操作的性能分析数据文件
sh 复制代码
go test -bench=. -blockprofile=block.profile
4. 生成互斥锁争用的性能分析数据文件
sh 复制代码
go test -bench=. -mutexprofile=mutex.profile
5. 生成执行跟踪数据文件
sh 复制代码
go test -bench=. -trace=trace.out

本次在终端中运行以下命令来执行基准测试:

sh 复制代码
go test -run=^$ -bench=StringBuilder -cpuprofile=cpu.prof -memprofile=mem.prof

命令解释:

复制代码
go test -run=^$ -bench=StringBuilder -cpuprofile=cpu.prof -memprofile=mem.prof
-run=^$ 代表只运行基准测试, 否则会运行其余的单元测试函数
-cpuprofile=cpu.prof -memprofile=mem.prof 输出对应的prof文件

输出示例:

复制代码
goos: darwin
goarch: amd64
pkg: main/utils
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkStringBuilder-12        7450434               155.8 ns/op
PASS
ok      main/utils      2.062s

3. 基准测试结果解读

  • BenchmarkStringBuilder-12 :表示在12个CPU核心上运行的基准测试。
  • 7450434:表示基准测试运行的次数。
  • 155.8 ns/op:表示每次操作的平均时间。

二、pprof工具

pprof 是 Go语言内置的性能分析工具,用于分析CPU和内存等使用情况。

1. 安装pprof

pprof 工具通常随Go语言安装包一起提供。如果没有安装,可以使用以下命令安装:

sh 复制代码
go get -u github.com/google/pprof

2. CPU性能分析

使用pprof工具分析通过基准测试生成的cpu.prof文件,在终端中运行以下命令:

sh 复制代码
go tool pprof -http=:6060 cpu.prof 
web图形化查看对应的prof文件

进入pprof交互界面

sh 复制代码
http://localhost:6060/ui/

通过火焰图分析主要的CPU消耗

三、对正在运行程序的pprof分析

1. 引入pprof包

首先,你需要在你的Go程序中引入 net/http/pprof 包,并启动一个HTTP服务器。这样,你就可以通过HTTP接口来访问pprof提供的各种性能分析数据。

go 复制代码
package main

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

func main() {
    go func() {
        http.ListenAndServe("localhost:6060", nil)
    }()
    // 模拟一些工作负载
    for {
        time.Sleep(1 * time.Second)
        go func() {
            for i := 0; i < 1000000; i++ {
                _ = i * i
            }
        }()
    }
}

2、采集性能数据

1. 采集CPU性能数据

你可以使用 wgetcurl 命令来采集CPU性能数据。默认情况下,pprof会采集30秒的CPU性能数据,但你可以通过在URL中添加 seconds 参数来指定采集时间。

使用 wget 采集CPU性能数据
sh 复制代码
wget http://127.0.0.1:6060/debug/pprof/profile?seconds=60 -O cpu.profile
使用 curl 采集CPU性能数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/profile?seconds=60 -o cpu.profile

所有采集都可以使用wgetcurl 命令,以上通过CPU性能数据展示wgetcurl 命令用法,其余采集同理。

其余性能数据采集:

2. 采集内存性能数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/heap -o heap.profile
3. 采集阻塞操作数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/block -o block.profile
4. 采集互斥锁争用数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/mutex -o mutex.profile
5. 采集Goroutine数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/goroutine -o goroutine.profile
6. 采集线程创建数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/threadcreate -o threadcreate.profile
7. 采集执行跟踪数据
sh 复制代码
curl http://127.0.0.1:6060/debug/pprof/trace?seconds=5 -o trace.out

3、查看可视化界面

以CPU性能采集数据为例,其余性能数据同理。

该命令将在所指定的端口号运行一个 PProf 的分析用的站点。

sh 复制代码
go tool pprof -http=:6001 cpu.profile

进入pprof交互界面

sh 复制代码
http://localhost:6001/ui/

通过火焰图分析主要的CPU消耗

四、总结

通过本文的介绍,你应该已经了解了如何在Go语言中进行基准测试,并使用pprof工具进行性能分析。这些工具可以帮助你识别和优化代码中的性能瓶颈,从而提高应用的整体性能。希望本文对你有所帮助!

相关推荐
aischang5 分钟前
统信桌面专业版如何使用python开发平台jupyter
开发语言·python·jupyter·统信uos
摘星编程21 分钟前
原型模式深度解析:Java设计模式实战指南与克隆机制优化实践
java·设计模式·性能优化·原型模式·创建型模式·软件架构·对象克隆
狐凄26 分钟前
Python实例题:Python计算概率论
开发语言·python·概率论
卫生纸不够用32 分钟前
02-性能方案设计
性能优化
q567315231 小时前
分布式增量爬虫实现方案
开发语言·分布式·爬虫·python
勤奋的知更鸟1 小时前
LLaMA-Factory和python版本的兼容性问题解决
开发语言·python·llama-factory
CIb0la1 小时前
Ai自动补全编程工具:llama vscode
运维·开发语言·学习·测试工具·程序人生
1candobetter1 小时前
JAVA后端开发——多租户
java·开发语言
freyazzr1 小时前
C++八股 | Day3 | 智能指针 / 内存管理 / 内存分区 / 内存对齐
开发语言·c++
序属秋秋秋2 小时前
《C++初阶之入门基础》【普通引用 + 常量引用 + 内联函数 + nullptr】
开发语言·c++·笔记