nginx-ui/server/tool/cpu_usage.go
2022-02-22 13:30:39 +08:00

46 lines
1 KiB
Go

package tool
import (
"github.com/shirou/gopsutil/v3/cpu"
"runtime"
"time"
)
type cpuUsage struct {
Time time.Time `json:"x"`
Usage float64 `json:"y"`
}
var CpuUserBuffer []cpuUsage
var CpuTotalBuffer []cpuUsage
func RecordCpuUsage() {
for {
cpuTimesBefore, _ := cpu.Times(false)
time.Sleep(1000 * time.Millisecond)
cpuTimesAfter, _ := cpu.Times(false)
threadNum := runtime.GOMAXPROCS(0)
cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
cpuUserUsage *= 100
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
cpuSystemUsage *= 100
now := time.Now()
u := cpuUsage{
Time: now,
Usage: cpuUserUsage,
}
CpuUserBuffer = append(CpuUserBuffer, u)
s := cpuUsage{
Time: now,
Usage: cpuUserUsage + cpuSystemUsage,
}
CpuTotalBuffer = append(CpuTotalBuffer, s)
if len(CpuUserBuffer) > 200 {
CpuUserBuffer = CpuUserBuffer[1:]
}
if len(CpuTotalBuffer) > 200 {
CpuTotalBuffer = CpuTotalBuffer[1:]
}
}
}