diff options
Diffstat (limited to 'internal/cpu/stresser.go')
| -rw-r--r-- | internal/cpu/stresser.go | 62 |
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() +} |
