如何监控Golang中的cpu、内存和goroutine分配

900 阅读6分钟

如果你想知道你的Go程序是如何使用cpu、内存和goroutine的,你可以使用运行时组件。了解系统资源的使用情况总是好的,这样你就可以扩大或缩小你的应用程序的规模。它也有助于调试应用程序中的潜在错误。然而,我建议你不要在生产中使用这个功能,因为它显然也会使用系统资源。如果你必须这样做,你最好把数据运送到某种消息队列中,这样它就不是一个阻塞性操作。另外,你可以存储和可视化数据,以便调查。我建议你阅读一篇非常有价值的高性能围棋研讨会的文章。

例子

package main

import (
	"time"

	"internal/monitor"
)

func main() {
	go monitor.System()

	// Simulate never ending client requests.
	for {
		// Pretend like each request task (goroutine) takes 10 seconds.
		go func() {
			time.Sleep(10 * time.Second)
		}()

		// Wait 1 second before creating a new request.
		time.Sleep(time.Second)
	}
}
package monitor

import (
	"log"
	"runtime"
	"time"
)

// Print system resource usage every 2 seconds.
func System() {
	mem := &runtime.MemStats{}

	for {
		cpu := runtime.NumCPU()
		log.Println("CPU:", cpu)

		rot := runtime.NumGoroutine()
		log.Println("Goroutine:", rot)

		// Byte
		runtime.ReadMemStats(mem)
		log.Println("Memory:", mem.Alloc)

		time.Sleep(2 * time.Second)
		log.Println("-------")
	}
}

测试

2020/04/25 21:32:44 CPU: 4
2020/04/25 21:32:44 Goroutine: 3
2020/04/25 21:32:44 Memory: 87096
2020/04/25 21:32:46 -------
2020/04/25 21:32:46 CPU: 4
2020/04/25 21:32:46 Goroutine: 4
2020/04/25 21:32:46 Memory: 88272
2020/04/25 21:32:48 -------
2020/04/25 21:32:48 CPU: 4
2020/04/25 21:32:48 Goroutine: 6
2020/04/25 21:32:48 Memory: 91688
2020/04/25 21:32:50 -------
2020/04/25 21:32:50 CPU: 4
2020/04/25 21:32:50 Goroutine: 8
2020/04/25 21:32:50 Memory: 92712
2020/04/25 21:32:52 -------
2020/04/25 21:32:52 CPU: 4
2020/04/25 21:32:52 Goroutine: 10
2020/04/25 21:32:52 Memory: 93944
2020/04/25 21:32:54 -------
2020/04/25 21:32:54 CPU: 4
2020/04/25 21:32:54 Goroutine: 11
2020/04/25 21:32:54 Memory: 94984
2020/04/25 21:32:56 -------
2020/04/25 21:32:56 CPU: 4
2020/04/25 21:32:56 Goroutine: 11
2020/04/25 21:32:56 Memory: 95736
2020/04/25 21:32:58 -------
2020/04/25 21:32:58 CPU: 4
2020/04/25 21:32:58 Goroutine: 11
2020/04/25 21:32:58 Memory: 96344
2020/04/25 21:33:00 -------
2020/04/25 21:33:00 CPU: 4
2020/04/25 21:33:00 Goroutine: 11
2020/04/25 21:33:00 Memory: 96552
2020/04/25 21:33:02 -------
2020/04/25 21:33:02 CPU: 4
2020/04/25 21:33:02 Goroutine: 11
2020/04/25 21:33:02 Memory: 96760
2020/04/25 21:33:04 -------
2020/04/25 21:33:04 CPU: 4
2020/04/25 21:33:04 Goroutine: 11
2020/04/25 21:33:04 Memory: 96968
2020/04/25 21:33:06 -------
2020/04/25 21:33:06 CPU: 4
2020/04/25 21:33:06 Goroutine: 12
2020/04/25 21:33:06 Memory: 97176
2020/04/25 21:33:08 -------
2020/04/25 21:33:08 CPU: 4
2020/04/25 21:33:08 Goroutine: 12
2020/04/25 21:33:08 Memory: 97384
2020/04/25 21:33:10 -------
2020/04/25 21:33:10 CPU: 4
2020/04/25 21:33:10 Goroutine: 12
2020/04/25 21:33:10 Memory: 97592
2020/04/25 21:33:12 -------
2020/04/25 21:33:12 CPU: 4
2020/04/25 21:33:12 Goroutine: 12
2020/04/25 21:33:12 Memory: 97800
2020/04/25 21:33:14 -------
2020/04/25 21:33:14 CPU: 4
2020/04/25 21:33:14 Goroutine: 12
2020/04/25 21:33:14 Memory: 98776
2020/04/25 21:33:16 -------
2020/04/25 21:33:16 CPU: 4
2020/04/25 21:33:16 Goroutine: 12
2020/04/25 21:33:16 Memory: 99000
2020/04/25 21:33:19 -------
2020/04/25 21:33:19 CPU: 4
2020/04/25 21:33:19 Goroutine: 12
2020/04/25 21:33:19 Memory: 99208
2020/04/25 21:33:21 -------
2020/04/25 21:33:21 CPU: 4
2020/04/25 21:33:21 Goroutine: 12
2020/04/25 21:33:21 Memory: 99432
2020/04/25 21:33:23 -------
2020/04/25 21:33:23 CPU: 4
2020/04/25 21:33:23 Goroutine: 12
2020/04/25 21:33:23 Memory: 99640
2020/04/25 21:33:25 -------
2020/04/25 21:33:25 CPU: 4
2020/04/25 21:33:25 Goroutine: 12
2020/04/25 21:33:25 Memory: 99864
2020/04/25 21:33:27 -------
2020/04/25 21:33:27 CPU: 4
2020/04/25 21:33:27 Goroutine: 12
2020/04/25 21:33:27 Memory: 100072
2020/04/25 21:33:29 -------
2020/04/25 21:33:29 CPU: 4
2020/04/25 21:33:29 Goroutine: 12
2020/04/25 21:33:29 Memory: 100280
2020/04/25 21:33:31 -------

...