-
Notifications
You must be signed in to change notification settings - Fork 63
/
vegeta.go
541 lines (511 loc) · 14.3 KB
/
vegeta.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/martini-contrib/render"
vegeta "github.com/tsenart/vegeta/lib"
"gopkg.in/mgo.v2/bson"
)
type RatePeriod struct {
// qps step setting
Rate uint64
Duration uint
}
type VegetaJob struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
// Job Name
Name string
// Http API Team Name
Team string
// Http API Project Name
Project string
// Http API Url
Url string
// Hosts Pool for randomize choice
Hosts []string
Method string
Jsonified bool // application/json
// Parameters Pool for randomize choice
Seeds []RequestSeed
CreateTs int64
LastRunTs int64
// Initial concurrency for vegeta
Workers uint64
// Timeout duration for each request
Timeout int
// Redirect times for each request
Redirects int
// http keepalive
Keepalive bool
// qps step settings
Periods []RatePeriod
// current qps in running
CurrentRate uint64
}
func (job *VegetaJob) IsRunning() bool {
return G_RunningVegetaJobs.Exists(job.Id.Hex())
}
func GetVegetaJobs(req *http.Request, r render.Render) {
var team = req.FormValue("team")
var project = req.FormValue("project")
var url = req.FormValue("url")
var page = req.FormValue("p")
var condition = bson.M{}
if team != "" {
condition["team"] = team
}
if project != "" {
condition["project"] = project
}
if url != "" {
condition["url"] = bson.M{"$regex": bson.RegEx{fmt.Sprintf("^%s", url), ""}}
}
if len(condition) == 0 {
condition = nil
}
total, err := G_MongoDB.C("vegeta_jobs").Find(condition).Count()
if err != nil {
log.Panic(err)
}
var pager = NewPager(20, total)
pager.CurrentPage, err = strconv.Atoi(page)
pager.UrlPattern = fmt.Sprintf("/vegeta/?p=%%d&team=%s&project=%s", team, project)
var jobs []VegetaJob
err = G_MongoDB.C("vegeta_jobs").Find(condition).Skip(pager.Offset()).Sort("-lastrunts").Limit(pager.Limit()).All(&jobs)
if err != nil {
log.Panic(err)
}
var context = make(map[string]interface{})
context["jobs"] = jobs
context["teams"] = GenTeamSelectors(team)
context["project"] = project
context["url"] = url
context["pager"] = pager
RenderTemplate(r, "vegeta_jobs", context)
}
func CreateVegetaJob(req *http.Request, r render.Render) {
var name = req.FormValue("name")
var team = req.FormValue("team")
var project = req.FormValue("project")
var job = VegetaJob{
Id: bson.NewObjectId(),
Name: name,
Team: team,
Hosts: []string{"localhost:8000"},
Project: project,
Jsonified: false,
Seeds: []RequestSeed{RequestSeed{}},
CreateTs: time.Now().Unix(),
LastRunTs: time.Now().Unix(),
Workers: 100,
Timeout: 10,
Redirects: 1,
Keepalive: true,
Periods: []RatePeriod{RatePeriod{10, 5}},
}
err := G_MongoDB.C("vegeta_jobs").Insert(&job)
if err != nil {
log.Panic(err)
}
r.Redirect(fmt.Sprintf("/vegeta/edit?job_id=%s", job.Id.Hex()))
}
type VegetaEditForm struct {
Job *VegetaJob
Methods []MethodSelector
Teams []TeamSelector
}
func EditVegetaJobPage(req *http.Request, r render.Render) {
var jobId = req.FormValue("job_id")
var job VegetaJob
err := G_MongoDB.C("vegeta_jobs").FindId(bson.ObjectIdHex(jobId)).One(&job)
if err != nil {
log.Panic(err)
}
var context = make(map[string]interface{})
var form = VegetaEditForm{Job: &job}
form.Methods = GenMethodSelectors(job.Method)
form.Teams = GenTeamSelectors(job.Team)
context["form"] = form
RenderTemplate(r, "vegeta_edit", context)
}
func EditVegetaJob(req *http.Request, r render.Render) {
req.ParseForm()
var jobId = req.FormValue("job_id")
var job VegetaJob
err := G_MongoDB.C("vegeta_jobs").FindId(bson.ObjectIdHex(jobId)).One(&job)
if err != nil {
log.Panic(err)
}
job.Name = req.FormValue("name")
job.Team = req.FormValue("team")
job.Project = req.FormValue("project")
job.Method = req.FormValue("method")
job.Url = req.FormValue("url")
job.Jsonified = req.FormValue("jsonified") != ""
var hosts []string
for _, host := range req.Form["host"] {
hosts = append(hosts, host)
}
job.Hosts = hosts
var headerSeeds = []map[string]interface{}{}
var paramSeeds = []map[string]interface{}{}
var dataSeeds = []map[string]interface{}{}
var jsonDataSeeds = []string{}
for _, header := range req.Form["header"] {
var seed map[string]interface{}
json.Unmarshal([]byte(header), &seed)
headerSeeds = append(headerSeeds, seed)
}
for _, param := range req.Form["param"] {
var seed map[string]interface{}
json.Unmarshal([]byte(param), &seed)
paramSeeds = append(paramSeeds, seed)
}
if job.Jsonified {
for _, data := range req.Form["data"] {
jsonDataSeeds = append(jsonDataSeeds, data)
}
} else {
for _, data := range req.Form["data"] {
var seed map[string]interface{}
json.Unmarshal([]byte(data), &seed)
dataSeeds = append(dataSeeds, seed)
}
}
job.Seeds = make([]RequestSeed, len(headerSeeds))
for i := 0; i < len(headerSeeds); i++ {
job.Seeds[i] = RequestSeed{headerSeeds[i], paramSeeds[i], nil, ""}
if len(dataSeeds) > 0 {
job.Seeds[i].Data = dataSeeds[i]
} else {
job.Seeds[i].JsonData = jsonDataSeeds[i]
}
}
var changed = bson.M{
"name": job.Name,
"team": job.Team,
"project": job.Project,
"method": job.Method,
"url": job.Url,
"hosts": job.Hosts,
"jsonified": job.Jsonified,
"seeds": job.Seeds,
}
var op = bson.M{"$set": changed}
err = G_MongoDB.C("vegeta_jobs").UpdateId(job.Id, op)
if err != nil {
log.Panic(err)
}
r.Redirect("/vegeta/")
}
type VegetaRunForm struct {
Job *VegetaJob
}
func RunVegetaJobPage(req *http.Request, r render.Render) {
var jobId = req.FormValue("job_id")
// same job, no concurrent
if G_RunningVegetaJobs.Exists(jobId) {
r.Redirect(req.Referer())
return
}
var job VegetaJob
err := G_MongoDB.C("vegeta_jobs").FindId(bson.ObjectIdHex(jobId)).One(&job)
if err != nil {
log.Panic(err)
}
var form = VegetaRunForm{&job}
var context = make(map[string]interface{})
context["form"] = form
RenderTemplate(r, "vegeta_run", context)
}
func RunVegetaJob(req *http.Request, r render.Render) {
req.ParseForm()
var jobId = req.FormValue("job_id")
var job VegetaJob
err := G_MongoDB.C("vegeta_jobs").FindId(bson.ObjectIdHex(jobId)).One(&job)
if err != nil {
log.Panic(err)
}
var workers, _ = strconv.Atoi(req.FormValue("workers"))
var timeout, _ = strconv.Atoi(req.FormValue("timeout"))
var redirects, _ = strconv.Atoi(req.FormValue("redirects"))
var keepalive = req.FormValue("keepalive") != ""
var rates = req.Form["rate"]
var durations = req.Form["duration"]
var comment = req.FormValue("comment")
var periods = []RatePeriod{}
for i, _ := range rates {
var rate, _ = strconv.Atoi(rates[i])
var duration, _ = strconv.Atoi(durations[i])
periods = append(periods, RatePeriod{uint64(rate), uint(duration)})
}
job.Workers = uint64(workers)
job.Timeout = timeout
job.Periods = periods
var changed = bson.M{
"workers": job.Workers,
"timeout": job.Timeout,
"redirects": redirects,
"keepalive": keepalive,
"periods": job.Periods,
"lastrunts": time.Now().Unix(),
}
var op = bson.M{"$set": changed}
err = G_MongoDB.C("vegeta_jobs").UpdateId(job.Id, op)
if err != nil {
log.Panic(err)
}
G_RunningVegetaJobs.Put(job.Id.Hex())
go AttackVegetaJob(&job, comment)
r.Redirect("/vegeta/")
}
func DeleteVegetaJob(req *http.Request, r render.Render) {
var jobId = req.FormValue("job_id")
G_RunningVegetaJobs.Delete(jobId)
err := G_MongoDB.C("vegeta_jobs").RemoveId(bson.ObjectIdHex(jobId))
if err != nil {
log.Panic(err)
}
r.Redirect("/vegeta/")
}
func StopVegetaJob(req *http.Request, r render.Render) {
// stop vegeta jobs
var jobId = req.FormValue("job_id")
if G_RunningVegetaJobs.Exists(jobId) {
G_StoppingVegetaJobs.Put(jobId)
}
r.Redirect(req.Referer())
}
func GetVegetaLogs(req *http.Request, r render.Render) {
var jobId = req.FormValue("job_id")
var page = req.FormValue("p")
var logs []AttackVegetaLog
var condition = bson.M{}
if jobId != "" {
condition = bson.M{"jobid": jobId}
} else {
condition = nil
}
total, err := G_MongoDB.C("vegeta_logs").Find(condition).Count()
if err != nil {
log.Panic(err)
}
var pager = NewPager(20, total)
pager.CurrentPage, err = strconv.Atoi(page)
pager.UrlPattern = fmt.Sprintf("/vegeta/logs?&p=%%d&job_id=%s", jobId)
err = G_MongoDB.C("vegeta_logs").Find(condition).Skip(pager.Offset()).Sort("-startts").Limit(pager.Limit()).All(&logs)
if err != nil {
log.Panic(err)
}
var context = make(map[string]interface{})
context["logs"] = logs
context["jobId"] = jobId
context["pager"] = pager
RenderTemplate(r, "vegeta_logs", context)
}
func DeleteVegetaLog(req *http.Request, r render.Render) {
var logId = bson.ObjectIdHex(req.FormValue("log_id"))
err := G_MongoDB.C("vegeta_logs").RemoveId(logId)
if err != nil {
log.Panic(err)
}
r.Redirect(req.Referer())
}
func GetVegetaMetrics(req *http.Request, r render.Render) {
var lg AttackVegetaLog
var lgId = bson.ObjectIdHex(req.FormValue("log_id"))
err := G_MongoDB.C("vegeta_logs").FindId(lgId).One(&lg)
if err != nil {
log.Panic(err)
}
var context = make(map[string]interface{})
context["log"] = &lg
RenderTemplate(r, "vegeta_metrics", context)
}
type AttackVegetaLog struct {
Id bson.ObjectId `json:"id" bson:"_id,omitempty"`
JobId string
JobName string
JobUrl string
JobDetail *VegetaJob
Comment string
State string
MetricsList []*vegeta.Metrics
StartTs int64
EndTs int64
}
func (log *AttackVegetaLog) IsRunning() bool {
return log.State == "Running"
}
func (log *AttackVegetaLog) LatencyMetrics() string {
var buffer bytes.Buffer
var startTime = 0.0
for _, metrics := range log.MetricsList {
var latency = metrics.Latencies.Mean.Seconds() * 1000
buffer.WriteString(fmt.Sprintf("%v,%v\n", startTime, latency))
startTime += metrics.Duration.Seconds()
buffer.WriteString(fmt.Sprintf("%v,%v\n", startTime, latency))
}
return buffer.String()
}
func (log *AttackVegetaLog) RateMetrics() string {
var buffer bytes.Buffer
var startTime = 0.0
for _, metrics := range log.MetricsList {
var rate = metrics.Rate
buffer.WriteString(fmt.Sprintf("%v,%v\n", startTime, rate))
startTime += metrics.Duration.Seconds()
buffer.WriteString(fmt.Sprintf("%v,%v\n", startTime, rate))
}
return buffer.String()
}
func (log *AttackVegetaLog) RateLatencyMetrics() string {
var buffer bytes.Buffer
for _, metrics := range log.MetricsList {
var rate = metrics.Rate
var latency = metrics.Latencies.Mean.Seconds() * 1000
buffer.WriteString(fmt.Sprintf("%v,%v\n", rate, latency))
}
return buffer.String()
}
func (log *AttackVegetaLog) StatusCodesList() map[string]bool {
var codeList = make(map[string]bool)
for _, metrics := range log.MetricsList {
for code, _ := range metrics.StatusCodes {
codeList[code] = true
}
}
return codeList
}
func (log *AttackVegetaLog) StatusCodesMetrics() string {
var buffer bytes.Buffer
var startTime = 0.0
var codeList = log.StatusCodesList()
for _, metrics := range log.MetricsList {
buffer.WriteString(fmt.Sprintf("%v", startTime))
for code, _ := range codeList {
var count, ok = metrics.StatusCodes[code]
if !ok {
count = 0
}
buffer.WriteString(fmt.Sprintf(",%v", count))
}
buffer.WriteString("\n")
startTime += metrics.Duration.Seconds()
buffer.WriteString(fmt.Sprintf("%v", startTime))
for code, _ := range codeList {
var count, ok = metrics.StatusCodes[code]
if !ok {
count = 0
}
buffer.WriteString(fmt.Sprintf(",%v", count))
}
buffer.WriteString("\n")
}
return buffer.String()
}
func AttackVegetaJob(job *VegetaJob, comment string) {
// start attacking target servers
var log = LogAttackVegetaStart(job, comment)
var metricsList []*vegeta.Metrics
attacker := vegeta.NewAttacker(
vegeta.Timeout(time.Duration(job.Timeout)*time.Second),
vegeta.Workers(job.Workers),
vegeta.KeepAlive(job.Keepalive),
vegeta.Redirects(job.Redirects))
targeter := NewRandomVegetaTargeter(job)
for _, period := range job.Periods {
var metrics vegeta.Metrics
var rate = period.Rate
var duration = time.Duration(period.Duration) * time.Second
UpdateJobCurrentRate(job, rate)
for res := range attacker.Attack(targeter, rate, duration) {
metrics.Add(res)
}
metrics.Close()
metricsList = append(metricsList, &metrics)
if G_StoppingVegetaJobs.Exists(job.Id.Hex()) {
G_StoppingVegetaJobs.Delete(job.Id.Hex())
break
}
}
G_RunningVegetaJobs.Delete(job.Id.Hex())
UpdateJobCurrentRate(job, 0)
LogAttackVegetaEnd(log, metricsList)
}
func UpdateJobCurrentRate(job *VegetaJob, rate uint64) {
// realtime update job's current rate for displaying
var op = bson.M{"$set": bson.M{"currentrate": rate}}
err := G_MongoDB.C("vegeta_jobs").UpdateId(job.Id, op)
if err != nil {
log.Panic(err)
}
}
func LogAttackVegetaStart(job *VegetaJob, comment string) *AttackVegetaLog {
// record attack log before job starts
var lg = AttackVegetaLog{
Id: bson.NewObjectId(),
JobId: job.Id.Hex(),
JobName: job.Name,
JobUrl: job.Url,
JobDetail: job,
Comment: comment,
State: "Running",
StartTs: time.Now().Unix(),
EndTs: 0,
}
err := G_MongoDB.C("vegeta_logs").Insert(&lg)
if err != nil {
log.Panic(err)
}
return &lg
}
func LogAttackVegetaEnd(lg *AttackVegetaLog, metricsList []*vegeta.Metrics) {
// record attack reports after job finished
var op = bson.M{"$set": bson.M{"metricslist": metricsList, "state": "End", "endts": time.Now().Unix()}}
err := G_MongoDB.C("vegeta_logs").UpdateId(lg.Id, op)
if err != nil {
log.Panic(err)
}
}
func NewRandomVegetaTargeter(job *VegetaJob) vegeta.Targeter {
// Generate http requests for vegeta job's attack
var targets []vegeta.Target
for _, host := range job.Hosts {
for i := 0; i < len(job.Seeds); i++ {
var header = http.Header{}
for k, v := range job.Seeds[i].Header {
switch v.(type) {
case []interface{}:
for _, vi := range v.([]interface{}) {
header.Add(k, fmt.Sprintf("%v", vi))
}
default:
header.Add(k, fmt.Sprintf("%v", v))
}
}
var param = job.Seeds[i].Param
var data = job.Seeds[i].Data
var jsonData = job.Seeds[i].JsonData
var body []byte
if job.Jsonified {
body = []byte(jsonData)
} else {
body = BodyBytes(data)
}
var target = vegeta.Target{
Method: job.Method,
URL: Urlcat(host, job.Url, param),
Body: body,
Header: header,
}
targets = append(targets, target)
}
}
return vegeta.NewStaticTargeter(targets...)
}