File tree 2 files changed +32
-15
lines changed
2 files changed +32
-15
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ package system_metric
16
16
17
17
import (
18
18
"os"
19
+ "runtime"
19
20
"sync"
20
21
"sync/atomic"
21
22
"time"
@@ -176,6 +177,10 @@ func retrieveAndUpdateCpuStat() {
176
177
return
177
178
}
178
179
180
+ // fix getProcessCpuStat return value (in percentage) breaks compatibility.
181
+ cpuNum := runtime .NumCPU ()
182
+ cpuPercent = cpuPercent / float64 (cpuNum ) / 100.0
183
+
179
184
cpuRatioGauge .Set (cpuPercent )
180
185
181
186
currentCpuUsage .Store (cpuPercent )
Original file line number Diff line number Diff line change @@ -47,21 +47,8 @@ func TestCurrentCpuUsage(t *testing.T) {
47
47
assert .True (t , util .Float64Equals (v , cpuUsage ))
48
48
}
49
49
50
- func Test_getProcessCpuStat (t * testing.T ) {
51
- wg := & sync.WaitGroup {}
52
- wg .Add (1 )
53
- go func () {
54
- i := 0
55
- wg .Done ()
56
- for i < 10000000000 {
57
- i ++
58
- if i == 1000000000 {
59
- i = 0
60
- }
61
- }
62
- }()
63
- wg .Wait ()
64
-
50
+ func TestGetProcessCpuStat (t * testing.T ) {
51
+ upraiseCpuRate ()
65
52
got , err := getProcessCpuStat ()
66
53
if err != nil {
67
54
t .Error (err )
@@ -83,3 +70,28 @@ func Test_getProcessCpuStat(t *testing.T) {
83
70
assert .True (t , int (got ) > 0 )
84
71
time .Sleep (time .Millisecond * 200 )
85
72
}
73
+
74
+ func TestRetrieveAndUpdateCpuStatReturnValueRange (t * testing.T ) {
75
+ // Initial cpu retrieval.
76
+ retrieveAndUpdateCpuStat ()
77
+ upraiseCpuRate ()
78
+ time .Sleep (time .Millisecond * 200 )
79
+ retrieveAndUpdateCpuStat ()
80
+ assert .True (t , true , CurrentCpuUsage () < 1.0 )
81
+ }
82
+
83
+ func upraiseCpuRate () {
84
+ wg := & sync.WaitGroup {}
85
+ wg .Add (1 )
86
+ go func () {
87
+ i := 0
88
+ wg .Done ()
89
+ for i < 10000000000 {
90
+ i ++
91
+ if i == 1000000000 {
92
+ i = 0
93
+ }
94
+ }
95
+ }()
96
+ wg .Wait ()
97
+ }
You can’t perform that action at this time.
0 commit comments