zoukankan      html  css  js  c++  java
  • golang 查看程序的运行时间和CPU利用率

    (1.) 使用系统自带的time工具查看

    time -v  go run test.go
    

    (2.)使用top命令

    top -p $(pidof 二进制)
    

    (3.)GODEBUG和gctrace

    执行程序之前,添加环境变量GODEBUG='gctrace=1'来跟踪垃圾回收信息。

    GODEBUG='gctrace=1' ./xxx
    

    (4.) 利用runtime.ReadMemStats()方法

    func readMemStats(){
    	var ms runtime.MemStats
    	runtime.ReadMemStats(&ms)
    	log.Printf(Alloc:%d(bytes) HeadIdle:%d(bytes) HeadReleased:%d(bytes))
    }
    // 说明:将该方法,放到要执行函数的前后,即可查看内存情况
    

    (5.)使用pprof工具

    import(
    _ "net/http/pprof"
    )
    
    func main(){
    	// 启动pprof
    	go func(){
    		log.Println(http.ListenAndServer("0.0.0.0:10000"),nil)
    	}()
    }
    
    //(1.) 浏览器访问:
    http://127.0.0.1:10000/debug/pprof/heap?debug=1   // 查看内存情况
    
    //(2.) 使用go tool pprof工具查看
    go tool pprof [binary] [profile]
    binary:必须指向生成这个性能分析数据的二进制文件
    profile:必须是该二进制文件所生成的性能分析数据文件
    
    示例:
    go tool pprof ./demo profile
    go tool pprof ./demo profileFile:demo //如果找不到grapviz需要安装
    
    
    
    数据说明:
    flat:当前函数CPU耗时
    sun%:当前函数占用CPU耗时百分比
    cum:当前函数+调用当前函数占用的CPU总耗时
    
    (3.)先启动程序,生成proflie文件后再查看
    示例:
    ./demo
    go tool pprof http://localhost:10000/debug/pprof/profile?seconds=60
    
    【励志篇】: 古之成大事掌大学问者,不惟有超世之才,亦必有坚韧不拔之志。
  • 相关阅读:
    经典的博客有价值的博客
    关于前后端接口的异常的处理
    java重新学习记载的一些资料。
    java重新开始学习
    MFC Socket
    修复 SQLite 数据库文件
    VC++源文件编码
    VC++ 中使用 std::string 转换字符串编码
    Windows代码页、区域
    UTF-7编码
  • 原文地址:https://www.cnblogs.com/tomtellyou/p/14492433.html
Copyright © 2011-2022 走看看