【博客543】golang pprof性能调试:寻找cpu瓶颈
创始人
2024-02-22 15:16:16
0

golang pprof性能调试:寻找cpu瓶颈

1、引入pprof进行性能调试

在代码中加入:

import _ "net/http/pprof"go func() {http.ListenAndServe("0.0.0.0:8899", nil)
}()

示例:为冒泡排序加入pprof debug

package mainimport ("math/rand""net/http"_ "net/http/pprof""time"
)func generate(n int) []int {rand.Seed(time.Now().UnixNano())nums := make([]int, 0)for i := 0; i < n; i++ {nums = append(nums, rand.Int())}return nums
}
func bubbleSort(nums []int) {for i := 0; i < len(nums); i++ {for j := 1; j < len(nums)-i; j++ {if nums[j] < nums[j-1] {nums[j], nums[j-1] = nums[j-1], nums[j]}}}
}func main() {go func() {http.ListenAndServe("0.0.0.0:8899", nil)}()for {n := 10for i := 0; i < 5; i++ {nums := generate(n)bubbleSort(nums)n *= 10}}
}

2、pprof命令行交互式查看程序最消耗cpu的地方

1、启动上述程序:

go run main.go

2、访问pprof暴露的cpu debug api接口,收集30s内的cpu数据:

go tool pprof http://localhost:8899/debug/pprof/profile\?seconds\=30

3、在交互命令中,使用top,list来查看最消耗cpu的地方

go tool pprof http://localhost:8899/debug/pprof/profile\?seconds\=30
Fetching profile over HTTP from http://localhost:8899/debug/pprof/profile?seconds=30
Saved profile in /Users/zejia.lu/pprof/pprof.samples.cpu.003.pb.gz
Type: cpu
Time: Nov 27, 2022 at 12:09pm (CST)
Duration: 30.19s, Total samples = 26.13s (86.56%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 26.06s, 99.73% of 26.13s total
Dropped 18 nodes (cum <= 0.13s)flat  flat%   sum%        cum   cum%24.57s 94.03% 94.03%     26.06s 99.73%  main.bubbleSort (inline)1.49s  5.70% 99.73%      1.49s  5.70%  runtime.asyncPreempt0     0% 99.73%     26.06s 99.73%  main.main0     0% 99.73%     26.06s 99.73%  runtime.main
(pprof) list main.bubbleSort
Total: 26.13s
ROUTINE ======================== main.bubbleSort in /Users/zejia.lu/go_pprof/main.go24.57s     26.06s (flat, cum) 99.73% of Total.          .     15:	}.          .     16:	return nums.          .     17:}.          .     18:func bubbleSort(nums []int) {.          .     19:	for i := 0; i < len(nums); i++ {12.41s     13.31s     20:		for j := 1; j < len(nums)-i; j++ {4.33s      4.92s     21:			if nums[j] < nums[j-1] {7.83s      7.83s     22:				nums[j], nums[j-1] = nums[j-1], nums[j].          .     23:			}.          .     24:		}.          .     25:	}.          .     26:}.          .     27:

4、输出列解析

flat	当前函数占用 cpu 耗时
flat %	当前函数占用 cpu 耗时百分比
sum%	函数占用 cpu 时间累积占比,从小到大一直累积到 100%
cum	    当前函数加上调用当前函数的函数占用 cpu 的总耗时
%cum	当前函数加上调用当前函数的函数占用 cpu 的总耗时占比

5、结果分析

我们可以看到最消耗cpu的函数:
24.57s 94.03% 94.03%     26.06s 99.73%  main.bubbleSort (inline)我们可以看到这个函数里cpu消耗在哪里:
12.41s     13.31s     20:		for j := 1; j < len(nums)-i; j++ {
4.33s      4.92s     21:			if nums[j] < nums[j-1] {
7.83s      7.83s     22:				nums[j], nums[j-1] = nums[j-1], nums[j]

3、pprof ui方式查看程序最消耗cpu的地方

1、调试时指定通过ui方式查看,ui地址为8888端口,要调试的服务暴露的pprof端口为8899:

go tool pprof -http=":8888" http://localhost:8899/debug/pprof/profile

2、访问8888端口查看ui界面进行可视化调试:

查看最消耗cpu的地方:
在这里插入图片描述
在这里插入图片描述

4、pprof ui方式查看cpu消耗拓扑图

在这里插入图片描述
在这里插入图片描述

5、pprof ui方式查看cpu消耗火焰图

在这里插入图片描述
在这里插入图片描述

生成追踪图和火焰图需要安装graphviz

brew install graphviz

常用命令

// 查看cpu消耗前20的函数调用
top 20// 查看累计cpu消耗前20的函数调用
top -cum 20// 查看代码符号附近的cpu占用情况。其中代码符号类似于c++的函数符号表,可以简单理解成函数名
list "代码符号"

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...