-
Notifications
You must be signed in to change notification settings - Fork 315
/
args_parser.go
266 lines (247 loc) · 6.96 KB
/
args_parser.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
package main
import (
"fmt"
"runtime"
"strconv"
"strings"
"time"
"github.com/alecthomas/kingpin"
"github.com/goware/urlx"
)
type argsParser interface {
parse([]string) (config, error)
}
type kingpinParser struct {
app *kingpin.Application
url string
numReqs *nullableUint64
duration *nullableDuration
headers *headersList
numConns uint64
timeout time.Duration
latencies bool
insecure bool
disableKeepAlives bool
method string
body string
bodyFilePath string
stream bool
certPath string
keyPath string
rate *nullableUint64
clientType clientTyp
printSpec *nullableString
noPrint bool
formatSpec string
}
func newKingpinParser() argsParser {
kparser := &kingpinParser{
numReqs: new(nullableUint64),
duration: new(nullableDuration),
headers: new(headersList),
numConns: defaultNumberOfConns,
timeout: defaultTimeout,
latencies: false,
method: "GET",
body: "",
bodyFilePath: "",
stream: false,
certPath: "",
keyPath: "",
insecure: false,
url: "",
rate: new(nullableUint64),
clientType: fhttp,
printSpec: new(nullableString),
noPrint: false,
formatSpec: "plain-text",
}
app := kingpin.New("", "Fast cross-platform HTTP benchmarking tool").
Version("bombardier version " + version + " " + runtime.GOOS + "/" +
runtime.GOARCH)
app.Flag("connections", "Maximum number of concurrent connections").
Short('c').
PlaceHolder(strconv.FormatUint(defaultNumberOfConns, decBase)).
Uint64Var(&kparser.numConns)
app.Flag("timeout", "Socket/request timeout").
PlaceHolder(defaultTimeout.String()).
Short('t').
DurationVar(&kparser.timeout)
app.Flag("latencies", "Print latency statistics").
Short('l').
BoolVar(&kparser.latencies)
app.Flag("method", "Request method").
PlaceHolder("GET").
Short('m').
StringVar(&kparser.method)
app.Flag("body", "Request body").
Default("").
Short('b').
StringVar(&kparser.body)
app.Flag("body-file", "File to use as request body").
Default("").
Short('f').
StringVar(&kparser.bodyFilePath)
app.Flag("stream", "Specify whether to stream body using "+
"chunked transfer encoding or to serve it from memory").
Short('s').
BoolVar(&kparser.stream)
app.Flag("cert", "Path to the client's TLS Certificate").
Default("").
StringVar(&kparser.certPath)
app.Flag("key", "Path to the client's TLS Certificate Private Key").
Default("").
StringVar(&kparser.keyPath)
app.Flag("insecure",
"Controls whether a client verifies the server's certificate"+
" chain and host name").
Short('k').
BoolVar(&kparser.insecure)
app.Flag("disableKeepAlives",
"Disable HTTP keep-alive. For fasthttp use -H 'Connection: close'").
Short('a').
BoolVar(&kparser.disableKeepAlives)
app.Flag("header", "HTTP headers to use(can be repeated)").
PlaceHolder("\"K: V\"").
Short('H').
SetValue(kparser.headers)
app.Flag("requests", "Number of requests").
PlaceHolder("[pos. int.]").
Short('n').
SetValue(kparser.numReqs)
app.Flag("duration", "Duration of test").
PlaceHolder(defaultTestDuration.String()).
Short('d').
SetValue(kparser.duration)
app.Flag("rate", "Rate limit in requests per second").
PlaceHolder("[pos. int.]").
Short('r').
SetValue(kparser.rate)
app.Flag("fasthttp", "Use fasthttp client").
Action(func(*kingpin.ParseContext) error {
kparser.clientType = fhttp
return nil
}).
Bool()
app.Flag("http1", "Use net/http client with forced HTTP/1.x").
Action(func(*kingpin.ParseContext) error {
kparser.clientType = nhttp1
return nil
}).
Bool()
app.Flag("http2", "Use net/http client with enabled HTTP/2.0").
Action(func(*kingpin.ParseContext) error {
kparser.clientType = nhttp2
return nil
}).
Bool()
app.Flag(
"print", "Specifies what to output. Comma-separated list of values"+
" 'intro' (short: 'i'), 'progress' (short: 'p'),"+
" 'result' (short: 'r'). Examples:"+
"\n\t* i,p,r (prints everything)"+
"\n\t* intro,result (intro & result)"+
"\n\t* r (result only)"+
"\n\t* result (same as above)").
PlaceHolder("<spec>").
Short('p').
SetValue(kparser.printSpec)
app.Flag("no-print", "Don't output anything").
Short('q').
BoolVar(&kparser.noPrint)
app.Flag("format", "Which format to use to output the result. "+
"<spec> is either a name (or its shorthand) of some format "+
"understood by bombardier or a path to the user-defined template, "+
"which uses Go's text/template syntax, prefixed with 'path:' string "+
"(without single quotes), i.e. \"path:/some/path/to/your.template\" "+
" or \"path:C:\\some\\path\\to\\your.template\" in case of Windows. "+
"Formats understood by bombardier are:"+
"\n\t* plain-text (short: pt)"+
"\n\t* json (short: j)").
PlaceHolder("<spec>").
Short('o').
StringVar(&kparser.formatSpec)
app.Arg("url", "Target's URL").Required().
StringVar(&kparser.url)
kparser.app = app
return argsParser(kparser)
}
func (k *kingpinParser) parse(args []string) (config, error) {
k.app.Name = args[0]
_, err := k.app.Parse(args[1:])
if err != nil {
return emptyConf, err
}
pi, pp, pr := true, true, true
if k.printSpec.val != nil {
pi, pp, pr, err = parsePrintSpec(*k.printSpec.val)
if err != nil {
return emptyConf, err
}
}
if k.noPrint {
pi, pp, pr = false, false, false
}
format := formatFromString(k.formatSpec)
if format == nil {
return emptyConf, fmt.Errorf(
"unknown format or invalid format spec %q", k.formatSpec,
)
}
url, err := urlx.Parse(k.url)
if err != nil {
return emptyConf, err
}
return config{
numConns: k.numConns,
numReqs: k.numReqs.val,
duration: k.duration.val,
url: url,
headers: k.headers,
timeout: k.timeout,
method: k.method,
body: k.body,
bodyFilePath: k.bodyFilePath,
stream: k.stream,
keyPath: k.keyPath,
certPath: k.certPath,
printLatencies: k.latencies,
insecure: k.insecure,
disableKeepAlives: k.disableKeepAlives,
rate: k.rate.val,
clientType: k.clientType,
printIntro: pi,
printProgress: pp,
printResult: pr,
format: format,
}, nil
}
func parsePrintSpec(spec string) (bool, bool, bool, error) {
pi, pp, pr := false, false, false
if spec == "" {
return false, false, false, errEmptyPrintSpec
}
parts := strings.Split(spec, ",")
partsCount := 0
for _, p := range parts {
switch p {
case "i", "intro":
pi = true
case "p", "progress":
pp = true
case "r", "result":
pr = true
default:
return false, false, false,
fmt.Errorf("%q is not a valid part of print spec", p)
}
partsCount++
}
if partsCount < 1 || partsCount > 3 {
return false, false, false,
fmt.Errorf(
"spec %q has too many parts, at most 3 are allowed", spec,
)
}
return pi, pp, pr, nil
}