【代码片段】定时记录CPU使用率并保存为CSV

👆关注微信公众号,获取更多编程内容


原文链接 : [https://blog.zhoutao123.com/#/blog/article/64])(https://blog.zhoutao123.com/#/blog/article/64)

github仓库地址: https://github.com/taoes/performance_record

记录CPU使用率

package main

import (
	"encoding/csv"
	"github.com/astaxie/beego/logs"
	"github.com/shirou/gopsutil/cpu"
	"os"
	"strconv"
	"sync"
	"time"
)

// 定义数据缓冲文件名称
const csvFile string = "CSV_RECORD.csv"

var wg sync.WaitGroup

var execSuccess = false

func main() {

	chanExec := make(chan int, 10)
	wg.Add(3)

	go sendSearchCommand(chanExec)
	go performance(chanExec)
	logs.Debug("启动检测线程完成")
	logs.Debug("正在启动目标程序信息")
	time.Sleep(time.Second * 5)
	go execCommand()
	logs.Debug("目标程序启动完成,正在收集数据,请稍等")
	wg.Wait()
}

// 执行命令
func execCommand() {
	// 延时5小时,记录数据
	time.Sleep(time.Hour * 5)
	execSuccess = true
}

// 一直发送数据直到程序执行完毕
func sendSearchCommand(ints chan int) {
	var index = 0
	//程序未执行完毕,每隔一秒发送一次数据
	for !execSuccess {
		ints <- index
		time.Sleep(time.Second)
		index++
	}
	ints <- -1
	logs.Debug("程序执行完成.....")
	defer wg.Done()
}

// 计算内存以及CPU使用率
func performance(chanExec chan int) {

	// CPU 核数
	count, _ := cpu.Counts(true)

	// 计算CPU的使用率
	csvFile, _ := os.Create(csvFile)
	defer csvFile.Close()
	defer wg.Done()

	_, _ = csvFile.WriteString("\xEF\xBB\xBF")
	write := csv.NewWriter(csvFile)

	var sum float64 = 0
	var dataStr []string
	dataStr = append(dataStr, "序号")
	for coreIndex := 1; coreIndex < count+1; coreIndex++ {
		dataStr = append(dataStr, "核心"+strconv.Itoa(coreIndex)+"使用率")
	}
	dataStr = append(dataStr, "平均使用率")
	_ = write.Write(dataStr)
	write.Flush()

	for chanNumber := range chanExec {
		if chanNumber == -1 {
			break
		}
		sum = 0
		logs.Debug("第", chanNumber, "次收集CPU数据")

		// CPU 采样案例代码
		data, _ := cpu.Percent(time.Second*2, true)
		dataStr = dataStr[0:0]
		dataStr = append(dataStr, strconv.Itoa(chanNumber))
		for i := 0; i < len(data); i++ {
			sum = sum + data[i]
			dataStr = append(dataStr, strconv.FormatFloat(data[i], 'f', 6, 64))
		}
		rate := sum / (float64(count))
		logs.Debug("第", chanNumber, "次收集CPU数据,当前平均使用率:", rate)
		dataStr = append(dataStr, strconv.FormatFloat(rate, 'f', 6, 64))
		_ = write.Write(dataStr)
		write.Flush()
	}

	logs.Info("====================数据汇总完成====================")
}

代码依赖(gopm版本管理)

  • gopm get -g -v github.com/shirou/gopsutil/

如果需要打包Windows 版本需要另外安装如下依赖

  • gopm get -g -v github.com/StackExchange/wmi
  • gopm get -g -v github.com/go-ole/go-ole
  • gopm get -g -v github.com/go-ole/go-ole/oleutil

打包方式

  • MacOS 下打包
$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./src/Main.go

$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build ./src/Main.go
  • Linux 平台
$ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build ./src/Main.go

$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build ./src/Main.go
  • windows 平台
$ SET CGO_ENABLED=0 SET GOOS=darwin3 SET GOARCH=amd64 go build ./src/Main.go

$ SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build ./src/Main.go
posted @ 2019-11-26 09:15  燕归来兮  阅读(501)  评论(0编辑  收藏  举报