-
-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
https://chat.openai.com/share/06fc3092-26ec-4f04-9680-93e0f8fda3b1
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"sync"
)
// generateLoad generates CPU load by performing mathematically intensive operations.
func generateLoad(wg *sync.WaitGroup) {
defer wg.Done()
for {
_ = 123456789 * 987654321
}
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run main.go <num_goroutines>")
return
}
// Determine the number of goroutines to launch based on user input.
numGoroutines, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Use all available CPUs.
runtime.GOMAXPROCS(runtime.NumCPU())
var wg sync.WaitGroup
// Create and start the specified number of goroutines.
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go generateLoad(&wg)
}
fmt.Printf("Generating CPU load with %d goroutines...\n", numGoroutines)
wg.Wait() // This line will actually never be reached as the load generation is infinite.
}Metadata
Metadata
Assignees
Labels
No labels