/
main.go
272 lines (237 loc) · 7.1 KB
/
main.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
// Copyright (c) 2018-2019, The Decred-Next developers
// Use of this source code is governed by an ISC license
// that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
vegeta "github.com/tsenart/vegeta/lib"
)
const (
outputFileTemplate = "result_%s_%s.json"
resultChanBuffer = 4096
)
// Attack specifies a duration and a series of attackers, each with their own
// frequency and targets. Attack is unmarshalled from a JSON definition file.
type Attack struct {
Duration int `json:"duration"`
Attackers []*Attacker `json:"attackers"`
}
// Attacker defines a list of endpoints for, and a frequency of, attack.
// An optional name will be displayed with error messages.
// An individual Attacker can be delayed, allowing attacks to be layered in
// sections. Frequency is 1/second and endpoints are relative to config.Server.
type Attacker struct {
Name string `json:"name"`
Frequency int `json:"frequency"`
Delay float32 `json:"delay"`
Endpoints []string `json:"endpoints"`
}
var Config *config
func main() {
var err error
Config, err = loadConfig()
if err != nil {
log.Fatalf("%v", err)
}
// Profiles load from a JSON file. The default profiles are in profiles.json.
profiles, err := loadProfiles()
if err != nil {
log.Fatalf("%v", err)
}
if Config.ListAttacks {
listAttacks(profiles)
return
}
if Config.Attack == "" {
fmt.Print("No attack specified. Specify an attack with -a.\n\n")
listAttacks(profiles)
return
}
attackProfile, ok := profiles[Config.Attack]
if !ok {
fmt.Printf("Specified attack profile, %s, not found in definitions at %s\n\n", Config.Attack, Config.ProfilesPath)
listAttacks(profiles)
return
}
err = createResultsDirectory()
if err != nil {
log.Fatalf("%v", err)
}
// If CPUs are specified, set a max
if Config.CPUs >= 1 {
runtime.GOMAXPROCS(Config.CPUs)
log.Printf("Running on %d CPUs.", Config.CPUs)
}
var wg sync.WaitGroup
resChan := make(chan *vegeta.Result, resultChanBuffer)
// Duration from profile can be overridden in config or cli argument.
seconds := attackProfile.Duration
if Config.Duration > 0 {
seconds = Config.Duration
}
// Perform the load test.
log.Printf("Beginning %s. Duration: %d seconds.", Config.Attack, seconds)
duration := time.Duration(seconds*1000) * time.Millisecond
server := strings.TrimSuffix(Config.Server, "/")
for idx, attacker := range attackProfile.Attackers {
if len(attacker.Endpoints) == 0 {
log.Fatalf("Empty endpoint list encountered for %s", Config.Attack)
}
frequency := attacker.Frequency
if Config.Frequency > 0 {
frequency = Config.Frequency
}
rate := vegeta.Rate{
Freq: frequency,
Per: time.Second,
}
var targets []vegeta.Target
for _, endpoint := range attacker.Endpoints {
targets = append(targets, vegeta.Target{
Method: "GET",
URL: fmt.Sprintf("%s/%s", server, strings.TrimLeft(endpoint, "/")),
})
}
targeter := vegeta.NewStaticTargeter(targets...)
// Truncate all response bodies at 100 bytes
vegAttacker := vegeta.NewAttacker(vegeta.MaxBody(100))
wg.Add(1)
go func(attacker *Attacker, id int) {
defer wg.Done()
remainder := duration
if attacker.Delay > 0 {
if attacker.Delay >= float32(attackProfile.Duration) {
log.Printf("The specified delay, %f, for attacker %d is >= the duration of test, %s.",
attacker.Delay, id, Config.Attack)
return
}
attackDelay := time.Duration(int(attacker.Delay*1000)) * time.Millisecond
time.Sleep(attackDelay)
remainder -= attackDelay
}
if attacker.Name != "" {
log.Printf("Beginning %s", attacker.Name)
}
for res := range vegAttacker.Attack(targeter, rate, remainder, strconv.Itoa(id)) {
resChan <- res
}
}(attacker, idx)
}
// Make a notification channel for the end of the attack.
c := make(chan struct{})
go func() {
wg.Wait()
close(c)
}()
// A couple of helper functions.
// Vegeta doesn't return the url in the result, so look it up,
// finding the Attacker along the way.
findAttacker := func(result *vegeta.Result) (attacker *Attacker, endpoint string) {
attackerIdx, err := strconv.Atoi(result.Attack)
if err != nil {
log.Fatalf("Failed to parse attacker index. Something is terribly wrong.")
return
}
attacker = attackProfile.Attackers[attackerIdx]
endpoint = attacker.Endpoints[int(result.Seq)%len(attacker.Endpoints)]
return
}
// A list of unique error URLs. Print messages for new ones. Ignore repeats.
var problematic []string
addProblematic := func(result *vegeta.Result) {
_, endpoint := findAttacker(result)
for _, url := range problematic {
if url == endpoint {
return
}
}
problematic = append(problematic, endpoint)
if result.Code == 0 {
log.Printf("Code 0 requesting %s. Connection failed.", endpoint)
} else {
log.Printf("Error code %d from %s: %s",
result.Code, endpoint, string(result.Body))
}
}
delay := time.Duration(1) * time.Millisecond
var metrics vegeta.Metrics
var res *vegeta.Result
// Monitor results. Wait for end-of-attack signal.
out:
for {
select {
case res = <-resChan:
if res.Code != 200 {
addProblematic(res)
}
// If more info is needed for e.g. plotting, grab it here.
metrics.Add(res)
case <-c:
break out
case <-time.After(delay):
}
}
metrics.Close()
// Vegeta report to terminal.
vegeta.NewTextReporter(&metrics)(os.Stdout)
// JSON results file.
fmtTime := time.Now().Format("2016-01-02-15-04-05")
var jsonBytes []byte
if Config.FormatResults {
jsonBytes, err = json.MarshalIndent(metrics, "", " ")
} else {
jsonBytes, err = json.Marshal(metrics)
}
if err != nil {
log.Fatalf("Failed to parse JSON from vegeta.Metrics for %s:", Config.Attack)
}
path := filepath.Join(Config.ResultDirectory, fmt.Sprintf(outputFileTemplate, Config.Attack, fmtTime))
err = ioutil.WriteFile(path, jsonBytes, 0755)
if err != nil {
log.Fatalf("Failed to write result file to %s", path)
}
}
func createResultsDirectory() (err error) {
fileInfo, err := os.Stat(Config.ResultDirectory)
if os.IsNotExist(err) {
err = os.MkdirAll(Config.ResultDirectory, 0755)
if err != nil {
return fmt.Errorf("Unable to create results directory: %v", err)
}
} else if err != nil {
return fmt.Errorf("Unable to retrieve file system information: %v", err)
} else if !fileInfo.IsDir() {
return fmt.Errorf("Unable to create results directory due to a filename conflict.")
}
return
}
func loadProfiles() (profiles map[string]*Attack, err error) {
jsonBytes, err := ioutil.ReadFile(Config.ProfilesPath)
if err != nil {
return nil, fmt.Errorf("Failed to load attack profile definitions from %s: %v", Config.ProfilesPath, err)
}
if err = json.Unmarshal(jsonBytes, &profiles); err != nil {
return nil, fmt.Errorf("Failed to parse JSON from attack profiles: %v", err)
}
if len(profiles) == 0 {
return nil, fmt.Errorf("No attack profiles found.")
}
return
}
func listAttacks(profiles map[string]*Attack) {
fmt.Println("available attacks")
fmt.Println("-----------------")
for key := range profiles {
fmt.Println(key)
}
}