forked from envoyproxy/nighthawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_impl.cc
717 lines (662 loc) · 33.5 KB
/
options_impl.cc
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#include "client/options_impl.h"
#include "external/envoy/source/common/protobuf/message_validator_impl.h"
#include "external/envoy/source/common/protobuf/utility.h"
#include "api/client/options.pb.validate.h"
#include "common/uri_impl.h"
#include "common/utility.h"
#include "common/version_info.h"
#include "client/output_formatter_impl.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_split.h"
#include "absl/types/optional.h"
#include "fmt/ranges.h"
namespace Nighthawk {
namespace Client {
#define TCLAP_SET_IF_SPECIFIED(command, value_member) \
((value_member) = (((command).isSet()) ? ((command).getValue()) : (value_member)))
OptionsImpl::OptionsImpl(int argc, const char* const* argv) {
setNonTrivialDefaults();
// Override some defaults, we are in CLI-mode.
verbosity_ = nighthawk::client::Verbosity::INFO;
output_format_ = nighthawk::client::OutputFormat::HUMAN;
// TODO(oschaaf): Purge the validation we perform here. Most of it should have become
// redundant now that we also perform validation of the resulting proto.
const char* descr = "L7 (HTTP/HTTPS/HTTP2) performance characterization tool.";
TCLAP::CmdLine cmd(descr, ' ', VersionInfo::version()); // NOLINT
// Any default values we pass into TCLAP argument declarations are arbitrary, as we do not rely on
// TCLAP for providing default values. Default values are declared in and sourced from
// options_impl.h, modulo non-trivial data types (see setNonTrivialDefaults()).
TCLAP::ValueArg<uint32_t> requests_per_second(
"", "rps",
fmt::format("The target requests-per-second rate. Default: {}.", requests_per_second_), false,
0, "uint32_t", cmd);
TCLAP::ValueArg<uint32_t> connections(
"", "connections",
fmt::format("The maximum allowed number of concurrent connections per event loop. HTTP/1 "
"only. Default: {}.",
connections_),
false, 0, "uint32_t", cmd);
TCLAP::ValueArg<uint32_t> duration(
"", "duration",
fmt::format("The number of seconds that the test should run. Default: {}.", duration_), false,
0, "uint32_t", cmd);
TCLAP::ValueArg<uint32_t> timeout(
"", "timeout",
fmt::format("Connection connect timeout period in seconds. Default: {}.", timeout_), false, 0,
"uint32_t", cmd);
TCLAP::SwitchArg h2("", "h2", "Use HTTP/2", cmd);
TCLAP::ValueArg<std::string> concurrency(
"", "concurrency",
fmt::format(
"The number of concurrent event loops that should be used. Specify 'auto' to let "
"Nighthawk leverage all vCPUs that have affinity to the Nighthawk process. Note that "
"increasing this results in an effective load multiplier combined with the configured "
"--rps and --connections values. Default: {}. ",
concurrency_),
false, "", "string", cmd);
std::vector<std::string> log_levels = {"trace", "debug", "info", "warn", "error", "critical"};
TCLAP::ValuesConstraint<std::string> verbosities_allowed(log_levels);
TCLAP::ValueArg<std::string> verbosity(
"v", "verbosity",
fmt::format(
"Verbosity of the output. Possible values: [trace, debug, info, warn, error, "
"critical]. The "
"default level is '{}'.",
absl::AsciiStrToLower(nighthawk::client::Verbosity_VerbosityOptions_Name(verbosity_))),
false, "", &verbosities_allowed, cmd);
std::vector<std::string> output_formats = OutputFormatterImpl::getLowerCaseOutputFormats();
TCLAP::ValuesConstraint<std::string> output_formats_allowed(output_formats);
TCLAP::ValueArg<std::string> output_format(
"", "output-format",
fmt::format("Output format. Possible values: {}. The "
"default output format is '{}'.",
output_formats,
absl::AsciiStrToLower(
nighthawk::client::OutputFormat_OutputFormatOptions_Name(output_format_))),
false, "", &output_formats_allowed, cmd);
TCLAP::SwitchArg prefetch_connections( // NOLINT
"", "prefetch-connections", // NOLINT
"Use proactive connection prefetching (HTTP/1 only).", // NOLINT
cmd); // NOLINT
// Note: we allow a burst size of 1, which intuitively may not make sense. However, allowing it
// doesn't hurt either, and it does allow one to use a the same code-execution-paths in test
// series that ramp up burst sizes.
TCLAP::ValueArg<uint32_t> burst_size(
"", "burst-size",
fmt::format("Release requests in bursts of the specified size (default: {}).", burst_size_),
false, 0, "uint32_t", cmd);
std::vector<std::string> address_families = {"auto", "v4", "v6"};
TCLAP::ValuesConstraint<std::string> address_families_allowed(address_families);
TCLAP::ValueArg<std::string> address_family(
"", "address-family",
fmt::format("Network address family preference. Possible values: [auto, v4, v6]. The "
"default output format is '{}'.",
nighthawk::client::AddressFamily::AddressFamilyOptions_Name(address_family_)),
false, "", &address_families_allowed, cmd);
std::vector<std::string> request_methods = {"GET", "HEAD", "POST", "PUT",
"DELETE", "CONNECT", "OPTIONS", "TRACE"};
TCLAP::ValuesConstraint<std::string> request_methods_allowed(request_methods);
TCLAP::ValueArg<std::string> request_method("", "request-method",
"Request method used when sending requests. The "
"default is 'GET'.",
false, "GET", &request_methods_allowed, cmd);
TCLAP::MultiArg<std::string> request_headers("", "request-header",
"Raw request headers in the format of 'name: value' "
"pairs. This argument may specified multiple times.",
false, "string", cmd);
TCLAP::ValueArg<uint32_t> request_body_size(
"", "request-body-size",
"Size of the request body to send. NH will send a number of consecutive 'a' characters equal "
"to the number specified here. (default: 0, no data).",
false, 0, "uint32_t", cmd);
TCLAP::ValueArg<std::string> tls_context(
"", "tls-context",
"DEPRECATED, use --transport-socket instead. "
"Tls context configuration in json or compact yaml. "
"Mutually exclusive with --transport-socket. Example (json): "
"{common_tls_context:{tls_params:{cipher_suites:[\"-ALL:ECDHE-RSA-AES128-SHA\"]}}}",
false, "", "string", cmd);
TCLAP::ValueArg<std::string> transport_socket(
"", "transport-socket",
"Transport socket configuration in json or compact yaml. "
"Mutually exclusive with --tls-context. Example (json): "
"{name:\"envoy.transport_sockets.tls\",typed_config:{"
"\"@type\":\"type.googleapis.com/envoy.api.v2.auth.UpstreamTlsContext\","
"common_tls_context:{tls_params:{cipher_suites:[\"-ALL:ECDHE-RSA-AES128-SHA\"]}}}}",
false, "", "string", cmd);
TCLAP::ValueArg<uint32_t> max_pending_requests(
"", "max-pending-requests",
fmt::format("Max pending requests (default: {}, no client side queuing. Specifying any other "
"value will "
"allow client-side queuing of requests).",
max_pending_requests_),
false, 0, "uint32_t", cmd);
TCLAP::ValueArg<uint32_t> max_active_requests(
"", "max-active-requests",
fmt::format(
"The maximum allowed number of concurrently active requests. HTTP/2 only. (default: {}).",
max_active_requests_),
false, 0, "uint32_t", cmd);
// NOLINTNEXTLINE
TCLAP::ValueArg<uint32_t> max_requests_per_connection(
"", "max-requests-per-connection",
fmt::format("Max requests per connection (default: {}).", max_requests_per_connection_),
false, 0, "uint32_t", cmd);
std::vector<std::string> sequencer_idle_strategies = {"spin", "poll", "sleep"};
TCLAP::ValuesConstraint<std::string> sequencer_idle_strategies_allowed(sequencer_idle_strategies);
TCLAP::ValueArg<std::string> sequencer_idle_strategy(
"", "sequencer-idle-strategy",
fmt::format(
"Choose between using a busy spin/yield loop or have the thread poll or sleep while "
"waiting for the next scheduled request (default: {}).",
absl::AsciiStrToLower(
nighthawk::client::SequencerIdleStrategy_SequencerIdleStrategyOptions_Name(
sequencer_idle_strategy_))),
false, "", &sequencer_idle_strategies_allowed, cmd);
TCLAP::ValueArg<std::string> trace(
"", "trace", "Trace uri. Example: zipkin://localhost:9411/api/v1/spans. Default is empty.",
false, "", "uri format", cmd);
TCLAP::MultiArg<std::string> termination_predicates(
"", "termination-predicate",
"Termination predicate. Allows specifying a counter name plus threshold value for "
"terminating execution.",
false, "string, uint64_t", cmd);
TCLAP::MultiArg<std::string> failure_predicates(
"", "failure-predicate",
"Failure predicate. Allows specifying a counter name plus threshold value for "
"failing execution. Defaults to not tolerating error status codes and connection errors.",
false, "string, uint64_t", cmd);
std::vector<std::string> h1_connection_reuse_strategies = {"mru", "lru"};
TCLAP::ValuesConstraint<std::string> h1_connection_reuse_strategies_allowed(
h1_connection_reuse_strategies);
TCLAP::ValueArg<std::string> experimental_h1_connection_reuse_strategy(
"", "experimental-h1-connection-reuse-strategy",
fmt::format(
"Choose picking the most recently used, or least-recently-used connections for re-use."
"(default: {}). WARNING: this option is experimental and may be removed or changed in "
"the future!",
absl::AsciiStrToLower(
nighthawk::client::H1ConnectionReuseStrategy_H1ConnectionReuseStrategyOptions_Name(
experimental_h1_connection_reuse_strategy_))),
false, "", &h1_connection_reuse_strategies_allowed, cmd);
TCLAP::SwitchArg open_loop(
"", "open-loop",
"Enable open loop mode. When enabled, the benchmark client will not provide backpressure "
"when resource limits are hit.",
cmd);
TCLAP::ValueArg<std::string> jitter_uniform(
"", "jitter-uniform",
"Add uniformly distributed absolute request-release timing jitter. For example, to add 10 us "
"of jitter, specify .00001s. Default is empty / no uniform jitter.",
false, "", "duration", cmd);
TCLAP::ValueArg<std::string> nighthawk_service(
"", "nighthawk-service",
"Nighthawk service uri. Example: grpc://localhost:8843/. Default is empty.", false, "",
"uri format", cmd);
TCLAP::SwitchArg h2_use_multiple_connections(
"", "experimental-h2-use-multiple-connections",
"Use experimental HTTP/2 pool which will use multiple connections. WARNING: feature may be "
"removed or changed in the future!",
cmd);
TCLAP::MultiArg<std::string> multi_target_endpoints(
"", "multi-target-endpoint",
"Target endpoint in the form IPv4:port, [IPv6]:port, or DNS:port. "
"This argument is intended to be specified multiple times. "
"Nighthawk will spread traffic across all endpoints with "
"round robin distribution. "
"Mutually exclusive with providing a URI.",
false, "string", cmd);
TCLAP::ValueArg<std::string> multi_target_path(
"", "multi-target-path",
"The single absolute path Nighthawk should request from each target endpoint. "
"Required when using --multi-target-endpoint. "
"Mutually exclusive with providing a URI.",
false, "", "string", cmd);
TCLAP::SwitchArg multi_target_use_https(
"", "multi-target-use-https",
"Use HTTPS to connect to the target endpoints. Otherwise HTTP is used. "
"Mutually exclusive with providing a URI.",
cmd);
TCLAP::MultiArg<std::string> labels("", "label",
"Label. Allows specifying multiple labels which will be "
"persisted in structured output formats.",
false, "string", cmd);
TCLAP::UnlabeledValueArg<std::string> uri(
"uri",
"URI to benchmark. http:// and https:// are supported, "
"but in case of https no certificates are validated. "
"Provide a URI when you need to benchmark a single endpoint. For multiple "
"endpoints, set --multi-target-* instead.",
false, "", "uri format", cmd);
TCLAP::ValueArg<std::string> request_source(
"", "request-source",
"Remote gRPC source that will deliver to-be-replayed traffic. Each worker will separately "
"connect to this source. For example grpc://127.0.0.1:8443/.",
false, "", "uri format", cmd);
TCLAP::SwitchArg simple_warmup(
"", "simple-warmup",
"Perform a simple single warmup request (per worker) before starting execution. Note that "
"this will be reflected in the counters that Nighthawk writes to the output. Default is "
"false.",
cmd);
Utility::parseCommand(cmd, argc, argv);
TCLAP_SET_IF_SPECIFIED(requests_per_second, requests_per_second_);
TCLAP_SET_IF_SPECIFIED(connections, connections_);
TCLAP_SET_IF_SPECIFIED(duration, duration_);
TCLAP_SET_IF_SPECIFIED(timeout, timeout_);
if (uri.isSet()) {
uri_ = uri.getValue();
}
TCLAP_SET_IF_SPECIFIED(h2, h2_);
TCLAP_SET_IF_SPECIFIED(concurrency, concurrency_);
// TODO(oschaaf): is there a generic way to set these enum values?
if (verbosity.isSet()) {
std::string upper_cased = verbosity.getValue();
absl::AsciiStrToUpper(&upper_cased);
RELEASE_ASSERT(nighthawk::client::Verbosity::VerbosityOptions_Parse(upper_cased, &verbosity_),
"Failed to parse verbosity");
}
if (output_format.isSet()) {
std::string upper_cased = output_format.getValue();
absl::AsciiStrToUpper(&upper_cased);
RELEASE_ASSERT(
nighthawk::client::OutputFormat::OutputFormatOptions_Parse(upper_cased, &output_format_),
"Failed to parse output format");
}
TCLAP_SET_IF_SPECIFIED(prefetch_connections, prefetch_connections_);
TCLAP_SET_IF_SPECIFIED(burst_size, burst_size_);
if (address_family.isSet()) {
std::string upper_cased = address_family.getValue();
absl::AsciiStrToUpper(&upper_cased);
RELEASE_ASSERT(
nighthawk::client::AddressFamily::AddressFamilyOptions_Parse(upper_cased, &address_family_),
"Failed to parse address family");
}
if (request_method.isSet()) {
std::string upper_cased = request_method.getValue();
absl::AsciiStrToUpper(&upper_cased);
RELEASE_ASSERT(envoy::config::core::v3::RequestMethod_Parse(upper_cased, &request_method_),
"Failed to parse request method");
}
TCLAP_SET_IF_SPECIFIED(request_headers, request_headers_);
TCLAP_SET_IF_SPECIFIED(request_body_size, request_body_size_);
TCLAP_SET_IF_SPECIFIED(max_pending_requests, max_pending_requests_);
TCLAP_SET_IF_SPECIFIED(max_active_requests, max_active_requests_);
TCLAP_SET_IF_SPECIFIED(max_requests_per_connection, max_requests_per_connection_);
if (sequencer_idle_strategy.isSet()) {
std::string upper_cased = sequencer_idle_strategy.getValue();
absl::AsciiStrToUpper(&upper_cased);
RELEASE_ASSERT(nighthawk::client::SequencerIdleStrategy::SequencerIdleStrategyOptions_Parse(
upper_cased, &sequencer_idle_strategy_),
"Failed to parse sequencer idle strategy");
}
TCLAP_SET_IF_SPECIFIED(request_source, request_source_);
if (experimental_h1_connection_reuse_strategy.isSet()) {
std::string upper_cased = experimental_h1_connection_reuse_strategy.getValue();
absl::AsciiStrToUpper(&upper_cased);
const bool ok =
nighthawk::client::H1ConnectionReuseStrategy::H1ConnectionReuseStrategyOptions_Parse(
upper_cased, &experimental_h1_connection_reuse_strategy_);
// TCLAP validation ought to have caught this earlier.
RELEASE_ASSERT(ok, "Failed to parse h1 connection reuse strategy");
}
TCLAP_SET_IF_SPECIFIED(trace, trace_);
parsePredicates(termination_predicates, termination_predicates_);
parsePredicates(failure_predicates, failure_predicates_);
TCLAP_SET_IF_SPECIFIED(open_loop, open_loop_);
if (jitter_uniform.isSet()) {
Envoy::ProtobufWkt::Duration duration;
if (Envoy::Protobuf::util::TimeUtil::FromString(jitter_uniform.getValue(), &duration)) {
if (duration.nanos() > 0 || duration.seconds() > 0) {
jitter_uniform_ = std::chrono::nanoseconds(
Envoy::Protobuf::util::TimeUtil::DurationToNanoseconds(duration));
} else {
throw MalformedArgvException("--jitter-uniform is out of range");
}
} else {
throw MalformedArgvException("Invalid value for --jitter-uniform");
}
}
TCLAP_SET_IF_SPECIFIED(nighthawk_service, nighthawk_service_);
TCLAP_SET_IF_SPECIFIED(h2_use_multiple_connections, h2_use_multiple_connections_);
TCLAP_SET_IF_SPECIFIED(multi_target_use_https, multi_target_use_https_);
TCLAP_SET_IF_SPECIFIED(multi_target_path, multi_target_path_);
if (multi_target_endpoints.isSet()) {
for (const std::string& host_port : multi_target_endpoints.getValue()) {
std::string host;
int port;
if (!Utility::parseHostPort(host_port, &host, &port)) {
throw MalformedArgvException(fmt::format("--multi-target-endpoint must be in the format "
"IPv4:port, [IPv6]:port, or DNS:port. Got '{}'",
host_port));
}
nighthawk::client::MultiTarget::Endpoint endpoint;
endpoint.mutable_address()->set_value(host);
endpoint.mutable_port()->set_value(port);
multi_target_endpoints_.push_back(endpoint);
}
}
TCLAP_SET_IF_SPECIFIED(labels, labels_);
TCLAP_SET_IF_SPECIFIED(simple_warmup, simple_warmup_);
// CLI-specific tests.
// TODO(oschaaf): as per mergconflicts's remark, it would be nice to aggregate
// these and present everything we couldn't understand to the CLI user in on go.
if (requests_per_second_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --rps");
}
if (connections_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --connections");
}
if (duration_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --duration");
}
if (timeout_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --timeout");
}
if (request_body_size_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --request-body-size");
}
if (burst_size_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --burst-size");
}
if (max_pending_requests_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --max-pending-requests");
}
if (max_active_requests_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --max-active-requests");
}
if (max_requests_per_connection_ > largest_acceptable_uint32_option_value) {
throw MalformedArgvException("Invalid value for --max-requests-per-connection");
}
if (!tls_context.getValue().empty()) {
ENVOY_LOG(warn, "--tls-context is deprecated. "
"It can be replaced by an equivalent --transport-socket. "
"See --help for an example.");
}
if (!tls_context.getValue().empty() && !transport_socket.getValue().empty()) {
throw MalformedArgvException("--tls-context and --transport-socket cannot both be set.");
}
if (!tls_context.getValue().empty()) {
try {
Envoy::MessageUtil::loadFromJson(tls_context.getValue(), tls_context_,
Envoy::ProtobufMessage::getStrictValidationVisitor());
} catch (const Envoy::EnvoyException& e) {
throw MalformedArgvException(e.what());
}
}
if (!transport_socket.getValue().empty()) {
try {
transport_socket_.emplace(envoy::config::core::v3::TransportSocket());
Envoy::MessageUtil::loadFromJson(transport_socket.getValue(), transport_socket_.value(),
Envoy::ProtobufMessage::getStrictValidationVisitor());
} catch (const Envoy::EnvoyException& e) {
throw MalformedArgvException(e.what());
}
}
validate();
}
void OptionsImpl::parsePredicates(const TCLAP::MultiArg<std::string>& arg,
TerminationPredicateMap& predicates) {
if (arg.isSet()) {
predicates.clear();
}
for (const auto& predicate : arg) {
std::vector<std::string> split_predicate =
absl::StrSplit(predicate, ':', absl::SkipWhitespace());
if (split_predicate.size() != 2) {
throw MalformedArgvException(
fmt::format("Termination predicate '{}' is badly formatted.", predicate));
}
uint32_t threshold = 0;
if (absl::SimpleAtoi(split_predicate[1], &threshold)) {
predicates[split_predicate[0]] = threshold;
} else {
throw MalformedArgvException(
fmt::format("Termination predicate '{}' has an out of range threshold.", predicate));
}
}
}
OptionsImpl::OptionsImpl(const nighthawk::client::CommandLineOptions& options) {
setNonTrivialDefaults();
requests_per_second_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, requests_per_second, requests_per_second_);
if (options.has_duration()) {
duration_ = options.duration().seconds();
}
if (options.has_timeout()) {
timeout_ = options.timeout().seconds();
}
if (options.has_uri()) {
uri_ = options.uri().value();
} else {
multi_target_path_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options.multi_target(), path, multi_target_path_);
multi_target_use_https_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options.multi_target(), use_https, multi_target_use_https_);
for (const nighthawk::client::MultiTarget::Endpoint& endpoint :
options.multi_target().endpoints()) {
multi_target_endpoints_.push_back(endpoint);
}
}
h2_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, h2, h2_);
concurrency_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, concurrency, concurrency_);
verbosity_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, verbosity, verbosity_);
output_format_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, output_format, output_format_);
prefetch_connections_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, prefetch_connections, prefetch_connections_);
burst_size_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, burst_size, burst_size_);
address_family_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, address_family, address_family_);
if (options.has_request_options()) {
const auto& request_options = options.request_options();
for (const auto& header : request_options.request_headers()) {
std::string header_string =
fmt::format("{}:{}", header.header().key(), header.header().value());
request_headers_.push_back(header_string);
}
if (request_options.request_method() !=
::envoy::config::core::v3::RequestMethod::METHOD_UNSPECIFIED) {
request_method_ = request_options.request_method();
}
request_body_size_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(request_options, request_body_size, request_body_size_);
} else if (options.has_request_source()) {
const auto& request_source_options = options.request_source();
request_source_ = request_source_options.uri();
}
max_pending_requests_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, max_pending_requests, max_pending_requests_);
max_active_requests_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, max_active_requests, max_active_requests_);
max_requests_per_connection_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(
options, max_requests_per_connection, max_requests_per_connection_);
connections_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, connections, connections_);
sequencer_idle_strategy_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, sequencer_idle_strategy, sequencer_idle_strategy_);
trace_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, trace, trace_);
experimental_h1_connection_reuse_strategy_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, experimental_h1_connection_reuse_strategy,
experimental_h1_connection_reuse_strategy_);
open_loop_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, open_loop, open_loop_);
tls_context_.MergeFrom(options.tls_context());
if (options.has_transport_socket()) {
transport_socket_.emplace(envoy::config::core::v3::TransportSocket());
transport_socket_.value().MergeFrom(options.transport_socket());
}
if (options.failure_predicates().size()) {
failure_predicates_.clear();
}
for (const auto& predicate : options.failure_predicates()) {
failure_predicates_[predicate.first] = predicate.second;
}
for (const auto& predicate : options.termination_predicates()) {
termination_predicates_[predicate.first] = predicate.second;
}
if (options.has_jitter_uniform()) {
jitter_uniform_ = std::chrono::nanoseconds(
Envoy::Protobuf::util::TimeUtil::DurationToNanoseconds(options.jitter_uniform()));
}
nighthawk_service_ =
PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, nighthawk_service, nighthawk_service_);
h2_use_multiple_connections_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(
options, experimental_h2_use_multiple_connections, h2_use_multiple_connections_);
simple_warmup_ = PROTOBUF_GET_WRAPPED_OR_DEFAULT(options, simple_warmup, simple_warmup_);
std::copy(options.labels().begin(), options.labels().end(), std::back_inserter(labels_));
validate();
}
void OptionsImpl::setNonTrivialDefaults() {
concurrency_ = "1";
// By default, we don't tolerate error status codes and connection failures, and will report
// upon observing those.
failure_predicates_["benchmark.http_4xx"] = 0;
failure_predicates_["benchmark.http_5xx"] = 0;
failure_predicates_["benchmark.pool_connection_failure"] = 0;
// Also, fail fast when a remote request source is specified that we can't connect to or otherwise
// fails.
failure_predicates_["requestsource.upstream_rq_5xx"] = 0;
jitter_uniform_ = std::chrono::nanoseconds(0);
}
void OptionsImpl::validate() const {
// concurrency must be either 'auto' or a positive integer.
if (concurrency_ != "auto") {
int parsed_concurrency;
try {
parsed_concurrency = std::stoi(concurrency_);
} catch (const std::invalid_argument& ia) {
throw MalformedArgvException("Invalid value for --concurrency");
} catch (const std::out_of_range& oor) {
throw MalformedArgvException("Value out of range: --concurrency");
}
if (parsed_concurrency <= 0) {
throw MalformedArgvException("Value for --concurrency should be greater then 0.");
}
}
if (request_source_ != "") {
try {
UriImpl uri(request_source_, "grpc");
if (uri.scheme() != "grpc") {
throw MalformedArgvException("Invalid replay source URI");
}
} catch (const UriException&) {
throw MalformedArgvException("Invalid replay source URI");
}
}
if (uri_.has_value()) {
try {
UriImpl uri(uri_.value());
} catch (const UriException&) {
throw MalformedArgvException(fmt::format("Invalid target URI: ''", uri_.value()));
}
if (!multi_target_endpoints_.empty() || !multi_target_path_.empty() ||
multi_target_use_https_) {
throw MalformedArgvException("URI and --multi-target-* options cannot both be specified.");
}
} else {
if (multi_target_endpoints_.empty()) {
throw MalformedArgvException("A URI or --multi-target-* options must be specified.");
}
if (multi_target_path_.empty()) {
throw MalformedArgvException("--multi-target-path must be specified.");
}
}
try {
Envoy::MessageUtil::validate(*toCommandLineOptionsInternal(),
Envoy::ProtobufMessage::getStrictValidationVisitor());
} catch (const Envoy::ProtoValidationException& e) {
throw MalformedArgvException(e.what());
}
}
CommandLineOptionsPtr OptionsImpl::toCommandLineOptions() const {
return toCommandLineOptionsInternal();
}
CommandLineOptionsPtr OptionsImpl::toCommandLineOptionsInternal() const {
CommandLineOptionsPtr command_line_options =
std::make_unique<nighthawk::client::CommandLineOptions>();
command_line_options->mutable_connections()->set_value(connections_);
command_line_options->mutable_duration()->set_seconds(duration_);
command_line_options->mutable_requests_per_second()->set_value(requests_per_second_);
command_line_options->mutable_timeout()->set_seconds(timeout_);
command_line_options->mutable_h2()->set_value(h2_);
if (uri_.has_value()) {
command_line_options->mutable_uri()->set_value(uri_.value());
} else {
nighthawk::client::MultiTarget* multi_target = command_line_options->mutable_multi_target();
multi_target->mutable_path()->set_value(multi_target_path_);
multi_target->mutable_use_https()->set_value(multi_target_use_https_);
for (const nighthawk::client::MultiTarget::Endpoint& endpoint : multi_target_endpoints_) {
nighthawk::client::MultiTarget::Endpoint* proto_endpoint = multi_target->add_endpoints();
proto_endpoint->mutable_address()->set_value(endpoint.address().value());
proto_endpoint->mutable_port()->set_value(endpoint.port().value());
}
}
command_line_options->mutable_concurrency()->set_value(concurrency_);
command_line_options->mutable_verbosity()->set_value(verbosity_);
command_line_options->mutable_output_format()->set_value(output_format_);
command_line_options->mutable_prefetch_connections()->set_value(prefetch_connections_);
command_line_options->mutable_burst_size()->set_value(burst_size_);
command_line_options->mutable_address_family()->set_value(
static_cast<nighthawk::client::AddressFamily_AddressFamilyOptions>(address_family_));
if (requestSource() != "") {
auto request_source = command_line_options->mutable_request_source();
*request_source->mutable_uri() = request_source_;
} else {
auto request_options = command_line_options->mutable_request_options();
request_options->set_request_method(request_method_);
for (const auto& header : request_headers_) {
auto header_value_option = request_options->add_request_headers();
// TODO(oschaaf): expose append option in CLI? For now we just set.
header_value_option->mutable_append()->set_value(false);
auto request_header = header_value_option->mutable_header();
// Skip past the first colon so we propagate ':authority: foo` correctly.
auto pos = header.empty() ? std::string::npos : header.find(':', 1);
if (pos != std::string::npos) {
request_header->set_key(std::string(absl::StripAsciiWhitespace(header.substr(0, pos))));
// Any visible char, including ':', is allowed in header values.
request_header->set_value(std::string(absl::StripAsciiWhitespace(header.substr(pos + 1))));
} else {
throw MalformedArgvException("A ':' is required in a header.");
}
request_options->mutable_request_body_size()->set_value(requestBodySize());
}
}
// Only set the tls context if needed, to avoid a warning being logged about field deprecation.
// Ideally this would follow the way transport_socket uses absl::optional below.
// But as this field is about to get eliminated this minimal effort shortcut may be more suitable.
if (!Envoy::Protobuf::util::MessageDifferencer::Equivalent(
tls_context_, envoy::extensions::transport_sockets::tls::v3::UpstreamTlsContext())) {
*(command_line_options->mutable_tls_context()) = tls_context_;
}
if (transport_socket_.has_value()) {
*(command_line_options->mutable_transport_socket()) = transport_socket_.value();
}
command_line_options->mutable_max_pending_requests()->set_value(max_pending_requests_);
command_line_options->mutable_max_active_requests()->set_value(max_active_requests_);
command_line_options->mutable_max_requests_per_connection()->set_value(
max_requests_per_connection_);
command_line_options->mutable_sequencer_idle_strategy()->set_value(sequencer_idle_strategy_);
command_line_options->mutable_trace()->set_value(trace_);
command_line_options->mutable_experimental_h1_connection_reuse_strategy()->set_value(
experimental_h1_connection_reuse_strategy_);
auto termination_predicates_option = command_line_options->mutable_termination_predicates();
for (const auto& predicate : termination_predicates_) {
termination_predicates_option->insert({predicate.first, predicate.second});
}
auto failure_predicates_option = command_line_options->mutable_failure_predicates();
for (const auto& predicate : failure_predicates_) {
failure_predicates_option->insert({predicate.first, predicate.second});
}
command_line_options->mutable_open_loop()->set_value(open_loop_);
if (jitter_uniform_.count() > 0) {
*command_line_options->mutable_jitter_uniform() =
Envoy::Protobuf::util::TimeUtil::NanosecondsToDuration(jitter_uniform_.count());
}
command_line_options->mutable_nighthawk_service()->set_value(nighthawk_service_);
command_line_options->mutable_experimental_h2_use_multiple_connections()->set_value(
h2_use_multiple_connections_);
for (const auto& label : labels_) {
*command_line_options->add_labels() = label;
}
command_line_options->mutable_simple_warmup()->set_value(simple_warmup_);
return command_line_options;
}
} // namespace Client
} // namespace Nighthawk