-
Notifications
You must be signed in to change notification settings - Fork 140
/
main.go
312 lines (267 loc) · 8.96 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
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
package main
import (
"fmt"
"io"
"net"
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"time"
"golang.org/x/time/rate"
"gopkg.in/alecthomas/kingpin.v3-unstable"
)
var (
concurrency = kingpin.Flag("concurrency", "Number of connections to run concurrently").Short('c').Default("1").Int()
reqRate = rateFlag(kingpin.Flag("rate", "Number of requests per time unit, examples: --rate 50 --rate 10/ms").Default("infinity"))
requests = kingpin.Flag("requests", "Number of requests to run").Short('n').Default("-1").Int64()
duration = kingpin.Flag("duration", "Duration of test, examples: -d 10s -d 3m").Short('d').PlaceHolder("DURATION").Duration()
interval = kingpin.Flag("interval", "Print snapshot result every interval, use 0 to print once at the end").Short('i').Default("200ms").Duration()
seconds = kingpin.Flag("seconds", "Use seconds as time unit to print").Bool()
jsonFormat = kingpin.Flag("json", "Print snapshot result as JSON").Bool()
body = kingpin.Flag("body", "HTTP request body, if body starts with '@' the rest will be considered a file's path from which to read the actual body content").Short('b').String()
stream = kingpin.Flag("stream", "Specify whether to stream file specified by '--body @file' using chunked encoding or to read into memory").Default("false").Bool()
methodSet = false
method = kingpin.Flag("method", "HTTP method").Action(func(_ *kingpin.ParseElement, _ *kingpin.ParseContext) error {
methodSet = true
return nil
}).Default("GET").Short('m').String()
headers = kingpin.Flag("header", "Custom HTTP headers").Short('H').PlaceHolder("K:V").Strings()
host = kingpin.Flag("host", "Host header").String()
contentType = kingpin.Flag("content", "Content-Type header").Short('T').String()
cert = kingpin.Flag("cert", "Path to the client's TLS Certificate").ExistingFile()
key = kingpin.Flag("key", "Path to the client's TLS Certificate Private Key").ExistingFile()
insecure = kingpin.Flag("insecure", "Controls whether a client verifies the server's certificate chain and host name").Short('k').Bool()
chartsListenAddr = kingpin.Flag("listen", "Listen addr to serve Web UI").Default(":18888").String()
timeout = kingpin.Flag("timeout", "Timeout for each http request").PlaceHolder("DURATION").Duration()
dialTimeout = kingpin.Flag("dial-timeout", "Timeout for dial addr").PlaceHolder("DURATION").Duration()
reqWriteTimeout = kingpin.Flag("req-timeout", "Timeout for full request writing").PlaceHolder("DURATION").Duration()
respReadTimeout = kingpin.Flag("resp-timeout", "Timeout for full response reading").PlaceHolder("DURATION").Duration()
socks5 = kingpin.Flag("socks5", "Socks5 proxy").PlaceHolder("ip:port").String()
autoOpenBrowser = kingpin.Flag("auto-open-browser", "Specify whether auto open browser to show web charts").Bool()
clean = kingpin.Flag("clean", "Clean the histogram bar once its finished. Default is true").Default("true").NegatableBool()
outputErrors = kingpin.Flag("output-errors", "Output errors to file").String()
summary = kingpin.Flag("summary", "Only print the summary without realtime reports").Default("false").Bool()
pprofAddr = kingpin.Flag("pprof", "Enable pprof at special address").Hidden().String()
url = kingpin.Arg("url", "Request url").Required().String()
)
// dynamically set by GoReleaser
var version = "dev"
func errAndExit(msg string) {
fmt.Fprintln(os.Stderr, "plow: "+msg)
os.Exit(1)
}
var CompactUsageTemplate = `{{define "FormatCommand" -}}
{{if .FlagSummary}} {{.FlagSummary}}{{end -}}
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}} ...{{end}}{{if not .Required}}]{{end}}{{end -}}
{{end -}}
{{define "FormatCommandList" -}}
{{range . -}}
{{if not .Hidden -}}
{{.Depth|Indent}}{{.Name}}{{if .Default}}*{{end}}{{template "FormatCommand" .}}
{{end -}}
{{template "FormatCommandList" .Commands -}}
{{end -}}
{{end -}}
{{define "FormatUsage" -}}
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{if .Help}}
{{.Help|Wrap 0 -}}
{{end -}}
{{end -}}
{{if .Context.SelectedCommand -}}
{{T "usage:"}} {{.App.Name}} {{template "FormatUsage" .Context.SelectedCommand}}
{{else -}}
{{T "usage:"}} {{.App.Name}}{{template "FormatUsage" .App}}
{{end -}}
Examples:
plow http://127.0.0.1:8080/ -c 20 -n 100000
plow https://httpbin.org/post -c 20 -d 5m --body @file.json -T 'application/json' -m POST
{{if .Context.Flags -}}
{{T "Flags:"}}
{{.Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
Flags default values also read from env PLOW_SOME_FLAG, such as PLOW_TIMEOUT=5s equals to --timeout=5s
{{end -}}
{{if .Context.Args -}}
{{T "Args:"}}
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{if .Context.SelectedCommand -}}
{{if .Context.SelectedCommand.Commands -}}
{{T "Commands:"}}
{{.Context.SelectedCommand}}
{{.Context.SelectedCommand.Commands|CommandsToTwoColumns|FormatTwoColumns}}
{{end -}}
{{else if .App.Commands -}}
{{T "Commands:"}}
{{.App.Commands|CommandsToTwoColumns|FormatTwoColumns}}
{{end -}}
`
type rateFlagValue struct {
infinity bool
limit rate.Limit
v string
}
func (f *rateFlagValue) Set(v string) error {
if v == "infinity" {
f.infinity = true
return nil
}
retErr := fmt.Errorf("--rate format %q doesn't match the \"freq/duration\" (i.e. 50/1s)", v)
ps := strings.SplitN(v, "/", 2)
switch len(ps) {
case 1:
ps = append(ps, "1s")
case 0:
return retErr
}
freq, err := strconv.Atoi(ps[0])
if err != nil {
return retErr
}
if freq == 0 {
f.infinity = true
return nil
}
switch ps[1] {
case "ns", "us", "µs", "ms", "s", "m", "h":
ps[1] = "1" + ps[1]
}
per, err := time.ParseDuration(ps[1])
if err != nil {
return retErr
}
f.limit = rate.Limit(float64(freq) / per.Seconds())
f.v = v
return nil
}
func (f *rateFlagValue) Limit() *rate.Limit {
if f.infinity {
return nil
}
return &f.limit
}
func (f *rateFlagValue) String() string {
return f.v
}
func rateFlag(c *kingpin.Clause) (target *rateFlagValue) {
target = new(rateFlagValue)
c.SetValue(target)
return
}
func main() {
kingpin.UsageTemplate(CompactUsageTemplate).
Version(version).
Author("six-ddc@github").
Resolver(kingpin.PrefixedEnvarResolver("PLOW_", ";")).
Help = `A high-performance HTTP benchmarking tool with real-time web UI and terminal displaying`
kingpin.Parse()
if *requests >= 0 && *requests < int64(*concurrency) {
errAndExit("requests must greater than or equal concurrency")
return
}
if (*cert != "" && *key == "") || (*cert == "" && *key != "") {
errAndExit("must specify cert and key at the same time")
return
}
if *pprofAddr != "" {
go http.ListenAndServe(*pprofAddr, nil)
}
var err error
var bodyBytes []byte
var bodyFile string
if *body != "" {
if strings.HasPrefix(*body, "@") {
fileName := (*body)[1:]
if _, err = os.Stat(fileName); err != nil {
errAndExit(err.Error())
return
}
if *stream {
bodyFile = fileName
} else {
bodyBytes, err = os.ReadFile(fileName)
if err != nil {
errAndExit(err.Error())
return
}
}
} else {
bodyBytes = []byte(*body)
}
if !methodSet {
*method = "POST"
}
}
errWriter := io.Discard
if *outputErrors != "" {
errWriter, err = os.Create(*outputErrors)
if err != nil {
errAndExit(err.Error())
return
}
}
clientOpt := ClientOpt{
url: *url,
method: *method,
headers: *headers,
bodyBytes: bodyBytes,
bodyFile: bodyFile,
certPath: *cert,
keyPath: *key,
insecure: *insecure,
maxConns: *concurrency,
doTimeout: *timeout,
readTimeout: *respReadTimeout,
writeTimeout: *reqWriteTimeout,
dialTimeout: *dialTimeout,
socks5Proxy: *socks5,
contentType: *contentType,
host: *host,
}
requester, err := NewRequester(*concurrency, *requests, *duration, reqRate.Limit(), errWriter, &clientOpt)
if err != nil {
errAndExit(err.Error())
return
}
// description
var desc string
desc = fmt.Sprintf("Benchmarking %s", *url)
if *requests > 0 {
desc += fmt.Sprintf(" with %d request(s)", *requests)
}
if *duration > 0 {
desc += fmt.Sprintf(" for %s", duration.String())
}
desc += fmt.Sprintf(" using %d connection(s).", *concurrency)
fmt.Fprintln(os.Stderr, desc)
// charts listener
var ln net.Listener
if *chartsListenAddr != "" {
ln, err = net.Listen("tcp", *chartsListenAddr)
if err != nil {
errAndExit(err.Error())
return
}
fmt.Fprintf(os.Stderr, "@ Real-time charts is listening on http://%s\n", ln.Addr().String())
}
fmt.Fprintln(os.Stderr, "")
// do request
go requester.Run()
// metrics collection
report := NewStreamReport()
go report.Collect(requester.RecordChan())
if ln != nil {
// serve charts data
charts, err := NewCharts(ln, report.Charts, desc)
if err != nil {
errAndExit(err.Error())
return
}
go charts.Serve(*autoOpenBrowser)
}
// terminal printer
printer := NewPrinter(*requests, *duration, !*clean, *summary)
printer.PrintLoop(report.Snapshot, *interval, *seconds, *jsonFormat, report.Done())
}