golang Diagnostics

方法

  • profiling:查看内存和cpu的使用率,定位高频率的函数,发现性能瓶颈
  • tracing:从整个请求的角度看,观察请求穿过的所有span,定位请求延迟,尤其是在链路很长的分布式系统中很有用
  • debugging:可以暂停程序的执行,观察执行过程中的变量和调用栈。最常见的logging无需暂停程序
  • Runtime statistics and events:类似于prometheus暴露的metrics,是从高纬度的角度看系统的状态,从而发现趋势

profiling

  • cpu: CPU profile determines where a program spends its time while actively consuming CPU cycles (as opposed to while sleeping or waiting for I/O).
  • heap: Heap profile reports memory allocation samples; used to monitor current and historical memory usage, and to check for memory leaks.
  • threadcreate: Thread creation profile reports the sections of the program that lead the creation of new OS threads.
  • goroutine: Goroutine profile reports the stack traces of all current goroutines.
  • block: Block profile shows where goroutines block waiting on synchronization primitives (including timer channels). Block profile is not enabled by default; use runtime.SetBlockProfileRate to enable it.
  • mutex: Mutex profile reports the lock contentions. When you think your CPU is not fully utilized due to a mutex contention, use this profile. Mutex profile is not enabled by default, see runtime.SetMutexProfileFraction to enable it.

linux下面还可以使用perf工具

因为profiling各种数据会相互影响,所以最好每次只profile一个数据

Debugging

delve或者gdb,delve要推荐一些,但是都很高端啊

Runtime statistics and events

  • runtime.ReadMemStats reports the metrics related to heap allocation and garbage collection. Memory stats are useful for monitoring how much memory resources a process is consuming, whether the process can utilize memory well, and to catch memory leaks.
  • debug.ReadGCStats reads statistics about garbage collection. It is useful to see how much of the resources are spent on GC pauses. It also reports a timeline of garbage collector pauses and pause time percentiles.
  • debug.Stack returns the current stack trace. Stack trace is useful to see how many goroutines are currently running, what they are doing, and whether they are blocked or not.
  • debug.WriteHeapDump suspends the execution of all goroutines and allows you to dump the heap to a file. A heap dump is a snapshot of a Go process’ memory at a given time. It contains all allocated objects as well as goroutines, finalizers, and more.
  • runtime.NumGoroutine returns the number of current goroutines. The value can be monitored to see whether enough goroutines are utilized, or to detect goroutine leaks.

看来runtime pkg很有用啊

Execution tracer

Tracer is useful to:

Understand how your goroutines execute.
Understand some of the core runtime events such as GC runs.
Identify poorly parallelized execution.

可以看出锁竞争导致的除串行化

Profiling Go Programs

demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值