Skip to content

Commit

Permalink
Update main.go
Browse files Browse the repository at this point in the history
Signed-off-by: Md Imran <imranaec@outlook.com>
  • Loading branch information
narmidm committed Oct 12, 2024
1 parent e97a329 commit 2a64db8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -35,10 +36,11 @@ func main() {

var stopFlag int32

// Improved workload generation
for i := 0; i < numGoroutines; i++ {
go func() {
workDuration := time.Duration(*cpuUsagePtr*100) * time.Millisecond
idleDuration := time.Duration((1-*cpuUsagePtr)*100) * time.Millisecond
workDuration := time.Duration(*cpuUsagePtr*1000) * time.Microsecond
idleDuration := time.Duration((1-*cpuUsagePtr)*1000) * time.Microsecond

for {
if atomic.LoadInt32(&stopFlag) == 1 {
Expand All @@ -48,6 +50,8 @@ func main() {
// Busy loop for the specified work duration
endWork := time.Now().Add(workDuration)
for time.Now().Before(endWork) {
// Perform a small computation to keep the CPU active
_ = rand.Float64() * rand.Float64()
}

// Idle for the rest of the interval
Expand All @@ -69,7 +73,8 @@ func main() {
fmt.Println("\nCPU stress completed.")
atomic.StoreInt32(&stopFlag, 1)
close(done)
os.Exit(0)
// Keep the process running to prevent the pod from restarting
select {}
}

// Run stress indefinitely
Expand Down

0 comments on commit 2a64db8

Please sign in to comment.