-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh2load.cc
1411 lines (1285 loc) · 49.6 KB
/
h2load.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2014 Tatsuhiro Tsujikawa
*
* 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.
*/
#include <fstream>
#include <streambuf>
#include "h2load.h"
#include <getopt.h>
#include <signal.h>
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif // HAVE_NETINET_IN_H
#include <sys/stat.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif // HAVE_FCNTL_H
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif // HAVE_SYS_SOCKET_H
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif // HAVE_NETDB_H
#ifndef _WINDOWS
#include <sys/un.h>
#endif
#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
#include <future>
#include <random>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <openssl/err.h>
#include <openssl/ssl.h>
#ifdef USE_LIBEV
extern "C" {
#include <ares.h>
}
#endif
#include "nghttp2_config.h"
#include <nghttp2/nghttp2.h>
#include "template.h"
#include "url-parser/url_parser.h"
#include "h2load_http1_session.h"
#include "h2load_http2_session.h"
#include "tls.h"
#include "http2.h"
#include "util.h"
#include "template.h"
#include "h2load_utils.h"
#include "h2load_Config.h"
#ifdef USE_LIBEV
#include "libev_client.h"
#endif
#include "base_worker.h"
#include "h2load_stats.h"
#include "staticjson/document.hpp"
#include "staticjson/staticjson.hpp"
#include "rapidjson/schema.h"
#include "rapidjson/prettywriter.h"
#include "config_schema.h"
#include "h2load_lua.h"
#ifndef O_BINARY
# define O_BINARY (0)
#endif // O_BINARY
using namespace nghttp2;
namespace h2load
{
Config config;
namespace
{
constexpr size_t MAX_SAMPLES = 100000;
constexpr size_t MAX_SAMPLES_PER_THREAD = 10000;
} // namespace
Stats::Stats(size_t req_todo, size_t nclients)
: req_todo(req_todo),
req_started(0),
req_done(0),
req_success(0),
req_status_success(0),
req_failed(0),
req_error(0),
req_timedout(0),
bytes_total(0),
bytes_head(0),
bytes_head_decomp(0),
bytes_body(0),
status()
{}
Stream::Stream(size_t scenario_id, size_t request_id, bool stats_eligible)
: req_stat(scenario_id, request_id),
status_success(-1),
statistics_eligible(stats_eligible) {}
namespace
{
void print_version(std::ostream& out)
{
out << "h2load nghttp2/" NGHTTP2_VERSION << std::endl;
}
} // namespace
namespace
{
void print_usage(std::ostream& out)
{
out << R"(Usage: h2loadrunner [OPTIONS]... [URI]...
benchmarking tool for HTTP1.x / HTTP/2 server)"
<< std::endl;
}
} // namespace
namespace
{
constexpr char DEFAULT_NPN_LIST[] = "h2,h2-16,h2-14,http/1.1";
} // namespace
namespace
{
constexpr char UNIX_PATH_PREFIX[] = "unix:";
} // namespace
namespace
{
void print_help(std::ostream& out)
{
print_usage(out);
Config config;
out << R"(
<URI> Specify URI to access. Multiple URIs can be specified.
URIs are used in this order for each client. All URIs
are used, then first URI is used and then 2nd URI, and
so on. The scheme, host and port in the subsequent
URIs, if present, are ignored. Those in the first URI
are used solely. Definition of a base URI overrides all
scheme, host or port values.
Options:
-n, --requests=<N>
Number of requests across all clients. If it is used
with --timing-script-file option, this option specifies
the number of requests each client performs rather than
the number of requests across all clients. This option
is ignored if timing-based benchmarking is enabled (see
--duration option).
Default: )"
<< config.nreqs << R"(
-c, --clients=<N>
Number of concurrent clients. With -r option, this
specifies the maximum number of connections to be made.
Default: )"
<< config.nclients << R"(
-t, --threads=<N>
Number of native threads.
Default: )"
<< config.nthreads << R"(
-i, --input-file=<PATH>
Path of a file with multiple URIs separated by EOLs.
This option will disable URIs getting from command-line.
If '-' is given as <PATH>, URIs will be read from stdin.
URIs are used in this order for each client. All URIs
are used, then first URI is used and then 2nd URI, and
so on. The scheme, host and port in the subsequent
URIs, if present, are ignored. Those in the first URI
are used solely. Definition of a base URI overrides all
scheme, host or port values.
-m, --max-concurrent-streams=<N>
Max concurrent streams to issue per session. Not used
for http/1.1
Default: 1
-w, --window-bits=<N>
Sets the stream level initial window size to (2**<N>)-1.
Default: )"
<< config.window_bits << R"(
-W, --connection-window-bits=<N>
Sets the connection level initial window size to
(2**<N>)-1.
Default: )"
<< config.connection_window_bits << R"(
-H, --header=<HEADER>
Add/Override a header to the requests.
--ciphers=<SUITE>
Set allowed cipher list. The format of the string is
described in OpenSSL ciphers(1).
Default: )"
<< config.ciphers << R"(
-p, --no-tls-proto=<PROTOID>
Specify ALPN identifier of the protocol to be used when
accessing http URI without SSL/TLS.
Available protocols: )"
<< NGHTTP2_CLEARTEXT_PROTO_VERSION_ID << R"( and )" << NGHTTP2_H1_1 << R"(
Default: )"
<< NGHTTP2_CLEARTEXT_PROTO_VERSION_ID << R"(
-d, --data=<PATH>
Post FILE to server. The request method is changed to
POST. For http/1.1 connection, if -d is used, the
maximum number of in-flight pipelined requests is set to
1.
-r, --rate=<N>
Specifies the fixed rate at which connections are
created. The rate must be a positive integer,
representing the number of connections to be made per
rate period. The maximum number of connections to be
made is given in -c option. This rate will be
distributed among threads as evenly as possible. For
example, with -t2 and -r4, each thread gets 2
connections per period. When the rate is 0, the program
will run as it normally does, creating connections at
whatever variable rate it wants. The default value for
this option is 0.
--rate-period=<DURATION>
Specifies the time period between creating connections.
The period must be a positive number, representing the
length of the period in time. This option is ignored if
the rate option is not used. The default value for this
option is 1s.
-D, --duration=<DURATION>
Specifies the main duration for the measurements in case
of timing-based benchmarking.
--warm-up-time=<DURATION>
Specifies the time period before starting the actual
measurements, in case of timing-based benchmarking.
Needs to provided along with -D option.
-T, --connection-active-timeout=<DURATION>
Specifies the maximum time that h2load is willing to
keep a connection open, regardless of the activity on
said connection. <DURATION> must be a positive integer,
specifying the amount of time to wait. When no timeout
value is set (either active or inactive), h2load will
keep a connection open indefinitely, waiting for a
response.
-N, --connection-inactivity-timeout=<DURATION>
Specifies the amount of time that h2load is willing to
wait to see activity on a given connection. <DURATION>
must be a positive integer, specifying the amount of
time to wait. When no timeout value is set (either
active or inactive), h2load will keep a connection open
indefinitely, waiting for a response.
--timing-script-file=<PATH>
Path of a file containing one or more lines separated by
EOLs. Each script line is composed of two tab-separated
fields. The first field represents the time offset from
the start of execution, expressed as a positive value of
milliseconds with microsecond resolution. The second
field represents the URI. This option will disable URIs
getting from command-line. If '-' is given as <PATH>,
script lines will be read from stdin. Script lines are
used in order for each client. If -n is given, it must
be less than or equal to the number of script lines,
larger values are clamped to the number of script lines.
If -n is not given, the number of requests will default
to the number of script lines. The scheme, host and
port defined in the first URI are used solely. Values
contained in other URIs, if present, are ignored.
Definition of a base URI overrides all scheme, host or
port values. --timing-script-file and --rps are
mutually exclusive.
-B, --base-uri=(<URI>|unix:<PATH>)
Specify URI from which the scheme, host and port will be
used for all requests. The base URI overrides all
values defined either at the command line or inside
input files. If argument starts with "unix:", then the
rest of the argument will be treated as UNIX domain
socket path. The connection is made through that path
instead of TCP. In this case, scheme is inferred from
the first URI appeared in the command line or inside
input files as usual.
--npn-list=<LIST>
Comma delimited list of ALPN protocol identifier sorted
in the order of preference. That means most desirable
protocol comes first. This is used in both ALPN and
NPN. The parameter must be delimited by a single comma
only and any white spaces are treated as a part of
protocol string.
Default: )"
<< DEFAULT_NPN_LIST << R"(
--h1 Short hand for --npn-list=http/1.1
--no-tls-proto=http/1.1, which effectively force
http/1.1 for both http and https URI.
--header-table-size=<SIZE>
Specify decoder header table size.
Default: )"
<< util::utos_unit(config.header_table_size) << R"(
--encoder-header-table-size=<SIZE>
Specify encoder header table size. The decoder (server)
specifies the maximum dynamic table size it accepts.
Then the negotiated dynamic table size is the minimum of
this option value and the value which server specified.
Default: )"
<< util::utos_unit(config.encoder_header_table_size) << R"(
--log-file=<PATH>
Write per-request information to a file as tab-separated
columns: start time as microseconds since epoch; HTTP
status code; microseconds until end of response. More
columns may be added later. Rows are ordered by end-of-
response time when using one worker thread, but may
appear slightly out of order with multiple threads due
to buffering. Status code is -1 for failed streams.
--connect-to=<HOST>[:<PORT>]
Host and port to connect instead of using the authority
in <URI>.
--rps=<N> Specify request per second for each client. --rps and
--timing-script-file are mutually exclusive.
--stream-timeout-interval-ms=<timeout value in ms>
request time out value. After timeout, RST_STREAM is
sent by h2load. Default 5000.
--rps-input-file=<PATH>
A file specifying rps number. It is useful when dynamic
change of rps is needed.
--config-file=<PATH>
A JSON file specifying the configurations needed.
--script=<PATH>
A Lua script file to load and run. Configuration related
to host, Scenarioes in above config-file will be ignored
And the actual connection and request will be controlled
by the script.
Multiple scripts are acceptable w/ multiple --script arg
-v, --verbose
Output debug information.
--version Display version information and exit.
-h, --help Display this help and exit.
--
The <SIZE> argument is an integer and an optional unit (e.g., 10K is
10 * 1024). Units are K, M and G (powers of 1024).
The <DURATION> argument is an integer and an optional unit (e.g., 1s
is 1 second and 500ms is 500 milliseconds). Units are h, m, s or ms
(hours, minutes, seconds and milliseconds, respectively). If a unit
is omitted, a second is used as unit.)"
<< std::endl;
}
} // namespace
int main(int argc, char** argv)
{
tls::libssl_init();
#ifdef USE_LIBEV
auto status = ares_library_init(ARES_LIB_INIT_ALL);
if (status != ARES_SUCCESS)
{
std::cerr << "ares_library_init failed" << std::endl;
exit(EXIT_FAILURE);
return 1;
}
#endif
#ifndef NOTHREADS
tls::LibsslGlobalLock lock;
#endif // NOTHREADS
std::string datafile;
std::vector<std::string> script_files;
bool nreqs_set_manually = false;
while (1)
{
static int flag = 0;
constexpr static option long_options[] =
{
{"requests", required_argument, nullptr, 'n'},
{"clients", required_argument, nullptr, 'c'},
{"data", required_argument, nullptr, 'd'},
{"threads", required_argument, nullptr, 't'},
{"max-concurrent-streams", required_argument, nullptr, 'm'},
{"window-bits", required_argument, nullptr, 'w'},
{"connection-window-bits", required_argument, nullptr, 'W'},
{"input-file", required_argument, nullptr, 'i'},
{"header", required_argument, nullptr, 'H'},
{"no-tls-proto", required_argument, nullptr, 'p'},
{"verbose", no_argument, nullptr, 'v'},
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, &flag, 1},
{"ciphers", required_argument, &flag, 2},
{"rate", required_argument, nullptr, 'r'},
{"connection-active-timeout", required_argument, nullptr, 'T'},
{"connection-inactivity-timeout", required_argument, nullptr, 'N'},
{"duration", required_argument, nullptr, 'D'},
{"timing-script-file", required_argument, &flag, 3},
{"base-uri", required_argument, nullptr, 'B'},
{"npn-list", required_argument, &flag, 4},
{"rate-period", required_argument, &flag, 5},
{"h1", no_argument, &flag, 6},
{"header-table-size", required_argument, &flag, 7},
{"encoder-header-table-size", required_argument, &flag, 8},
{"warm-up-time", required_argument, &flag, 9},
{"log-file", required_argument, &flag, 10},
{"connect-to", required_argument, &flag, 11},
{"rps", required_argument, &flag, 12},
{"stream-timeout-interval-ms", required_argument, &flag, 23},
{"rps-input-file", required_argument, &flag, 24},
{"config-file", required_argument, &flag, 25},
{"script", required_argument, &flag, 26},
{nullptr, 0, nullptr, 0}
};
int option_index = 0;
auto c = getopt_long(argc, argv,
"hvW:c:d:m:n:p:t:w:H:i:r:T:N:D:B:", long_options,
&option_index);
if (c == -1)
{
break;
}
switch (c)
{
case 'n':
config.nreqs = strtoul(optarg, nullptr, 10);
nreqs_set_manually = true;
break;
case 'c':
config.nclients = strtoul(optarg, nullptr, 10);
break;
case 'd':
datafile = optarg;
break;
case 't':
#ifdef NOTHREADS
std::cerr << "-t: WARNING: Threading disabled at build time, "
<< "no threads created." << std::endl;
#else
config.nthreads = strtoul(optarg, nullptr, 10);
#endif // NOTHREADS
break;
case 'm':
config.max_concurrent_streams = strtoul(optarg, nullptr, 10);
break;
case 'w':
case 'W':
{
errno = 0;
char* endptr = nullptr;
auto n = strtoul(optarg, &endptr, 10);
if (errno == 0 && *endptr == '\0' && n < 31)
{
if (c == 'w')
{
config.window_bits = n;
}
else
{
config.connection_window_bits = n;
}
}
else
{
std::cerr << "-" << static_cast<char>(c)
<< ": specify the integer in the range [0, 30], inclusive"
<< std::endl;
exit(EXIT_FAILURE);
}
break;
}
case 'H':
{
char* header = optarg;
// Skip first possible ':' in the header name
char* value = strchr(optarg + 1, ':');
if (!value || (header[0] == ':' && header + 1 == value))
{
std::cerr << "-H: invalid header: " << optarg << std::endl;
exit(EXIT_FAILURE);
}
*value = 0;
value++;
//while (isspace(*value))
//{
// value++;
//}
if (*value == 0)
{
// This could also be a valid case for suppressing a header
// similar to curl
std::cerr << "-H: invalid header - value missing: " << optarg
<< std::endl;
exit(EXIT_FAILURE);
}
// Note that there is no processing currently to handle multiple
// message-header fields with the same field name
config.custom_headers.emplace_back(header, value);
util::inp_strlower(config.custom_headers.back().name);
break;
}
case 'i':
config.ifile = optarg;
break;
case 'p':
{
auto proto = StringRef {optarg};
if (util::strieq(StringRef::from_lit(NGHTTP2_CLEARTEXT_PROTO_VERSION_ID),
proto))
{
config.no_tls_proto = Config::PROTO_HTTP2;
}
else if (util::strieq(NGHTTP2_H1_1, proto))
{
config.no_tls_proto = Config::PROTO_HTTP1_1;
}
else
{
std::cerr << "-p: unsupported protocol " << proto << std::endl;
exit(EXIT_FAILURE);
}
break;
}
case 'r':
config.rate = strtoul(optarg, nullptr, 10);
if (config.rate == 0)
{
std::cerr << "-r: the rate at which connections are made "
<< "must be positive." << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'T':
config.conn_active_timeout = util::parse_duration_with_unit(optarg);
if (!std::isfinite(config.conn_active_timeout))
{
std::cerr << "-T: bad value for the conn_active_timeout wait time: "
<< optarg << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'N':
config.conn_inactivity_timeout = util::parse_duration_with_unit(optarg);
if (!std::isfinite(config.conn_inactivity_timeout))
{
std::cerr << "-N: bad value for the conn_inactivity_timeout wait time: "
<< optarg << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'B':
{
auto arg = StringRef {optarg};
config.base_uri = "";
#ifndef _WINDOWS
config.base_uri_unix = false;
if (util::istarts_with_l(arg, UNIX_PATH_PREFIX))
{
// UNIX domain socket path
sockaddr_un un;
auto path = StringRef {std::begin(arg) + str_size(UNIX_PATH_PREFIX),
std::end(arg)
};
if (path.size() == 0 || path.size() + 1 > sizeof(un.sun_path))
{
std::cerr << "--base-uri: invalid UNIX domain socket path: " << arg
<< std::endl;
exit(EXIT_FAILURE);
}
config.base_uri_unix = true;
auto& unix_addr = config.unix_addr;
std::copy(std::begin(path), std::end(path), unix_addr.sun_path);
unix_addr.sun_path[path.size()] = '\0';
unix_addr.sun_family = AF_UNIX;
break;
}
#endif
if (!parse_base_uri(arg, config))
{
std::cerr << "--base-uri: invalid base URI: " << arg << std::endl;
exit(EXIT_FAILURE);
}
config.base_uri = arg.str();
break;
}
case 'D':
config.duration = util::parse_duration_with_unit(optarg);
if (!std::isfinite(config.duration))
{
std::cerr << "-D: value error " << optarg << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'v':
config.verbose = true;
break;
case 'h':
print_help(std::cerr);
exit(EXIT_SUCCESS);
case '?':
util::show_candidates(argv[optind - 1], long_options);
exit(EXIT_FAILURE);
case 0:
switch (flag)
{
case 1:
// version option
print_version(std::cerr);
exit(EXIT_SUCCESS);
case 2:
// ciphers option
config.ciphers = optarg;
break;
case 3:
// timing-script option
config.ifile = optarg;
config.timing_script = true;
break;
case 4:
// npn-list option
config.npn_list = util::parse_config_str_list(StringRef {optarg});
break;
case 5:
// rate-period
config.rate_period = util::parse_duration_with_unit(optarg);
if (!std::isfinite(config.rate_period))
{
std::cerr << "--rate-period: value error " << optarg << std::endl;
exit(EXIT_FAILURE);
}
break;
case 6:
// --h1
config.npn_list =
util::parse_config_str_list(StringRef::from_lit("http/1.1"));
config.no_tls_proto = Config::PROTO_HTTP1_1;
break;
case 7:
// --header-table-size
if (parse_header_table_size(config.header_table_size,
"header-table-size", optarg) != 0)
{
exit(EXIT_FAILURE);
}
break;
case 8:
// --encoder-header-table-size
if (parse_header_table_size(config.encoder_header_table_size,
"encoder-header-table-size", optarg) != 0)
{
exit(EXIT_FAILURE);
}
break;
case 9:
// --warm-up-time
config.warm_up_time = util::parse_duration_with_unit(optarg);
if (!std::isfinite(config.warm_up_time))
{
std::cerr << "--warm-up-time: value error " << optarg << std::endl;
exit(EXIT_FAILURE);
}
break;
case 10:
// --log-file
config.json_config_schema.log_file = optarg;
break;
case 11:
{
// --connect-to
auto p = util::split_hostport(StringRef {optarg});
int64_t port = 0;
if (p.first.empty() ||
(!p.second.empty() && (port = util::parse_uint(p.second)) == -1))
{
std::cerr << "--connect-to: Invalid value " << optarg << std::endl;
exit(EXIT_FAILURE);
}
config.connect_to_host = p.first.str();
config.connect_to_port = port;
break;
}
case 12:
{
char* end;
auto v = std::strtod(optarg, &end);
if (end == optarg || *end != '\0' || !std::isfinite(v) ||
1. / v < 1e-6)
{
std::cerr << "--rps: Invalid value " << optarg << std::endl;
exit(EXIT_FAILURE);
}
config.rps = v;
break;
}
case 23:
{
config.stream_timeout_in_ms = (uint16_t)strtoul(optarg, nullptr, 10);
}
break;
case 24:
{
config.rps_file = optarg;
}
break;
case 25:
{
std::string config_file_name = optarg;
std::ifstream buffer(config_file_name);
std::string jsonStr((std::istreambuf_iterator<char>(buffer)),
std::istreambuf_iterator<char>());
staticjson::ParseStatus result;
if (!staticjson::from_json_string(jsonStr.c_str(), &config.json_config_schema, &result))
{
std::cerr << "error reading config file:" << result.description() << std::endl;
exit(EXIT_FAILURE);
}
post_process_json_config_schema(config);
populate_config_from_json(config);
}
break;
case 26:
{
std::string script_file = optarg;
script_files.push_back(script_file);
}
break;
}
break;
default:
break;
}
}
if (script_files.size())
{
std::vector<std::string> lua_scripts;
for (auto& script_file : script_files)
{
std::ifstream buffer(script_file);
if (!buffer.is_open())
{
std::cerr << "file open error: " << script_file << std::endl;
}
std::string lua_script((std::istreambuf_iterator<char>(buffer)),
std::istreambuf_iterator<char>());
lua_scripts.push_back(lua_script);
}
load_and_run_lua_script(lua_scripts, config);
return 0;
}
if (argc == optind)
{
if (config.ifile.empty() && (config.host.empty() || config.scheme.empty()))
{
std::cerr << "no URI or input file given" << std::endl;
exit(EXIT_FAILURE);
}
}
if (config.nclients == 0)
{
std::cerr << "-c: the number of clients must be strictly greater than 0."
<< std::endl;
exit(EXIT_FAILURE);
}
if (config.npn_list.empty())
{
config.npn_list =
util::parse_config_str_list(StringRef::from_lit(DEFAULT_NPN_LIST));
}
// serialize the APLN tokens
for (auto& proto : config.npn_list)
{
proto.insert(proto.begin(), static_cast<unsigned char>(proto.size()));
}
if (config.ifile.empty())
{
std::vector<std::string> uris;
std::copy(&argv[optind], &argv[argc], std::back_inserter(uris));
if (uris.empty() && config.host.size() && config.scheme.size())
{
// no exit
}
else
{
config.reqlines = parse_uris(std::begin(uris), std::end(uris), config);
}
}
else
{
std::vector<std::string> uris;
if (!config.timing_script)
{
if (config.ifile == "-")
{
uris = read_uri_from_file(std::cin);
}
else
{
std::ifstream infile(config.ifile);
if (!infile)
{
std::cerr << "cannot read input file: " << config.ifile << std::endl;
exit(EXIT_FAILURE);
}
uris = read_uri_from_file(infile);
}
}
else
{
if (config.ifile == "-")
{
read_script_from_file(std::cin, config.timings, uris);
}
else
{
std::ifstream infile(config.ifile);
if (!infile)
{
std::cerr << "cannot read input file: " << config.ifile << std::endl;
exit(EXIT_FAILURE);
}
read_script_from_file(infile, config.timings, uris);
}
if (nreqs_set_manually)
{
if (config.nreqs > uris.size())
{
std::cerr << "-n: the number of requests must be less than or equal "
"to the number of timing script entries. Setting number "
"of requests to "
<< uris.size() << std::endl;
config.nreqs = uris.size();
}
}
else
{
config.nreqs = uris.size();
}
}
config.reqlines = parse_uris(std::begin(uris), std::end(uris), config);
}
if (config.reqlines.empty() && (config.host.empty() || config.scheme.empty()))
{
std::cerr << "No URI given" << std::endl;
exit(EXIT_FAILURE);
}
//if (config.is_timing_based_mode() && config.is_rate_mode())
//{
// std::cerr << "-r, -D: they are mutually exclusive." << std::endl;
// exit(EXIT_FAILURE);
//}
if (config.timing_script && config.rps_enabled())
{
std::cerr << "--timing-script-file, --rps: they are mutually exclusive."
<< std::endl;
exit(EXIT_FAILURE);
}
if (config.nreqs == 0 && !config.is_timing_based_mode())
{
std::cerr << "-n: the number of requests must be strictly greater than 0 "
"if timing-based test is not being run."
<< std::endl;
exit(EXIT_FAILURE);
}
if (config.max_concurrent_streams == 0)
{
std::cerr << "-m: the max concurrent streams must be strictly greater "
<< "than 0." << std::endl;
exit(EXIT_FAILURE);
}
if (config.nthreads == 0)
{
std::cerr << "-t: the number of threads must be strictly greater than 0."
<< std::endl;
exit(EXIT_FAILURE);
}
if (config.nthreads > std::thread::hardware_concurrency())
{
std::cerr << "-t: warning: the number of threads is greater than hardware "
<< "cores." << std::endl;
}
// With timing script, we don't distribute config.nreqs to each
// client or thread.
if (!config.timing_script && config.nreqs < config.nclients &&
!config.is_timing_based_mode())
{
std::cerr << "-n, -c: the number of requests must be greater than or "
<< "equal to the clients." << std::endl;
exit(EXIT_FAILURE);
}
if (config.nclients < config.nthreads)
{
std::cerr << "-c, -t: the number of clients must be greater than or equal "
<< "to the number of threads." << std::endl;
exit(EXIT_FAILURE);
}
if (config.is_timing_based_mode())
{
config.nreqs = 0;
}
if (config.is_rate_mode())
{
if (config.rate < config.nthreads)
{
std::cerr << "-r, -t: the connection rate must be greater than or equal "
<< "to the number of threads." << std::endl;
exit(EXIT_FAILURE);
}
if (config.rate > config.nclients)
{
std::cerr << "-r, -c: the connection rate must be smaller than or equal "
"to the number of clients."
<< std::endl;
exit(EXIT_FAILURE);
}
}
if (!datafile.empty())
{
std::ifstream f(datafile);
if (f.good())
{
std::string content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
config.payload_data = content;
config.data_length = content.size();
}
else
{
std::cerr << "-d: Could not open file " << datafile << std::endl;
exit(EXIT_FAILURE);
}
}
#ifndef _WINDOWS
struct sigaction act {};
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, nullptr);
#endif
auto ssl_ctx = SSL_CTX_new(SSLv23_client_method());