-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathactivity-simulator.go
215 lines (182 loc) · 4.61 KB
/
activity-simulator.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package prom_http_simulator
import (
"time"
"sync"
"math/rand"
"github.com/prometheus/client_golang/prometheus"
)
var (
httpRequestsTotal = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of http requests by response status code",
},
[]string{"endpoint", "status"},
)
httpRequestDurationMs = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_milliseconds",
Help: "Http request latency histogram",
Buckets: prometheus.ExponentialBuckets(25, 2, 7),
},
[]string{"endpoint", "status"},
)
)
func init() {
prometheus.MustRegister(httpRequestsTotal, httpRequestDurationMs)
}
type Simulator struct {
opts SimulatorOpts
mutex *sync.Mutex
rng *rand.Rand
spikeMode bool
}
func NewSimulator(opts SimulatorOpts) *Simulator {
return &Simulator{
opts,
&sync.Mutex{},
rand.New(rand.NewSource(time.Now().UnixNano())),
false,
}
}
func (s *Simulator) UpdateOpts(opts SimulatorOpts) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.opts = opts
}
func (s *Simulator) SetSpikeMode(mode string) {
s.mutex.Lock()
defer s.mutex.Unlock()
switch mode {
case "on":
s.opts.SpikeModeStatus = ON
case "off":
s.opts.SpikeModeStatus = OFF
case "random":
s.opts.SpikeModeStatus = RANDOM
}
}
func (s *Simulator) SetErrorRate(rate int) {
s.mutex.Lock()
defer s.mutex.Unlock()
if rate > 100 {
rate = 100
}
if rate < 0 {
rate = 0
}
s.opts.ErrorRate = rate
}
func (s *Simulator) GetOpts() SimulatorOpts {
return s.opts
}
func (s *Simulator) Run() {
for {
s.simulateActivity()
time.Sleep(1 * time.Second)
}
}
func (s *Simulator) simulateActivity() {
s.mutex.Lock()
defer s.mutex.Unlock()
requestRate := s.opts.RequestRate
if s.giveSpikeMode() {
requestRate *= 5 + s.rng.Intn(10)
}
nbRequests := s.giveWithUncertainty(requestRate, s.opts.RequestRateUncertainty)
var nbErrors int
for i := 0; i < nbRequests; i++ {
statusCode := s.giveStatusCode()
endpoint := s.giveEndpoint()
httpRequestsTotal.WithLabelValues(endpoint, statusCode).Inc()
latency := s.giveLatency(statusCode)
if s.spikeMode {
latency *= 2
}
if statusCode == "500" {
nbErrors++
}
httpRequestDurationMs.WithLabelValues(endpoint, statusCode).Observe(float64(latency))
}
}
func (s *Simulator) giveSpikeMode() bool {
switch s.opts.SpikeModeStatus {
case ON:
s.spikeMode = true
case OFF:
s.spikeMode = false
case RANDOM:
n := s.rng.Intn(100)
if !s.spikeMode && n < s.opts.SpikeStartChance {
s.spikeMode = true
} else if s.spikeMode && n < s.opts.SpikeEndChance {
s.spikeMode = false
}
}
return s.spikeMode
}
func (s *Simulator) giveWithUncertainty(n int, u int) int {
delta := s.rng.Intn(n*u/50) - (n * u / 100)
return n + delta
}
func (s *Simulator) giveStatusCode() string {
if s.rng.Intn(100) < s.opts.ErrorRate {
return "500"
} else {
return "200"
}
}
func (s *Simulator) giveEndpoint() string {
n := s.rng.Intn(len(s.opts.Endpoints))
return s.opts.Endpoints[n]
}
func (s *Simulator) giveLatency(statusCode string) int {
if statusCode != "200" {
return 5+s.rng.Intn(50)
}
p := s.rng.Intn(100)
if p < 50 {
return s.giveWithUncertainty(s.opts.LatencyMin+s.rng.Intn(s.opts.LatencyP50-s.opts.LatencyMin), s.opts.LatencyUncertainty)
}
if p < 90 {
return s.giveWithUncertainty(s.opts.LatencyP50+s.rng.Intn(s.opts.LatencyP90-s.opts.LatencyP50), s.opts.LatencyUncertainty)
}
if p < 99 {
return s.giveWithUncertainty(s.opts.LatencyP90+s.rng.Intn(s.opts.LatencyP99-s.opts.LatencyP90), s.opts.LatencyUncertainty)
}
return s.giveWithUncertainty(s.opts.LatencyP99+s.rng.Intn(s.opts.LatencyMax-s.opts.LatencyP99), s.opts.LatencyUncertainty)
}
type SpikeMode int
const (
OFF SpikeMode = iota
ON
RANDOM
)
type SimulatorOpts struct {
// Endpoints Weighted map of endpoints to simulate
Endpoints []string
// RequestRate requests per second
RequestRate int
// RequestRateUncertainty Percentage of uncertainty when generating requests (+/-)
RequestRateUncertainty int
// LatencyMin in milliseconds
LatencyMin int
// LatencyP50 in milliseconds
LatencyP50 int
// LatencyP90 in milliseconds
LatencyP90 int
// LatencyP99 in milliseconds
LatencyP99 int
// LatencyMax in milliseconds
LatencyMax int
// LatencyUncertainty Percentage of uncertainty when generating latency (+/-)
LatencyUncertainty int
// ErrorRate Percentage of chance of requests causing 500
ErrorRate int
// SpikeModeStatus ON/OFF/RANDOM
SpikeModeStatus SpikeMode
// SpikeStartChance Percentage of chance of entering spike mode
SpikeStartChance int
// SpikeStartChance Percentage of chance of exiting spike mode
SpikeEndChance int
}