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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Package cpu provides CPU stress testing with multiple algorithms and chaos modes.
package cpu
import (
"context"
"time"
)
// worker.go implements the worker goroutine loop, chaos controller, and stress dispatching.
func (s *Stresser) worker(ctx context.Context, id int) {
defer s.wg.Done()
mode := s.config.Mode
intensity := s.randomIntensity()
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if s.config.ChaosEnabled {
s.mu.Lock()
mode = s.current
s.mu.Unlock()
intensity = s.randomIntensity()
s.sleepWithJitter(1, 50)
}
s.executeStress(ctx, mode, intensity)
}
}
}
func (s *Stresser) chaosController(ctx context.Context) {
ticker := time.NewTicker(s.config.ChaosInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.mu.Lock()
s.current = s.getRandomMode()
newCPUCount := s.determineCPUCount()
if newCPUCount != s.currentCPUs {
s.adjustWorkers(ctx, newCPUCount)
}
s.mu.Unlock()
}
}
}
func (s *Stresser) adjustWorkers(ctx context.Context, newCount int) {
if newCount > s.currentCPUs {
for i := s.currentCPUs; i < newCount; i++ {
s.wg.Add(1)
go s.worker(ctx, i)
}
} else if newCount < s.currentCPUs {
s.cancel()
s.wg.Wait()
ctx, s.cancel = context.WithCancel(context.Background())
for i := 0; i < newCount; i++ {
s.wg.Add(1)
go s.worker(ctx, i)
}
}
s.currentCPUs = newCount
}
func (s *Stresser) executeStress(ctx context.Context, mode Mode, intensity int) {
switch mode {
case ModePrime:
s.stressPrime(ctx, intensity)
case ModeMatrix:
s.stressMatrix(ctx, intensity)
case ModeHash:
s.stressHash(ctx, intensity)
case ModeBitwise:
s.stressBitwise(ctx, intensity)
case ModeFloat:
s.stressFloat(ctx, intensity)
case ModeRecurse:
s.stressRecurse(ctx, intensity)
case ModeCompress:
s.stressCompress(ctx, intensity)
case ModeEncrypt:
s.stressEncrypt(ctx, intensity)
case ModeSort:
s.stressSort(ctx, intensity)
case ModeBusy:
s.stressBusy(ctx, intensity)
}
}
|