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

add simple CPU load generator #235

Open
ondrejsika opened this issue Feb 1, 2024 · 0 comments
Open

add simple CPU load generator #235

ondrejsika opened this issue Feb 1, 2024 · 0 comments

Comments

@ondrejsika
Copy link
Member

ondrejsika commented Feb 1, 2024

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.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant