-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
395 lines (333 loc) · 11.7 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
"time"
"github.com/yarpc/yab/encoding"
"github.com/yarpc/yab/peerprovider"
"github.com/yarpc/yab/transport"
"github.com/casimir/xdg-go"
"github.com/jessevdk/go-flags"
"github.com/opentracing/opentracing-go"
opentracing_ext "github.com/opentracing/opentracing-go/ext"
"github.com/uber/jaeger-client-go"
"github.com/uber/tchannel-go"
)
var errHealthAndProcedure = errors.New("cannot specify procedure and use --health")
func findGroup(parser *flags.Parser, group string) *flags.Group {
if g := parser.Group.Find(group); g != nil {
return g
}
panic("no group called " + group + " found.")
}
func setGroupDescs(parser *flags.Parser, groupName, shortDesc, longDesc string) {
g := findGroup(parser, groupName)
g.ShortDescription = shortDesc
g.LongDescription = longDesc
}
func fromPositional(args []string, index int, s *string) bool {
if len(args) <= index {
return false
}
if args[index] != "" {
*s = args[index]
}
return true
}
func main() {
log.SetFlags(0)
parseAndRun(consoleOutput{os.Stdout})
}
var errExit = errors.New("sentinel error used to exit cleanly")
func toGroff(s string) string {
// Expand tabbed lines beginning with "-" as items in a bullet list.
s = strings.Replace(s, "\n\t* ", "\n.IP \\[bu]\n", -1 /* all occurences */)
// Two newlines start a new paragraph.
s = strings.Replace(s, "\n\n", "\n.PP\n", -1)
// Lines beginning with a tab are interpreted as example code.
//
// See http://liw.fi/manpages/ for an explanation of these
// commands -- tl;dr: turn of paragraph filling and indent the
// block one level.
indentRegexp := regexp.MustCompile(`\t(.*)\n`)
s = indentRegexp.ReplaceAllString(s, ".nf\n.RS\n$1\n.RE\n.fi\n")
return s
}
func newParser() (*flags.Parser, *Options) {
opts := newOptions()
return flags.NewParser(opts, flags.HelpFlag|flags.PassDoubleDash), opts
}
func getOptions(args []string, out output) (*Options, error) {
parser, opts := newParser()
parser.Usage = "[<service> <method> <body>] [OPTIONS]"
parser.ShortDescription = "yet another benchmarker"
parser.LongDescription = `
yab is a benchmarking tool for TChannel and HTTP applications. It's primarily intended for Thrift applications but supports other encodings like JSON and binary (raw). It can be used in a curl-like fashion when benchmarking features are disabled.
`
// Read defaults if they're available, before we change the group names.
if err := parseDefaultConfigs(parser); err != nil {
return nil, fmt.Errorf("error reading defaults: %v", err)
}
overrideDefaults(opts, args)
setGroupDescs(parser, "request", "Request Options", toGroff(_reqOptsDesc))
setGroupDescs(parser, "transport", "Transport Options", toGroff(_transportOptsDesc))
setGroupDescs(parser, "benchmark", "Benchmark Options", toGroff(_benchmarkOptsDesc))
remaining, err := parser.ParseArgs(args)
// If there are no arguments specified, write the help.
// We do this after Parse, otherwise the output doesn't show defaults.
if len(args) == 0 {
parser.WriteHelp(out)
return opts, errExit
}
if err != nil {
if ferr, ok := err.(*flags.Error); ok {
if ferr.Type == flags.ErrHelp {
parser.WriteHelp(out)
return opts, errExit
}
}
return opts, err
}
setEncodingOptions(opts)
if opts.DisplayVersion {
out.Printf("yab version %v\n", versionString)
return opts, errExit
}
if opts.ManPage {
parser.LongDescription += `
Default options can be specified in a ~/.config/yab/defaults.ini file (or ~/Library/Preferences/yab/defaults.ini on Mac) with contents similar to this:
[request]
timeout = 2s
[transport]
peer-list = "/path/to/peer/list.json"
[benchmark]
warmup = 10
`
parser.LongDescription = toGroff(parser.LongDescription)
parser.WriteManPage(out)
return opts, errExit
}
fromPositional(remaining, 0, &opts.TOpts.ServiceName)
fromPositional(remaining, 1, &opts.ROpts.Procedure)
// We support both:
// [service] [method] [request]
// [service] [method] [headers] [request]
if fromPositional(remaining, 3, &opts.ROpts.RequestJSON) {
fromPositional(remaining, 2, &opts.ROpts.HeadersJSON)
} else {
fromPositional(remaining, 2, &opts.ROpts.RequestJSON)
}
return opts, nil
}
// parseAndRun is like main, but uses the given output.
func parseAndRun(out output) {
opts, err := getOptions(os.Args[1:], out)
if err != nil {
if err == errExit {
return
}
out.Fatalf("Failed to parse options: %v", err)
}
runWithOptions(*opts, out)
}
// overrideDefaults clears fields in the default options that may
// clash with user-specified options.
// E.g., if the defaults has a peer list file, and the user has specifed
// a peer through the command line, then the final options should only
// contain the peer specified in the args.
func overrideDefaults(defaults *Options, args []string) {
argsParser, argsOnly := newParser()
argsParser.ParseArgs(args)
// Clear default peers if the user has specified peer options in args.
if len(argsOnly.TOpts.Peers) > 0 {
defaults.TOpts.PeerList = ""
}
if len(argsOnly.TOpts.PeerList) > 0 {
defaults.TOpts.Peers = nil
}
}
// findBestConfigFile finds the best config file to use. An empty string will be
// returned if no config file should be used.
func findBestConfigFile() string {
app := xdg.App{Name: "yab"}
// Find the best config path to use, preferring the user's config path and
// falling back to the system config path.
configPaths := []string{app.ConfigPath("defaults.ini")}
configPaths = append(configPaths, app.SystemConfigPaths("defaults.ini")...)
var configFile string
for _, path := range configPaths {
if _, err := os.Stat(path); err == nil {
configFile = path
break
}
}
return configFile
}
// parseDefaultConfigs reads defaults from ~/.config/yab/defaults.ini if they're
// available.
func parseDefaultConfigs(parser *flags.Parser) error {
configFile := findBestConfigFile()
if configFile == "" {
return nil // no defaults file was found
}
iniParser := flags.NewIniParser(parser)
if err := iniParser.ParseFile(configFile); err != nil {
return fmt.Errorf("couldn't read %v: %v", configFile, err)
}
return nil
}
func runWithOptions(opts Options, out output) {
if opts.TOpts.PeerList == "?" {
for _, scheme := range peerprovider.Schemes() {
out.Printf("%s\n", scheme)
}
return
}
if opts.ROpts.YamlTemplate != "" {
if err := readYAMLRequest(&opts); err != nil {
out.Fatalf("Failed while reading yaml template: %v\n", err)
}
}
reqInput, err := getRequestInput(opts.ROpts.RequestJSON, opts.ROpts.RequestFile)
if err != nil {
out.Fatalf("Failed while loading body input: %v\n", err)
}
headers, err := getHeaders(opts.ROpts.HeadersJSON, opts.ROpts.HeadersFile, opts.ROpts.Headers)
if err != nil {
out.Fatalf("Failed while loading headers input: %v\n", err)
}
serializer, err := NewSerializer(opts.ROpts)
if err != nil {
out.Fatalf("Failed while parsing input: %v\n", err)
}
if opts.TOpts.CallerName != "" {
if opts.BOpts.enabled() {
out.Fatalf("Cannot override caller name when running benchmarks\n")
}
} else {
opts.TOpts.CallerName = "yab-" + os.Getenv("USER")
}
tracer, closer := getTracer(opts, out)
if closer != nil {
defer closer.Close()
}
// transport abstracts the underlying wire protocol used to make the call.
transport, err := getTransport(opts.TOpts, serializer.Encoding(), tracer)
if err != nil {
out.Fatalf("Failed while parsing options: %v\n", err)
}
serializer = withTransportSerializer(transport.Protocol(), serializer, opts.ROpts)
// req is the transport.Request that will be used to make a call.
req, err := serializer.Request(reqInput)
if err != nil {
out.Fatalf("Failed while parsing request input: %v\n", err)
}
req.Headers = headers
req.TransportHeaders = opts.TOpts.TransportHeaders
req.Timeout = opts.ROpts.Timeout.Duration()
if req.Timeout == 0 {
req.Timeout = time.Second
}
req.Baggage = opts.ROpts.Baggage
// Only make the request if the user hasn't specified 0 warmup.
if !(opts.BOpts.enabled() && opts.BOpts.WarmupRequests == 0) {
makeInitialRequest(out, transport, serializer, req)
}
runBenchmark(out, opts, benchmarkMethod{
serializer: serializer,
req: req,
})
}
type noEnveloper interface {
WithoutEnvelopes() encoding.Serializer
}
func getTracer(opts Options, out output) (opentracing.Tracer, io.Closer) {
var (
tracer opentracing.Tracer = opentracing.NoopTracer{}
closer io.Closer
)
if opts.TOpts.Jaeger && !opts.TOpts.NoJaeger {
tracer, closer = jaeger.NewTracer(opts.TOpts.CallerName, jaeger.NewConstSampler(true), jaeger.NewNullReporter())
} else if len(opts.ROpts.Baggage) > 0 {
out.Fatalf("To propagate baggage, you must opt-into a tracing client, i.e., --jaeger")
}
return tracer, closer
}
// withTransportSerializer may modify the serializer for the transport used.
// E.g. Thrift payloads are not enveloped when used with TChannel.
func withTransportSerializer(p transport.Protocol, s encoding.Serializer, rOpts RequestOptions) encoding.Serializer {
switch {
case p == transport.TChannel && s.Encoding() == encoding.Thrift,
rOpts.ThriftDisableEnvelopes:
s = s.(noEnveloper).WithoutEnvelopes()
}
return s
}
// makeRequest makes a request using the given transport.
func makeRequest(t transport.Transport, request *transport.Request) (*transport.Response, error) {
return makeRequestWithTracePriority(t, request, 0)
}
func makeRequestWithTracePriority(t transport.Transport, request *transport.Request, trace uint16) (*transport.Response, error) {
ctx, cancel := tchannel.NewContext(request.Timeout)
defer cancel()
if tracer := t.Tracer(); tracer != nil {
span := tracer.StartSpan(request.Method)
opentracing_ext.SamplingPriority.Set(span, trace)
for k, v := range request.Baggage {
span = span.SetBaggageItem(k, v)
}
ctx = opentracing.ContextWithSpan(ctx, span)
}
return t.Call(ctx, request)
}
func makeInitialRequest(out output, transport transport.Transport, serializer encoding.Serializer, req *transport.Request) {
response, err := makeRequestWithTracePriority(transport, req, 1)
if err != nil {
out.Fatalf("Failed while making call: %v\n", err)
}
// responseMap converts the Thrift bytes response to a map.
responseMap, err := serializer.Response(response)
if err != nil {
out.Fatalf("Failed while parsing response: %v\n", err)
}
// Print the initial output body.
outSerialized := map[string]interface{}{
"body": responseMap,
}
if len(response.Headers) > 0 {
outSerialized["headers"] = response.Headers
}
for k, v := range response.TransportFields {
outSerialized[k] = v
}
bs, err := json.MarshalIndent(outSerialized, "", " ")
if err != nil {
out.Fatalf("Failed to convert map to JSON: %v\nMap: %+v\n", err, responseMap)
}
out.Printf("%s\n\n", bs)
}