Skip to content

add simple CPU load generator #235

@ondrejsika

Description

@ondrejsika

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions