首页 > 其他分享 >Go的pprof使用

Go的pprof使用

时间:2023-06-22 19:37:46浏览次数:56  
标签:http cpuprofile pprof go num fibonacci 使用 Go

Go 中监控代码性能的有两个包:

  • net/http/pprof
  • runtime/pprof

这两个包都是可以监控代码性能的, 只不过net/http/pprof是通过http端口方式暴露出来的,内部封装的仍然是runtime/pprof。

 

runtime/pprof 的用法示例

这里的例子我们用 递归实现的斐波纳契数列来测试性能,斐波纳契数列 的代码如下:

package main    
import (     
    "fmt"     
)     
func main() {     
    for i := 0; i < 10; i++ {     
        nums := fibonacci(i)     
        fmt.Println(nums)     
    }     
}     
func fibonacci(num int) int {     
    if num < 2 {     
        return 1     
    }     
    return fibonacci(num-1) + fibonacci(num-2)     
}

我们对这里的代码做了部分改造,以便可以采集性能数据:

package main 
import (    
    "flag"     
    "fmt"     
    "log"     
    "os"     
    "runtime/pprof"     
) var (    
    cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file.")     
) func main() {    
    log.Println("begin")     
    flag.Parse()     if *cpuprofile != "" {    
        f, err := os.Create(*cpuprofile)     
        if err != nil {     
            log.Fatal(err)     
        }         pprof.StartCPUProfile(f)    
        defer pprof.StopCPUProfile()     
    }     for i := 0; i < 30; i++ {    
        nums := fibonacci(i)     
        fmt.Println(nums)     
    }     
} func fibonacci(num int) int {    
    if num < 2 {     
        return 1     
    }     
    return fibonacci(num-1) + fibonacci(num-2)     
}

当 执行这个应用时,带了 cpuprofile 参数就可以记录CPU到的Profile信息。

testfibonacci.exe -cpuprofile=2.prof

Go的pprof使用_Go

我们随后使用这个prof 信息做性能分析图了(需要安装graphviz)

Graphviz 下载地址: http://www.graphviz.org/Download_windows.php

Graphviz 安装完成后,需要把bin目录放入 path 参数中。比如我这里 win7下安装目录是: C:\Program Files (x86)\Graphviz2.38\bin 。

使用go tool pprof (应用程序) (应用程序的prof文件)   我这里是: go tool pprof testfibonacci.exe 2.prof
进入到pprof,使用web命令就会生成svg文件,svg文件是可以在浏览器下看的。像这个样子:

Go的pprof使用_github_02

这时候产生的 svg 图像如下:

Go的pprof使用_Go_03

 

net/http/pprof 的用法示例:

我这里以 gotour 为例, 我用的是这个中英文混排的版本:  https://github.com/zhanming/go-tour-cn 

修改 D:\mycodes\golang\src\github.com\zhanming\go-tour-cn\gotour.cn 这个目录的   local.go 文件, 在引入包时,增加下面一行代码:

_ "net/http/pprof"

然后编译执行这个站点,我们在访问 http://localhost:3999/debug/pprof/ 时候,就可以看到下面信息。

Go的pprof使用_github_04

想要生成CPU状态分析图,调用go tool pprof http://localhost:3999/debug/pprof/profile

就会进入30秒的profile收集时间,在这段事件内猛刷新点击go-tour浏览器上的页面,尽量让cpu占用性能产生数据。

Go的pprof使用_github_05

使用 web 命令生产的图如下:

Go的pprof使用_github_06

 

有关go tool pprof 命令相关的内容可以参考: 

https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md

它的主要命令如下:

表 pprof工具在交互模式下支持的命令

Go的pprof使用_ci_07


补充
如果你的go程序不是web服务器,而是一个服务进程,那么你也可以选择使用net/http/pprof包,同样引入包net/http/pprof,然后在开启另外一个goroutine来开启端口监听。

比如:

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


参考资料:

Go的pprof使用


标签:http,cpuprofile,pprof,go,num,fibonacci,使用,Go
From: https://blog.51cto.com/u_15588078/6535314

相关文章

  • Golang 做的几个跟性能有关的工具
    1、Vegetahttps://github.com/tsenart/vegetaVegeta是一个万能的HTTP负载测试工具,提供命令行工具和一个开发包。使用方法:$vegetaattack-hUsageofattack:-duration=10s:Durationofthetest-header=:Targetsrequestheader-ordering="random":Attackorderi......
  • win下 golang 跨平台编译
    本篇文章是win下的跨平台编译。安装GCC编译器(MinGW)我们需要GCC编译环境,这里我选择的是:MinGW。mac下安装了Xcode自动就带了GCC,win下没有,所以我们用MinGW这个最小化安装的GCC。MinGW官网提供了一种自动在线下载安装的gui小程序,http://sourceforge.net/projects/mingw/files/Install......
  • Android 使用 TableLayout 布局拉伸宽度
    布局文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent" android:lay......
  • VSCODE cannot find package "GOPROJECT/src/chapter1/model" in any of解决方法
    环境:win10go1.20问题描述:在go项目中想要导入自己的其他包的方法或变量,保存后提示cannotfindpackage"GOPROJECT/src/chapter1/model"inanyof: D:\VScode\language\Go\src\GOPROJECT\src\chapter1\model(from$GOROOT) C:\Users\艾坤\go\src\GOPROJECT\src\chapt......
  • virtualenv的安装和使用
    转载请注明来源:http://www.eword.name/Author:ewordEmail:[email protected]的安装和使用一、virtualenv的作用在开发Python应用程序的时候,每个应用可能需要各自拥有一套“独立”的Python运行环境,来使用各自需要的不同Python版本。virtualenv就是用来为一个应用......
  • virtualenv的安装和使用
    转载请注明来源:http://www.eword.name/Author:ewordEmail:[email protected]的安装和使用一、virtualenv的作用在开发Python应用程序的时候,每个应用可能需要各自拥有一套“独立”的Python运行环境,来使用各自需要的不同Python版本。virtualenv就是用来为一个应用......
  • django中使用redis
    django中使用redis方法1,通用安装redis#pipinstallredis#1写一个连接池 importredis.ConnectionPool(host='xx.xx.xx.xx',port=6379,password='xxx',max_connections=1000)#2在使用地方导入即可 conn=redis.Redis(connection_pool=pool)conn.incr(�......
  • [转]ubuntu20.04使用dev-sidecar找不到安装证书
    火狐、chrome等浏览器不走系统证书,火狐、谷歌浏览器必须在浏览器上安装证书然后死活找不到证书,搜索了整个目录也没有。原来是我的显示隐藏文件没打开。打开目录的“显示隐藏文件“的方法如下图所示:打开显示隐藏文件属性之后,dev-sidecar.ca.crt就出来了,如下图所示: ......
  • redis使用事务
    redis管道#事务--》四大特性-原子性-一致性-隔离性-持久性#redis使用事务importrediscon=redis.Redis()p=con.pipeline(transacction=True)#使用事务p.multi()开启事务#任务p.execute()p.close()......
  • redis简单介绍和使用
    redis安装"""1、官网下载:安装包或是绿色面安装2、安装并配置环境变量#官网:https://redis.io/ -下载完是源代码:c语言源码:https://redis.io/download/#redis-stack-downloads-最稳定:6.x-最新7.x#中文网:http://redis.cn/download.html -上面最新只到5.x......