GO之性能调优工具

背景: 

        最近刚好有需求需要进行调试相关的开发程序性能,并且可追溯相关的内存使用等相关情况。

  

当前发现可以使用的调试工具为Pprof,挺好使用的,下面具体介绍相关的使用:

1.   单元测试:

如果是test文件,进行调试单个的功能可以直接使用下面的方式:

Go工具集-2(定时任务,TCP客户端,RPC基本实现,PProf 调优工具,map转Struct,以及Json转Struct工具包,二进制的位处理)_gcglhd的博客-CSDN博客

里面有主题关于Pprof的相关使用。

2.  实时动态加载性能

如果是测试服部署的项目为了方便我们调试,我们可以采用的方式就是在程序中加载指定的一小段代码。并且开启协程去启动,就可以实现实时的监控相关的程序性能的消耗追踪情况。

依赖包:

	"github.com/DeanThompson/ginpprof"
    "github.com/gin-gonic/gin"

相关的代码(一般是在启动主程序的时候自动开启协程去实时获取相关的性能信息):

//开启携程监控pprof
	router := gin.Default()
	group := router.Group("/debug/pprof")
	ginpprof.WrapGroup(group)

	go router.Run("127.0.0.1:25003")

运行效果如下:

访问接口:    http://127.0.0.1:25003/debug/pprof/

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(所有当前goroutine的堆栈跟踪)
  • heap:

     A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.(活动对象的内存分配抽样。您可以指定gc GET参数,以便在获取堆样本之前运行gc。)
  • 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.(CPU配置文件。您可以以秒为单位指定持续时间。获得概要文件后,使用go工具pprof命令来研究概要文件。)
  • threadcreate:

     Stack traces that led to the creation of new OS threads(导致创建新的OS线程的堆栈跟踪)
  • 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.(当前程序执行的轨迹。您可以以秒为单位指定持续时间。获得跟踪文件后,使用go工具trace命令来研究跟踪。)

3. go run -race  xxx.go

采用race命令进行运行代码会直接打印数据竞争的异常。进而用于判断代码是否可靠,一般在开发阶段使用,线上不使用。因为消耗性能很大。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值