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 1 commit
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
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())
}
16 changes: 11 additions & 5 deletions src/acceptance/assets/app/go_app/internal/app/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,15 @@ 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()
geigerj0 marked this conversation as resolved.
Show resolved Hide resolved

for utilisation > 0 {
perGoRoutineUtilisation := min(utilisation, 100)
utilisation = utilisation - perGoRoutineUtilisation

go func(util uint64) {
run := time.Duration(util) * time.Microsecond / 10
sleep := time.Duration(100-util) * time.Microsecond / 10
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 +81,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 @@ -104,6 +104,12 @@ func (m *ConcurrentBusyLoopCPUWaster) StopTest() {
m.isRunning = false
}

func (m *ConcurrentBusyLoopCPUWaster) StartTest() {
geigerj0 marked this conversation as resolved.
Show resolved Hide resolved
m.mu.Lock()
defer m.mu.Unlock()
m.isRunning = true
}

func min[T constraints.Ordered](a, b T) T {
if a < b {
return a
Expand Down
15 changes: 0 additions & 15 deletions src/acceptance/assets/app/go_app/internal/app/cpuInfo.go

This file was deleted.

41 changes: 28 additions & 13 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,44 @@ 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("UseCPU", FlakeAttempts(3), func() {
DescribeTable("should use cpu",
func(utilisation uint64, duration time.Duration) {
oldCpu := getTotalCPUUsage("before cpuTest info 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("allocating cpu")
cpuInfo := &app.ConcurrentBusyLoopCPUWaster{}
cpuInfo.UseCPU(utilisation, duration)
Expect(cpuInfo.IsRunning()).To(Equal(true))
Eventually(cpuInfo.IsRunning).WithTimeout(duration + time.Second).WithPolling(time.Second).Should(Equal(false))
newCpu := getTotalCPUUsage("after cpuTest info 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