-
Notifications
You must be signed in to change notification settings - Fork 17
/
loader.go
255 lines (221 loc) · 5.98 KB
/
loader.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/cheggaaa/pb"
"github.com/hagen1778/fasthttploader/fastclient"
"github.com/hagen1778/fasthttploader/ratelimiter"
"github.com/hagen1778/fasthttploader/report"
)
const (
// Duration of burst-testing, without qps-limit. Used to estimate start test conditions
calibrateDuration = 10 * time.Second
// Duration of adjustable testing, while trying to reach max qps with minimal lvl of errors
adjustmentDuration = 30 * time.Second
// Period of sample taking, while testing
samplePeriod = 500 * time.Millisecond
)
var (
// client do http requests, populate metrics
client *fastclient.Client
// report represent html-report
r *report.Page
// errors storage of errors amount in current step. Used to compare changes in errors-metric
errors uint64
// multiplier is a coefficient of qps multiplying during tests
multiplier = float64(0.1)
throttle = ratelimiter.NewLimiter()
)
type loadConfig struct {
// qps is the rate limit.
qps float64
// c is a number of workers (clients)
c int
}
func run() {
r = &report.Page{
Title: string(req.URI().Host()),
RequestDuration: make(map[float64][]float64),
Interval: samplePeriod.Seconds(),
}
cfg := loadConfig{}
if *q == 0 {
fmt.Println("Run burst-load phase")
burstThroughput(&cfg)
fmt.Println("Run calibrate phase")
calibrateThroughput(&cfg)
} else {
cfg.qps = float64(*q)
cfg.c = *c
}
fmt.Println("Run load phase")
makeLoad(&cfg)
f, err := os.Create(*fileName)
if err != nil {
log.Fatalf("Error while trying to create file: %s", err)
}
f.WriteString(report.PrintPage(r))
f.Close()
}
func burstThroughput(cfg *loadConfig) {
client = fastclient.New(req, *t, *successStatusCode)
startTime := time.Now()
timeout := time.After(calibrateDuration)
bar, progressTicker := acquireProgressBar(calibrateDuration)
client.RunWorkers(*c)
for {
select {
case <-timeout:
finishProgressBar(bar)
cfg.qps = float64(client.RequestSum()) / calibrateDuration.Seconds()
cfg.c = client.Amount()
if (client.Errors()/client.RequestSum())*100 > 2 { // just more than 2% of errors
cfg.qps /= 2
cfg.c /= 2
}
printSummary("Burst Throughput", startTime)
return
case <-progressTicker:
bar.Increment()
default:
client.Jobsch <- struct{}{}
}
}
}
func calibrateThroughput(cfg *loadConfig) {
client = fastclient.New(req, *t, *successStatusCode)
t := time.Now()
ctx, cancel := context.WithCancel(context.Background())
throttle.SetLimit(cfg.qps)
client.RunWorkers(cfg.c)
go func() {
timeout := time.After(adjustmentDuration)
sampler := time.Tick(samplePeriod)
bar, progressTicker := acquireProgressBar(adjustmentDuration)
for {
select {
case <-timeout:
finishProgressBar(bar)
cfg.qps = throttle.Limit()
cfg.c = client.Amount()
printSummary("Adjustment test", t)
cancel()
return
case <-progressTicker:
bar.Increment()
case <-sampler:
printState()
calibrate()
}
}
}()
load(ctx)
}
var await = 0
func calibrate() {
if await > 0 {
await -= 1
return
}
if !isFlawed() {
if client.Overflow() > 0 {
n := int(float64(client.Amount()) * multiplier)
client.RunWorkers(n)
await += 1
} else {
throttle.SetLimit(throttle.Limit() * (1 + multiplier))
await += 1
}
} else {
multiplier /= 1.2
await += 3
}
}
func makeLoad(cfg *loadConfig) {
client = fastclient.New(req, *t, *successStatusCode)
startTime := time.Now()
ctx, cancel := context.WithCancel(context.Background())
throttle.SetLimit(cfg.qps)
client.RunWorkers(cfg.c)
go func() {
stateTick := time.Tick(samplePeriod)
timeout := time.After(*d)
bar, progressTicker := acquireProgressBar(*d)
for {
select {
case <-timeout:
finishProgressBar(bar)
printSummary("Loading test", startTime)
throttle.Stop()
cancel()
return
case <-progressTicker:
bar.Increment()
case <-stateTick:
printState()
}
}
}()
load(ctx)
}
func printState() {
if *debug {
fmt.Println("------------")
fmt.Printf("[ Multiplier = %f ]\n", multiplier)
fmt.Printf("QPS was increased to: %f\nWorkers: %d\nJobsch len: %d\n", throttle.Limit(), client.Amount(), client.Overflow())
fmt.Printf(" >> Num of cons: %d; Req done: %d; Errors: %d; Timeouts: %d\n", client.ConnOpen(), client.RequestSum(), client.Errors(), client.Timeouts())
fmt.Println("------------")
}
r.Lock()
r.Connections = append(r.Connections, client.ConnOpen())
r.Errors = append(r.Errors, client.Errors())
r.Timeouts = append(r.Timeouts, client.Timeouts())
r.RequestSum = append(r.RequestSum, client.RequestSum())
r.RequestSuccess = append(r.RequestSuccess, client.RequestSuccess())
r.BytesWritten = append(r.BytesWritten, client.BytesWritten())
r.BytesRead = append(r.BytesRead, client.BytesRead())
r.Qps = append(r.Qps, uint64(throttle.Limit()))
r.StatusCodes = client.StatusCodes()
r.ErrorMessages = client.ErrorMessages()
r.UpdateRequestDuration(client.RequestDuration())
r.Unlock()
}
func isFlawed() bool {
if client.Errors() > 0 && errors != client.Errors() {
errors = client.Errors()
return true
}
return false
}
func load(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-throttle.QPS():
client.Jobsch <- struct{}{}
}
}
}
func printSummary(stage string, t time.Time) {
since := time.Since(t).Seconds()
fmt.Printf("\n------ %s ------\n", stage)
fmt.Printf("Elapsed time: %fs\n", since)
fmt.Printf("Req done: %d; Success: %.2f %%\n", client.RequestSum(), (float64(client.RequestSuccess())/float64(client.RequestSum()))*100)
fmt.Printf("QPS: %f; Connections: %d\n", float64(client.RequestSum())/since, client.ConnOpen())
fmt.Printf("Errors: %d; Timeouts: %d\n\n", client.Errors(), client.Timeouts())
}
func acquireProgressBar(t time.Duration) (*pb.ProgressBar, <-chan time.Time) {
pb := pb.New64(int64(t.Seconds()))
pb.ShowCounters = false
pb.ShowPercent = false
pb.Start()
return pb, time.Tick(time.Second)
}
func finishProgressBar(pb *pb.ProgressBar) {
pb.Set64(pb.Total)
pb.Finish()
}