summaryrefslogtreecommitdiff
path: root/internal/cpu/types.go
blob: 45f6ff5e853818e2c6718224bf7163d57b91acd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Package cpu provides CPU stress testing with multiple algorithms and chaos modes.
package cpu

import (
	"runtime"
	"time"
)

// types.go defines the configuration types and mode constants for CPU stress testing.

type Mode string

const (
	ModePrime    Mode = "prime"
	ModeMatrix   Mode = "matrix"
	ModeHash     Mode = "hash"
	ModeBitwise  Mode = "bitwise"
	ModeFloat    Mode = "float"
	ModeRecurse  Mode = "recurse"
	ModeCompress Mode = "compress"
	ModeEncrypt  Mode = "encrypt"
	ModeSort     Mode = "sort"
	ModeBusy     Mode = "busy"
)

var allModes = []Mode{
	ModePrime,
	ModeMatrix,
	ModeHash,
	ModeBitwise,
	ModeFloat,
	ModeRecurse,
	ModeCompress,
	ModeEncrypt,
	ModeSort,
	ModeBusy,
}

type Config struct {
	Mode          Mode
	Workers       int
	Duration      time.Duration
	ChaosEnabled  bool
	ChaosInterval time.Duration
	IntensityMin  int
	IntensityMax  int
	MinCPUs       int
	MaxCPUs       int
}

func (c *Config) normalize() {
	if c.MinCPUs <= 0 {
		c.MinCPUs = 1
	}
	if c.MaxCPUs <= 0 {
		c.MaxCPUs = runtime.NumCPU()
	}
	if c.MinCPUs > runtime.NumCPU() {
		c.MinCPUs = runtime.NumCPU()
	}
	if c.MaxCPUs > runtime.NumCPU() {
		c.MaxCPUs = runtime.NumCPU()
	}
	if c.MinCPUs > c.MaxCPUs {
		c.MinCPUs, c.MaxCPUs = c.MaxCPUs, c.MinCPUs
	}
}