Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go_app): fix CPU usage by test app #2809

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/acceptance/assets/app/go_app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
)

require (
Expand Down Expand Up @@ -62,6 +61,7 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions src/acceptance/assets/app/go_app/internal/app/c_clock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package app

//#include <time.h>
import "C"

const ClocksPerSec = C.CLOCKS_PER_SEC

func GetClock() float64 {
return float64(C.clock())
}
31 changes: 17 additions & 14 deletions src/acceptance/assets/app/go_app/internal/app/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/go-logr/logr"
"golang.org/x/exp/constraints"
)

//counterfeiter:generate . CPUWaster
Expand All @@ -20,7 +19,7 @@ type CPUWaster interface {
}

type ConcurrentBusyLoopCPUWaster struct {
mu sync.Mutex
mu sync.RWMutex
isRunning bool
}

Expand Down Expand Up @@ -64,17 +63,20 @@ func CPUTests(logger logr.Logger, r *gin.RouterGroup, cpuTest CPUWaster) *gin.Ro
}

func (m *ConcurrentBusyLoopCPUWaster) UseCPU(utilisation uint64, duration time.Duration) {
geigerj0 marked this conversation as resolved.
Show resolved Hide resolved
m.mu.Lock()
defer m.mu.Unlock()
m.isRunning = true
m.startTest()

for utilisation > 0 {
// to allow to use more than one CPU, we distribute the utilisation to multiple goroutines
perGoRoutineUtilisation := min(utilisation, 100)
utilisation = utilisation - perGoRoutineUtilisation

// the core cpu wasting goroutine
go func(util uint64) {
run := time.Duration(util) * time.Microsecond / 10
sleep := time.Duration(100-util) * time.Microsecond / 10
// to achieve a desired utilisation, we run a busy loop for a certain percentage of time and then wait for the remainder
// concretely, we split a second into two parts: one busy loop and one sleep
// we repeat this "second" until the test is stopped
run := time.Duration(util) * time.Second / 100
Dismissed Show dismissed Hide dismissed
geigerj0 marked this conversation as resolved.
Show resolved Hide resolved
sleep := time.Duration(100-util) * time.Second / 100
runtime.LockOSThread()
for m.IsRunning() {
begin := time.Now()
Expand All @@ -83,8 +85,10 @@ func (m *ConcurrentBusyLoopCPUWaster) UseCPU(utilisation uint64, duration time.D
}
time.Sleep(sleep)
}
runtime.UnlockOSThread()
}(perGoRoutineUtilisation)
}

// how long
go func() {
time.Sleep(duration)
Expand All @@ -93,8 +97,8 @@ func (m *ConcurrentBusyLoopCPUWaster) UseCPU(utilisation uint64, duration time.D
}

func (m *ConcurrentBusyLoopCPUWaster) IsRunning() bool {
m.mu.Lock()
defer m.mu.Unlock()
m.mu.RLock()
defer m.mu.RUnlock()
return m.isRunning
}

Expand All @@ -104,9 +108,8 @@ func (m *ConcurrentBusyLoopCPUWaster) StopTest() {
m.isRunning = false
}

func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
func (m *ConcurrentBusyLoopCPUWaster) startTest() {
m.mu.Lock()
defer m.mu.Unlock()
m.isRunning = true
}
15 changes: 0 additions & 15 deletions src/acceptance/assets/app/go_app/internal/app/cpuInfo.go

This file was deleted.

41 changes: 29 additions & 12 deletions src/acceptance/assets/app/go_app/internal/app/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,46 @@ var _ = Describe("CPU tests", func() {
Expect(utilization).Should(Equal(uint64(5)))
})
})
Context("UseCPU", func() {
It("should use cpu and release when stopped", func() {

oldCpu := getTotalCPUUsage("before cpuTest info test")
// This test is timing sensitive and may fail on GitHub workers, which is why it is marked as flaky
geigerj0 marked this conversation as resolved.
Show resolved Hide resolved
Context("ConcurrentBusyLoopCPUWaster", func() {
Context("UseCPU", FlakeAttempts(3), func() {
DescribeTable("should use cpu",
func(utilisation uint64, duration time.Duration) {
oldCpu := getTotalCPUUsage("before test")

By("allocating cpu")
cpuInfo := &app.ConcurrentBusyLoopCPUWaster{}
cpuInfo.UseCPU(100, time.Second)
Expect(cpuInfo.IsRunning()).To(Equal(true))
Eventually(cpuInfo.IsRunning, "2s").Should(Equal(false))
newCpu := getTotalCPUUsage("after cpuTest info test")
Expect(newCpu - oldCpu).To(BeNumerically(">=", 500*time.Millisecond))
By("wasting cpu time")
cpuWaster := &app.ConcurrentBusyLoopCPUWaster{}
cpuWaster.UseCPU(utilisation, duration)
Expect(cpuWaster.IsRunning()).To(Equal(true))
Eventually(cpuWaster.IsRunning).WithTimeout(duration + time.Second).WithPolling(time.Second).Should(Equal(false))
newCpu := getTotalCPUUsage("after test")
expectedCPUUsage := multiplyDurationByPercentage(duration, utilisation)
// Give 10% tolerance - but at least 1 second, as this is the internal resolution of the CPU waster
tolerance := max(multiplyDurationByPercentage(expectedCPUUsage, 10), time.Second)
Expect(newCpu - oldCpu).To(BeNumerically("~", expectedCPUUsage, tolerance))
},
Entry("25% for 10 seconds", uint64(25), time.Second*10),
Entry("50% for 10 seconds", uint64(50), time.Second*10),
Entry("100% for 10 seconds", uint64(100), time.Second*10),
Entry("200% for 10 seconds", uint64(200), time.Second*10),
Entry("400% for 10 seconds", uint64(400), time.Second*10),
)
})
})
})

func getTotalCPUUsage(action string) time.Duration {
GinkgoHelper()

cpuTotalUsage := app.CpuTotalUsageTime()
cpuTotalDuration := time.Duration(cpuTotalUsage * float64(time.Second))
cpuTotalUsage := app.GetClock()
cpuTotalDuration := time.Duration(float64(time.Second) * cpuTotalUsage / app.ClocksPerSec)

GinkgoWriter.Printf("total cpu time %s: %s\n", action, cpuTotalDuration.String())

return cpuTotalDuration
}

func multiplyDurationByPercentage(duration time.Duration, percentage uint64) time.Duration {
return time.Duration(float64(duration) * float64(percentage) / 100)
}
Loading