forked from codesenberg/bombardier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_parser.go
167 lines (153 loc) · 4.19 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
package main
import (
"runtime"
"strconv"
"time"
"github.com/alecthomas/kingpin"
)
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
method string
body string
bodyFilePath string
stream bool
certPath string
keyPath string
rate *nullableUint64
clientType clientTyp
}
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,
}
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("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.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
}
return config{
numConns: k.numConns,
numReqs: k.numReqs.val,
duration: k.duration.val,
url: k.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,
rate: k.rate.val,
clientType: k.clientType,
}, nil
}