zoukankan      html  css  js  c++  java
  • golang程序性能分析

    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简述

  • 相关阅读:
    spark map和mapPartitions的区别
    RDD实例
    Scala类和对象
    Scala的集合框架
    Scala的to和until
    用不同RequestMethod制作出restful规范的应用
    isc-dhcp-server的分配的地址列表在哪,linux/树莓派做无线路由器怎么查看已连接设备
    在java中实现通过身份证号码判断籍贯的小工具类
    解决eclipse写jsp javaee时自动代码提示弹出过慢的问题
    第一篇博兼测试博之稍稍修改一下博客主题
  • 原文地址:https://www.cnblogs.com/lanyangsh/p/13198763.html
Copyright © 2011-2022 走看看