LoginSignup
0
0

More than 1 year has passed since last update.

Go言語 : システムパフォーマンスチェック

Last updated at Posted at 2022-10-23

概要

システムパフォーマンスを計測するログを出力するためのサンプルである。
計測サンプルとして、素数を求めるためのゴルーチンを5つ起動させて、計算が終了した時点でそのゴルーチンは終了させている。
そして、それぞれの計算が終了した時点でのシステムログを出力させている。

ライブラリ

今回は"gopsutil"というライブラリを使用している。
https://github.com/shirou/gopsutil

このライブラリは、CPUやメモリなど、さまざまな情報を取得することができる。

適宜、以下のコマンドなどでインストールしてほしい。
$ go get github.com/shirou/gopsutil/cpu

ログ出力コード

// Output system performance
func checkPerformance() {

	// CPU Performance
	ps, _ := cpu.Percent(100*time.Millisecond, false)
	cpuPercent := int(ps[0])
  
	// Memory Performance
	var ms runtime.MemStats
	runtime.ReadMemStats(&ms)
	heapMem := int(toKb(ms.HeapAlloc))
	sysMem := int(toKb(ms.Sys))
  
	// Counting Goroutines
	goroutineCount := runtime.NumGoroutine()
  
	// Log output of system performance
	fmt.Printf("CPU : %d percent, HeapMemory : %d KB, SystemMemory : %d KB, GoroutineCount : %d \n", cpuPercent, heapMem, sysMem, goroutineCount)
}

出力サンプル

$ go build -o SystemPerformanceCheck SystemPerformanceCheck.go
$ ./SystemPerformanceCheck
100000 Line Target Goroutine Wake UP
200000 Line Target Goroutine Wake UP
250000 Line Target Goroutine Wake UP
150000 Line Target Goroutine Wake UP
300000 Line Target Goroutine Wake UP
CPU : 63 percent, HeapMemory : 180 KB, SystemMemory : 9043 KB, GoroutineCount : 6
PrimeAnswer1 : 100003
CPU : 51 percent, HeapMemory : 182 KB, SystemMemory : 9299 KB, GoroutineCount : 5
PrimeAnswer2 : 150001
CPU : 40 percent, HeapMemory : 182 KB, SystemMemory : 9299 KB, GoroutineCount : 4
PrimeAnswer3 : 200003
CPU : 26 percent, HeapMemory : 183 KB, SystemMemory : 9299 KB, GoroutineCount : 3
PrimeAnswer4 : 250007
CPU : 12 percent, HeapMemory : 184 KB, SystemMemory : 9299 KB, GoroutineCount : 2
PrimeAnswer5 : 300007
CPU : 5 percent, HeapMemory : 184 KB, SystemMemory : 9299 KB, GoroutineCount : 1

GitHub

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0