美文网首页
golang性能分析go tool pprof

golang性能分析go tool pprof

作者: 杀破魂 | 来源:发表于2018-04-23 16:34 被阅读258次

在Go语言中,我们可以通过标准库的代码包runtimeruntime/pprof中的程序来生成三种包含实时性数据的概要文件,分别是CPU概要文件、内存概要文件和程序阻塞概要文件。

CPU概要文件

我们可以通过从样本记录中分析出哪些代码是计算时间最长或者说最耗CPU资源的部分了。我们可以通过以下代码启动对CPU使用情况的记录

func startCPUProfile() {
    if *cpuProfile != "" {
        f, err := os.Create(*cpuProfile)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Can not create cpu profile output file: %s",
                err)
            return
        }
        if err := pprof.StartCPUProfile(f); err != nil {
            fmt.Fprintf(os.Stderr, "Can not start cpu profile: %s", err)
            f.Close()
            return
        }
    }
}

通过使用startCPUProfile,创建一个用于存放CPU使用情况记录的文件。需要注意的是,只有CPU概要文件的绝对路径有效时此函数才会开启记录操作。

func stopCPUProfile() {
    if *cpuProfile != "" {
        pprof.StopCPUProfile()
    }
}

在某一时刻停止CPU使用情况记录操作

内存概要文件

内存概要文件用于保存在用户程序执行期间的内存使用情况。这里所说的内存使用情况,其实就是程序运行过程中堆内存的分配情况。Go语言运行时系统会对用户程序运行期间的所有的堆内存分配进行记录。不论在取样的那一时刻、堆内存已用字节数是否有增长,只要有字节被分配且数量足够,分析器就会对其进行取样。开启内存使用情况记录的方式如下:

func startMemProfile() {
    if *memProfile != "" && *memProfileRate > 0 {
        runtime.MemProfileRate = *memProfileRate
    }
}

在默认情况下,内存使用情况的取样数据只会被保存在运行时内存中,而保存到文件的操作只能由我们自己来完成。请看如下代码:

func stopMemProfile() {
    if *memProfile != "" {
        f, err := os.Create(*memProfile)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Can not create mem profile output file: %s", err)
            return
        }
        if err = pprof.WriteHeapProfile(f); err != nil {
            fmt.Fprintf(os.Stderr, "Can not write %s: %s", *memProfile, err)
        }
        f.Close()
    }
}

程序阻塞概要文件

程序阻塞概要文件用于保存用户程序中的Goroutine阻塞事件的记录。我们来看开启这项操作的方法:

func startBlockProfile() {
    if *blockProfile != "" && *blockProfileRate > 0 {
        runtime.SetBlockProfileRate(*blockProfileRate)
    }
}

在默认情况下,每发生一次Goroutine阻塞事件,分析器就会取样一次。与内存使用情况记录一样,运行时系统对Goroutine阻塞事件的取样操作也会贯穿于用户程序的整个运行期。但是,如果我们通过runtime.SetBlockProfileRate函数将这个取样间隔设置为0或者负数,那么这个取样操作就会被取消

我们在程序结束之前可以将被保存在运行时内存中的Goroutine阻塞事件记录存放到指定的文件中。代码如下:

func stopBlockProfile() {
    if *blockProfile != "" && *blockProfileRate >= 0 {
        f, err := os.Create(*blockProfile)
        if err != nil {
            fmt.Fprintf(os.Stderr, "Can not create block profile output file: %s", err)
            return
        }
        if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
            fmt.Fprintf(os.Stderr, "Can not write %s: %s", *blockProfile, err)
        }
        f.Close()
    }
}

在创建程序阻塞概要文件之后,stopBlockProfile函数会先通过函数pprof.Lookup将保存在运行时内存中的内存使用情况记录取出,并在记录的实例上调用WriteTo方法将记录写入到文件中。

相关文章

  • golang性能分析go tool pprof

    在Go语言中,我们可以通过标准库的代码包runtime和runtime/pprof中的程序来生成三种包含实时性数据...

  • Golang 使用pprof分析性能

    参考Golang 使用pprof分析goweb的性能问题 go的pprof可以用来对服务的性能进行检测,其中net...

  • go学习笔记

    go build,go test,go tool pprof ,go tool cover。建议了解 -race ...

  • Go tooling, GC和grpc常用的命令

    Go tooling go tool pprof -web ~/x.profile go tool traceTh...

  • pprof接口隐藏/认证

    pprof是golang提供的性能分析工具,这里就不过多介绍了。 使用方式很简单,导入pprof包即可 pprof...

  • pprof 工具使用

    pprof golang pprof是golang的可视化和性能分析的工具。其提供了可视化的web页面,火焰图等更...

  • golang内存分析工具

    go tool pprof http://localhost:6060/debug/pprof/heap 安装好g...

  • golang pprof 使用简介

    golang 的 pprof 是性能分析的神器,包括交互命令行和 UI 图像化的分析。主要来看看 pprof 支持...

  • golang pprof

    go test -bench . -cpuprofile=cpu.outgo tool pprof cpu.out...

  • pprof 的使用

    基本介绍 pprof 是在做性能优化前的性能分析工具。 安装: go get -u github.com/goog...

网友评论

      本文标题:golang性能分析go tool pprof

      本文链接:https://www.haomeiwen.com/subject/bysllftx.html