From d795a2ac4fab4f559c2518f9e742990ae4601030 Mon Sep 17 00:00:00 2001 From: v-heyuan Date: Mon, 22 Jan 2024 11:39:30 +0800 Subject: [PATCH] fix:cpu rate bug --- api/init.go | 1 + core/system_metric/sys_metric_stat.go | 2 ++ core/system_metric/sys_metric_stat_test.go | 41 ++++++++++++++-------- go.mod | 3 +- go.sum | 11 ++++-- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/api/init.go b/api/init.go index 27229201f..d74e19a72 100644 --- a/api/init.go +++ b/api/init.go @@ -25,6 +25,7 @@ import ( metric_exporter "github.com/alibaba/sentinel-golang/exporter/metric" "github.com/alibaba/sentinel-golang/util" "github.com/pkg/errors" + _ "go.uber.org/automaxprocs" ) // Initialization func initialize the Sentinel's runtime environment, including: diff --git a/core/system_metric/sys_metric_stat.go b/core/system_metric/sys_metric_stat.go index c87514199..d6e29f0ed 100644 --- a/core/system_metric/sys_metric_stat.go +++ b/core/system_metric/sys_metric_stat.go @@ -176,6 +176,8 @@ func retrieveAndUpdateCpuStat() { return } + cpuPercent = cpuPercent / 100.0 + cpuRatioGauge.Set(cpuPercent) currentCpuUsage.Store(cpuPercent) diff --git a/core/system_metric/sys_metric_stat_test.go b/core/system_metric/sys_metric_stat_test.go index e3a6cdf3b..86fba3f7e 100644 --- a/core/system_metric/sys_metric_stat_test.go +++ b/core/system_metric/sys_metric_stat_test.go @@ -47,20 +47,8 @@ func TestCurrentCpuUsage(t *testing.T) { assert.True(t, util.Float64Equals(v, cpuUsage)) } -func Test_getProcessCpuStat(t *testing.T) { - wg := &sync.WaitGroup{} - wg.Add(1) - go func() { - i := 0 - wg.Done() - for i < 10000000000 { - i++ - if i == 1000000000 { - i = 0 - } - } - }() - wg.Wait() +func TestGetProcessCpuStat(t *testing.T) { + upraiseCpuRate() got, err := getProcessCpuStat() if err != nil { @@ -83,3 +71,28 @@ func Test_getProcessCpuStat(t *testing.T) { assert.True(t, int(got) > 0) time.Sleep(time.Millisecond * 200) } + +func TestRetrieveAndUpdateCpuStatReturnValueRange(t *testing.T) { + // Initial cpu retrieval. + retrieveAndUpdateCpuStat() + upraiseCpuRate() + time.Sleep(time.Millisecond * 200) + retrieveAndUpdateCpuStat() + assert.True(t, true, CurrentCpuUsage() < 1.0) +} + +func upraiseCpuRate() { + wg := &sync.WaitGroup{} + wg.Add(1) + go func() { + i := 0 + wg.Done() + for i < 10000000000 { + i++ + if i == 1000000000 { + i = 0 + } + } + }() + wg.Wait() +} diff --git a/go.mod b/go.mod index f67c45a76..94b18dede 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,8 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.9.0 github.com/shirou/gopsutil/v3 v3.21.6 - github.com/stretchr/testify v1.6.1 + github.com/stretchr/testify v1.7.1 + go.uber.org/automaxprocs v1.5.3 go.uber.org/multierr v1.5.0 gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 53aa5abba..1a922233d 100644 --- a/go.sum +++ b/go.sum @@ -213,6 +213,8 @@ github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6J github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= +github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -268,8 +270,10 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4= github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI= 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= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= +go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A= @@ -424,8 +430,9 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=