Skip to content

Commit d7f5902

Browse files
committed
fix getProcessCpuStat return value (in percentage) breaks compatibility.
1 parent 0807185 commit d7f5902

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

api/init.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric"
2626
"github.com/alibaba/sentinel-golang/util"
2727
"github.com/pkg/errors"
28+
29+
_ "go.uber.org/automaxprocs"
2830
)
2931

3032
// Initialization func initialize the Sentinel's runtime environment, including:

core/system_metric/sys_metric_stat.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package system_metric
1616

1717
import (
1818
"os"
19+
"runtime"
1920
"sync"
2021
"sync/atomic"
2122
"time"
@@ -176,6 +177,10 @@ func retrieveAndUpdateCpuStat() {
176177
return
177178
}
178179

180+
// fix getProcessCpuStat return value (in percentage) breaks compatibility.
181+
cpuNum := runtime.NumCPU()
182+
cpuPercent = cpuPercent / float64(cpuNum) / 100.0
183+
179184
cpuRatioGauge.Set(cpuPercent)
180185

181186
currentCpuUsage.Store(cpuPercent)

core/system_metric/sys_metric_stat_test.go

+27-15
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,8 @@ func TestCurrentCpuUsage(t *testing.T) {
4747
assert.True(t, util.Float64Equals(v, cpuUsage))
4848
}
4949

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()
6552
got, err := getProcessCpuStat()
6653
if err != nil {
6754
t.Error(err)
@@ -83,3 +70,28 @@ func Test_getProcessCpuStat(t *testing.T) {
8370
assert.True(t, int(got) > 0)
8471
time.Sleep(time.Millisecond * 200)
8572
}
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+
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ require (
88
github.com/pkg/errors v0.9.1
99
github.com/prometheus/client_golang v1.9.0
1010
github.com/shirou/gopsutil/v3 v3.21.6
11-
github.com/stretchr/testify v1.6.1
11+
github.com/stretchr/testify v1.7.1
12+
go.uber.org/automaxprocs v1.5.0 // indirect
1213
go.uber.org/multierr v1.5.0
1314
gopkg.in/yaml.v2 v2.3.0
1415
)

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6J
213213
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
214214
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
215215
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
216+
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
216217
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
217218
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
218219
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
@@ -270,6 +271,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
270271
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
271272
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
272273
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
274+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
275+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
276+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
273277
github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4=
274278
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
275279
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
@@ -287,6 +291,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
287291
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
288292
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
289293
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
294+
go.uber.org/automaxprocs v1.5.0 h1:UctqgMvxKU3PHD5qSSlKkOcTAptYykGIpJlEBYNGPvM=
295+
go.uber.org/automaxprocs v1.5.0/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
290296
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
291297
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
292298
go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
@@ -426,6 +432,8 @@ gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
426432
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
427433
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
428434
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
435+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
436+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
429437
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
430438
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
431439
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

0 commit comments

Comments
 (0)