mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
45 lines
1 KiB
Go
45 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)
|
|
cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
|
|
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:]
|
|
}
|
|
// time.Sleep(1 * time.Second)
|
|
}
|
|
}
|