-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstress.go
255 lines (232 loc) · 6.64 KB
/
stress.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 (
"flag"
"fmt"
"io/ioutil"
"net/http"
gurl "net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
lbstress "github.com/wenjiax/stress/stress"
)
var (
m = flag.String("m", "GET", "")
// headers = flag.String("h", "", "")
body = flag.String("b", "", "")
bodyFile = flag.String("B", "", "")
output = flag.String("o", "", "")
proxyAddr = flag.String("x", "", "")
host = flag.String("host", "", "")
n = flag.Int("n", 100, "")
c = flag.Int("c", 10, "")
t = flag.Int("t", 20, "")
d = flag.Int("d", 0, "")
thinkTime = flag.Int("think-time", 0, "")
h2 = flag.Bool("h2", false, "")
disableCompression = flag.Bool("disable-compression", false, "")
disableKeepalive = flag.Bool("disable-keepalive", false, "")
disableRedirects = flag.Bool("disable-redirects", false, "")
enableTran = flag.Bool("enable-tran", false, "")
)
const (
headerRegexp = `^([\w-]+):\s*(.+)`
methodsRegexp = `m:([a-zA-Z]+),*`
bodyRegexp = `b:([^,]+),*`
bodyFileRegexp = `B:([^,]+),*`
proxyAddrRegexp = `x:([^,]+),*`
thinkTimeRegexp = `thinkTime:([\d]+),*`
)
var usage = `Usage: stress [options...] <url> || stress [options...] -enable-tran <urls...>
Options:
-n Number of requests to run. Default value is 100.
If set to -1, the request has been sent, but the report will
not be output by default.
-c Number of requests to run concurrently.
Total number of requests cannot smaller than the concurrency level.
Default value is 10.
-d Duration of requests to run. Default value is 0 sec.
-o Output file path. For example: /home/user or ./files.
-h Custom HTTP header. For example:
-h "Accept: text/html" -h "Content-Type: application/xml".
-m HTTP method, any of GET, POST, PUT, DELETE, HEAD, OPTIONS.
-t Timeout for each request in seconds. Default value is 20,
use 0 for infinite.
-b HTTP request body.
-B HTTP request body from file. For example:
/home/user/file.txt or ./file.txt.
-x HTTP Proxy address as host:port.
-h2 Enable HTTP/2.
-host Set HTTP Host header.
-think-time Time to think after request. Default value is 0 sec.
-disable-compression Disable compression.
-disable-keepalive Disable keep-alive, prevents re-use of TCP
connections between different HTTP requests.
-disable-redirects Disable following of HTTP redirects.
-enable-tran Enable transactional requests. Multiple urls
form a transactional requests.
For example: "stress [options...] -enable-tran
http://localhost:8080,m:post,b:hi,x:http://127.0.0.1:8888
http://localhost:8888,m:post,B:/home/file.txt,thinkTime:2
[urls...]".
`
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
}
var hs headerSlice
flag.Var(&hs, "h", "")
flag.Parse()
if flag.NArg() <= 0 {
usageAndExit("")
}
// Parsing global request header.
header := make(http.Header)
// hs := strings.Split(*headers, ";")
for _, h := range hs {
match, err := parseInputWithRegexp(h, headerRegexp)
if err != nil {
usageAndExit(err.Error())
}
header.Set(match[1], match[2])
}
// Parsing global request proxyAddr.
var proxyURL *gurl.URL
if *proxyAddr != "" {
var err error
proxyURL, err = gurl.Parse(*proxyAddr)
if err != nil {
usageAndExit(err.Error())
}
}
// Set parameters and global configuration.
task := &lbstress.Task{
Number: *n,
Concurrent: *c,
Duration: time.Duration(*d) * time.Second,
Output: *output,
Timeout: *t,
ThinkTime: *thinkTime,
ProxyAddr: proxyURL,
DisableCompression: *disableCompression,
DisableKeepAlives: *disableKeepalive,
DisableRedirects: *disableRedirects,
Host: *host,
H2: *h2,
}
if *enableTran {
runTran(task, header)
} else {
run(task, header)
}
}
func run(task *lbstress.Task, header http.Header) {
// Parsing request body.
var bodyAll []byte
if *body != "" {
bodyAll = []byte(*body)
}
if *bodyFile != "" {
content, err := ioutil.ReadFile(*bodyFile)
if err != nil {
errAndExit(err.Error())
}
bodyAll = content
}
// Run task.
err := task.Run(&lbstress.RequestConfig{
URLStr: flag.Args()[0],
Method: *m,
ReqBody: bodyAll,
Header: header,
})
if err != nil {
errAndExit(err.Error())
}
}
func runTran(task *lbstress.Task, header http.Header) {
var configs []*lbstress.RequestConfig
for i, len := 0, flag.NArg(); i < len; i++ {
argstr := flag.Args()[i]
url := strings.Split(argstr, ",")[0]
// Parsing request method.
methodMatch, err := parseInputWithRegexp(argstr, methodsRegexp)
if err != nil {
errAndExit(err.Error())
}
// Parsing request body.
bodyMatch, _ := parseInputWithRegexp(argstr, bodyRegexp)
bodyFileMatch, _ := parseInputWithRegexp(argstr, bodyFileRegexp)
var bodyAll []byte
if bodyMatch != nil {
bodyAll = []byte(bodyMatch[1])
}
if bodyFileMatch != nil {
content, err := ioutil.ReadFile(bodyFileMatch[1])
if err != nil {
errAndExit(err.Error())
}
bodyAll = content
}
// Parsing request proxyAddr.
proxyAddrMatch, _ := parseInputWithRegexp(argstr, proxyAddrRegexp)
var proxyURL *gurl.URL
if proxyAddrMatch != nil {
var err error
proxyURL, err = gurl.Parse(*proxyAddr)
if err != nil {
usageAndExit(err.Error())
}
}
// Parsing request thinkTime.
thinkTime := 0
thinkTimeMatch, _ := parseInputWithRegexp(argstr, thinkTimeRegexp)
if thinkTimeMatch != nil {
thinkTime, _ = strconv.Atoi(thinkTimeMatch[1])
}
configs = append(configs, &lbstress.RequestConfig{
URLStr: url,
Method: methodMatch[1],
ReqBody: bodyAll,
Header: header,
ProxyAddr: proxyURL,
ThinkTime: thinkTime,
})
}
// Run transactional task.
err := task.RunTran(configs...)
if err != nil {
errAndExit(err.Error())
}
}
func parseInputWithRegexp(input, regx string) ([]string, error) {
re := regexp.MustCompile(regx)
matches := re.FindStringSubmatch(input)
if len(matches) < 1 {
return nil, fmt.Errorf("could not parse the provided input; input = %v", input)
}
return matches, nil
}
func usageAndExit(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func errAndExit(msg string) {
fmt.Fprintf(os.Stderr, "Error:%s\n", msg)
os.Exit(1)
}
type headerSlice []string
func (h *headerSlice) String() string {
return fmt.Sprintf("%s", *h)
}
func (h *headerSlice) Set(value string) error {
*h = append(*h, value)
return nil
}