summaryrefslogtreecommitdiff
path: root/internal/cpu/stresser.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-03 23:28:57 +0300
committerPaul Buetow <paul@buetow.org>2026-05-03 23:28:57 +0300
commit2f09915d3da7a23574e1a90fa70eb1af1cdf830a (patch)
tree3c46947fd67ac0e20aff84ae2420a4788a8a8c0e /internal/cpu/stresser.go
initial stuff
Diffstat (limited to 'internal/cpu/stresser.go')
-rw-r--r--internal/cpu/stresser.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/cpu/stresser.go b/internal/cpu/stresser.go
new file mode 100644
index 0000000..906467a
--- /dev/null
+++ b/internal/cpu/stresser.go
@@ -0,0 +1,62 @@
+// Package cpu provides CPU stress testing with multiple algorithms and chaos modes.
+package cpu
+
+import (
+ "context"
+ "math/rand"
+ "sync"
+ "time"
+)
+
+// stresser.go contains the main Stresser type, constructor, and lifecycle methods.
+
+// Stresser uses pointer receivers because it contains a sync.Mutex
+// and shared mutable state that must not be copied.
+type Stresser struct {
+ config Config
+ cancel context.CancelFunc
+ wg sync.WaitGroup
+ mu sync.Mutex
+ current Mode
+ rand *rand.Rand
+}
+
+func New(cfg Config) *Stresser {
+ if cfg.Workers <= 0 {
+ cfg.Workers = 1
+ }
+ if cfg.IntensityMin <= 0 {
+ cfg.IntensityMin = 100
+ }
+ if cfg.IntensityMax <= 0 {
+ cfg.IntensityMax = 1000
+ }
+ if cfg.Mode == "" {
+ cfg.Mode = ModePrime
+ }
+ return &Stresser{
+ config: cfg,
+ rand: rand.New(rand.NewSource(time.Now().UnixNano())),
+ }
+}
+
+func (s *Stresser) Start() {
+ ctx, cancel := context.WithCancel(context.Background())
+ s.cancel = cancel
+
+ s.wg.Add(s.config.Workers)
+ for i := 0; i < s.config.Workers; i++ {
+ go s.worker(ctx, i)
+ }
+
+ if s.config.ChaosEnabled {
+ go s.chaosController(ctx)
+ }
+}
+
+func (s *Stresser) Stop() {
+ if s.cancel != nil {
+ s.cancel()
+ }
+ s.wg.Wait()
+}