/
profile.go
71 lines (63 loc) · 1.3 KB
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package util
import (
"fmt"
"runtime"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/mem"
)
const (
Cpu = "cpu"
Memory = "mem"
Disk = "disk"
Goroutine = "goroutine"
)
func GetCpuPercent() (float64, *Err) {
percent, e := cpu.Percent(time.Second, false)
if e != nil {
return 0, WrapErr(EcServiceErr, e)
}
return percent[0], nil
}
func GetMemPercent() float64 {
memInfo, _ := mem.VirtualMemory()
return memInfo.UsedPercent
}
func GetDiskPercent() float64 {
parts, _ := disk.Partitions(true)
diskInfo, _ := disk.Usage(parts[0].Mountpoint)
return diskInfo.UsedPercent
}
func StartProfile(dur time.Duration, receiver chan<- M) {
go func() {
sampling(receiver)
ticker := time.NewTicker(dur)
for {
select {
case <-_Ctx.Done():
ticker.Stop()
return
case <-ticker.C:
sampling(receiver)
}
}
}()
}
func sampling(receiver chan<- M) {
status := M{
Memory: float32(GetMemPercent()),
Disk: float32(GetDiskPercent()),
Goroutine: uint32(runtime.NumGoroutine()),
}
if runtime.GOOS != "darwin" { //暂时不支持
cp, err := GetCpuPercent()
if err == nil {
status[Cpu] = float32(cp)
} else {
fmt.Println("get cpu fail:")
fmt.Println(err.Error())
}
}
receiver <- status
}