zoukankan      html  css  js  c++  java
  • golang---查看程序运行时状态

    1. 介绍

    对于生产环境中运行的进程,可以用 Go 内置的性能分析工具 pprof 窥测进程的当前状况。

    Profiling Go Programs 很好地演示了用 pprof 找到性能瓶颈的过程,这里只演示简单用法。

    2. 启用实时的pprof

    2.1 启用实时的 pprof

    非常简单,只需要引入 "net/http/pprof",然后启动 http server 就可以了:

    import (
        "fmt"
        "log"
        "net/http"
        _ "net/http/pprof"
        "time"
    )
    
    func Write(num int, c chan int) {
        for {
            c <- num
        }
    }
    
    func main() {
        go func() {
            log.Println(http.ListenAndServe("localhost:6060", nil))
        }()
    
        c := make(chan int)
        go Write(10, c)
        go Write(20, c)
    
        for {
            select {
            case v := <-c:
                fmt.Printf("receive %d
    ", v)
                time.Sleep(2 * time.Second)
            }
        }
    }
    

    直接用浏览器打开 http://127.0.0.1:6060/debug/pprof/ 查看:

    其中 debug/pprof/profile 是 cpu 采样文件,访问时触发,用 seonds 参数控制采集持续时间:

    # 默认是 30 秒
    http://localhost:6060/debug/pprof/profile?seconds=30
    

    2.2 对于非常驻环境

    对于非常驻运行的 Go 语言程序,可以在程序添加代码,经 pprof 信息写入文件中:

    var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
    
    func main() {
        flag.Parse()
        if *cpuprofile != "" {
            f, err := os.Create(*cpuprofile)
            if err != nil {
                log.Fatal(err)
            }
            pprof.StartCPUProfile(f)
            defer pprof.StopCPUProfile()
        }
        ...
    

    3. 如何使用pprof

    如何使用 pprof 才是重点,除了 profiletrace ,其它 url 可以直接在浏览器中查看,
    profiletrace 是两个采样文件要分别用 pproftrace 工具查看。

    对于离线文件:

    $ go tool pprof havlak1 havlak1.prof
    Welcome to pprof!  For help, type 'help'.
    (pprof)
    

    对于在线地址,以 cpu 采样为例(为了采集到数据把上面程序中的 sleep 时间调整为 0):

    $ go tool pprof http://localhost:6060/debug/pprof/profile   # 30-second CPU profile
    

    web 命令绘制采样图并用浏览器打开 ,如果遇到下面错误,安装 graphviz:

    (pprof) web
    failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH
    

    MACOS上:

    brew install graphviz
    

    3. trace查看

  • 相关阅读:
    使用 Spring data redis 结合 Spring cache 缓存数据配置
    Spring Web Flow 笔记
    Linux 定时实行一次任务命令
    css js 优化工具
    arch Failed to load module "intel"
    go 冒泡排序
    go (break goto continue)
    VirtualBox,Kernel driver not installed (rc=-1908)
    go运算符
    go iota
  • 原文地址:https://www.cnblogs.com/double12gzh/p/12272486.html
Copyright © 2011-2022 走看看