翔云

Just try, don't shy. 最新文章请点击
随笔 - 294, 文章 - 0, 评论 - 27, 阅读 - 49万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

golang程序性能分析

Posted on   翔云123456  阅读(420)  评论(0编辑  收藏  举报

pprof和trace 是golang程序性能分析中经常用到的两个工具。

本文简单介绍其使用方法。

1.程序中引入pprof package

对于长期运行的的后台程序来说,使用package net/http/pprof 是比较便捷的选择。

使用方法非常简单,只要import package的地方加上:

import _ "net/http/pprof"

就可以使用提供的接口包括:

"/debug/pprof/"
"/debug/pprof/cmdline"
"/debug/pprof/profile"
"/debug/pprof/symbol"
"/debug/pprof/trace"
"/debug/pprof/goroutine"
"/debug/pprof/heap"
...

例子如下所示:

/*simple.go*/

package main

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

func main() {

        go func() {
                log.Println(http.ListenAndServe("localhost:6060", nil))
        }()

        go worker()

        select{}
}

// simple worker
func worker(){

        strSlice := []string{}
        for {
                str := "hello world "
                strSlice = append(strSlice, str)

                time.Sleep(time.Second)
        }

}

2.使用

浏览器中打开
http://127.0.0.1:6060/debug/pprof

可以看到如下的界面:

 /debug/pprof/

Types of profiles available:
Count	Profile
2	allocs
0	block
0	cmdline
5	goroutine
2	heap
0	mutex
0	profile
9	threadcreate
0	trace
full goroutine stack dump

Profile Descriptions:

    allocs:
    A sampling of all past memory allocations
    block:
    Stack traces that led to blocking on synchronization primitives
    cmdline:
    The command line invocation of the current program
    goroutine:
    Stack traces of all current goroutines
    heap:
    A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.
    mutex:
    Stack traces of holders of contended mutexes
    profile:
    CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.
    threadcreate:
    Stack traces that led to the creation of new OS threads
    trace:
    A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.

2.1 查看当前正在执行的goroutine

浏览器中打开链接127.0.0.1:6060/debug/pprof/goroutine
会下载goroutine文件。

下载后,在命令行下执行:

go tool pprof -http=":8081" goroutine

会自动打开浏览器页面如下图所示。
在这里插入图片描述

在图中可以清晰的看到goroutine的数量以及调用关系。

左侧的菜单栏,可以查看Top、Graph、Flame Graph等。

2.2 查看内存占用

浏览器中打开链接127.0.0.1:6060/debug/pprof/heap
会下载heap文件。

下载后,在命令行下执行:

go tool pprof -http=":8081" heap

会自动打开浏览器页面如下图所示。
在这里插入图片描述

从图中可以直观的发现内存占用最多的都是哪些部分。

2.3 查看CPU使用情况

浏览器中打开链接http://127.0.0.1:6060/debug/pprof/profile?seconds=5

会下载profile文件。

这里指定采样参数seconds为5s,意思是采用5s之内的cpu使用情况。

下载后,在命令行下执行:

go tool pprof -http=":8081" profile

会自动打开浏览器页面如下图所示。
在这里插入图片描述

从图中可以直观的发现内存占用最多的都是哪些部分。

由于例子程序一直空闲,此处的图借用其他程序的图。:)

2.4 跟踪当前程序的执行

浏览器中打开链接
http://127.0.0.1:6060/debug/pprof/trace?seconds=5

会下载trace文件。

通过trace文件,可以查看各个goroutine执行耗时情况,包括网络等待耗时、同步耗时、GC耗时等。

下载后,在命令行下执行:
go tool trace -http=":8081" trace

会自动打开浏览器页面如下图所示。
在这里插入图片描述

点击“Goroutine analysis” 打开页面如下所示。
图中列出所有goroutine。
在这里插入图片描述

打开第一个,可以看到各阶段耗时情况表。
在这里插入图片描述

打开图中graph链接,可以看到调用关系和耗时。

在这里插入图片描述

go tool trace主要用于解决两个棘手的问题:诊断延迟问题和诊断并发问题。

延迟问题,是指程序执行过程被意外阻塞住。例如,被系统调用阻塞,被channel、muext阻塞, 或者调度器问题, 或者GC问题等。

并发问题,是指程序没有充分利用CPU。可能的原因包括,串行化、资源竞争等。

3.总结

本文介绍了pprof和trace的使用,更多详情见链接:
golang 性能剖析pprof

4.参考

[重要]go tool trace能做什么

[解惑]go tool trace可视化界面详解--An Introduction to go tool trace

Command trace

go tool pprof简述

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-06-27 Golang cpu的使用设置--GOMAXPROCS
点击右上角即可分享
微信分享提示