-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathh2load_utils.cc
More file actions
2041 lines (1840 loc) · 76.4 KB
/
h2load_utils.cc
File metadata and controls
2041 lines (1840 loc) · 76.4 KB
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
#include <algorithm>
#include <numeric>
#include <cctype>
#include <mutex>
#ifndef _WINDOWS
#include <execinfo.h>
#endif
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <boost/algorithm/string.hpp>
#ifdef USE_LIBEV
extern "C" {
#include <ares.h>
}
#include "libev_client.h"
#endif
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <fstream>
#include <fcntl.h>
#include "template.h"
#include "util.h"
#include "config_schema.h"
#include "tls.h"
#include <nghttp2/asio_httpx_server.h>
#include "h2load_utils.h"
#include "base_client.h"
#include "asio_worker.h"
using namespace h2load;
std::shared_ptr<h2load::base_worker> create_worker(uint32_t id, SSL_CTX* ssl_ctx,
size_t nreqs, size_t nclients,
size_t rate, size_t max_samples, h2load::Config& config)
{
std::stringstream rate_report;
if (config.is_rate_mode() && nclients > rate)
{
rate_report << "Up to " << rate << " client(s) will be created every "
<< util::duration_str(config.rate_period) << " ";
}
if (config.is_timing_based_mode())
{
std::cerr << "spawning thread #" << id << ": " << nclients
<< " total client(s). Timing-based test with "
<< config.warm_up_time << "s of warm-up time and "
<< config.duration << "s of main duration for measurements."
<< std::endl;
}
else
{
std::cerr << "spawning thread #" << id << ": " << nclients
<< " total client(s). " << rate_report.str() << nreqs
<< " total requests" << std::endl;
}
#ifndef USE_LIBEV
if (config.is_rate_mode())
{
return std::make_shared<asio_worker>(id, nreqs, nclients, rate,
max_samples, &config);
}
else
{
// Here rate is same as client because the rate_timeout callback
// will be called only once
return std::make_shared<asio_worker>(id, nreqs, nclients, nclients,
max_samples, &config);
}
#else
if (config.is_rate_mode())
{
return std::make_shared<libev_worker>(id, ssl_ctx, nreqs, nclients, rate,
max_samples, &config);
}
else
{
// Here rate is same as client because the rate_timeout callback
// will be called only once
return std::make_shared<libev_worker>(id, ssl_ctx, nreqs, nclients, nclients,
max_samples, &config);
}
#endif
}
int parse_header_table_size(uint32_t& dst, const char* opt,
const char* optarg)
{
auto n = util::parse_uint_with_unit(optarg);
if (n == -1)
{
std::cerr << "--" << opt << ": Bad option value: " << optarg << std::endl;
return -1;
}
if (n > std::numeric_limits<uint32_t>::max())
{
std::cerr << "--" << opt
<< ": Value too large. It should be less than or equal to "
<< std::numeric_limits<uint32_t>::max() << std::endl;
return -1;
}
dst = (int)n;
return 0;
}
void read_script_from_file(std::istream& infile,
std::vector<double>& timings,
std::vector<std::string>& uris)
{
std::string script_line;
int line_count = 0;
while (std::getline(infile, script_line))
{
line_count++;
if (script_line.empty())
{
std::cerr << "Empty line detected at line " << line_count
<< ". Ignoring and continuing." << std::endl;
continue;
}
std::size_t pos = script_line.find("\t");
if (pos == std::string::npos)
{
std::cerr << "Invalid line format detected, no tab character at line "
<< line_count << ". \n\t" << script_line << std::endl;
exit(EXIT_FAILURE);
}
const char* start = script_line.c_str();
char* end;
auto v = std::strtod(start, &end);
errno = 0;
if (v < 0.0 || !std::isfinite(v) || end == start || errno != 0)
{
auto error = errno;
std::cerr << "Time value error at line " << line_count << ". \n\t"
<< "value = " << script_line.substr(0, pos) << std::endl;
if (error != 0)
{
std::cerr << "\t" << strerror(error) << std::endl;
}
exit(EXIT_FAILURE);
}
timings.push_back(v / 1000.0);
uris.push_back(script_line.substr(pos + 1, script_line.size()));
}
}
void sampling_init(h2load::Sampling& smp, size_t max_samples)
{
smp.n = 0;
smp.max_samples = max_samples;
}
#ifdef USE_LIBEV
void writecb(struct ev_loop* loop, ev_io* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->restart_timeout_timer();
auto rv = client->do_write();
if (rv == libev_client::ERR_CONNECT_FAIL)
{
client->disconnect();
if (client->reconnect_to_alt_addr())
{
return;
}
// Try next address
client->current_addr = nullptr;
rv = client->connect();
if (rv != 0)
{
client->fail();
client->worker->free_client(client);
return;
}
return;
}
if (rv != 0)
{
client->fail();
if (client->reconnect_to_alt_addr())
{
return;
}
client->worker->free_client(client);
}
}
void readcb(struct ev_loop* loop, ev_io* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->restart_timeout_timer();
if (client->do_read() != 0)
{
if (client->try_again_or_fail() == 0)
{
return;
}
if (client->reconnect_to_alt_addr())
{
return;
}
client->worker->free_client(client);
return;
}
writecb(loop, &client->wev, revents);
// client->disconnect() and client->fail() may be called
}
// Called every rate_period when rate mode is being used
void rate_period_timeout_w_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto worker = static_cast<libev_worker*>(w->data);
worker->rate_period_timeout_handler();
}
// Called when the duration for infinite number of requests are over
void duration_timeout_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto worker = static_cast<libev_worker*>(w->data);
worker->duration_timeout_handler();
//ev_break (EV_A_ EVBREAK_ALL);
}
// Called when the warmup duration for infinite number of requests are over
void warmup_timeout_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto worker = static_cast<libev_worker*>(w->data);
worker->warmup_timeout_handler();
}
void rps_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->on_rps_timer();
}
void stream_timeout_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->reset_timeout_requests();
}
void client_connection_timeout_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->call_connected_callbacks(false);
client->connection_timeout_handler();
}
void delayed_request_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->resume_delayed_request_execution();
}
// Called when an a connection has been inactive for a set period of time
// or a fixed amount of time after all requests have been made on a
// connection
void conn_activity_timeout_cb(EV_P_ ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->conn_activity_timeout_handler();
}
void client_request_timeout_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->timing_script_timeout_handler();
}
int get_ev_loop_flags()
{
if (ev_supported_backends() & ~ev_recommended_backends() & EVBACKEND_KQUEUE)
{
return ev_recommended_backends() | EVBACKEND_KQUEUE;
}
return 0;
}
void ping_w_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
client->submit_ping();
}
void ares_addrinfo_query_callback(void* arg, int status, int timeouts, struct ares_addrinfo* res)
{
libev_client* client = static_cast<libev_client*>(arg);
if (status == ARES_SUCCESS)
{
if (client->ares_address)
{
ares_freeaddrinfo(client->ares_address);
}
client->next_addr = nullptr;
client->current_addr = nullptr;
client->ares_address = res;
client->connect();
ares_freeaddrinfo(client->ares_address);
client->ares_address = nullptr;
}
else
{
client->fail();
}
}
void ares_io_cb(struct ev_loop* loop, struct ev_io* watcher, int revents)
{
libev_client* client = static_cast<libev_client*>(watcher->data);
ares_process_fd(client->channel,
revents & EV_READ ? watcher->fd : ARES_SOCKET_BAD,
revents & EV_WRITE ? watcher->fd : ARES_SOCKET_BAD);
}
void reconnect_to_used_host_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
ev_timer_stop(loop, w);
client->reconnect_to_used_host();
}
void ares_addrinfo_query_callback_for_probe(void* arg, int status, int timeouts, struct ares_addrinfo* res)
{
libev_client* client = static_cast<libev_client*>(arg);
if (status == ARES_SUCCESS)
{
client->probe_address(res);
ares_freeaddrinfo(res);
}
}
void connect_to_prefered_host_cb(struct ev_loop* loop, ev_timer* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
if (CLIENT_CONNECTED != client->state)
{
ev_timer_stop(loop, w); // reconnect will connect to preferred host first
}
else if (client->authority == client->preferred_authority && CLIENT_CONNECTED == client->state)
{
ev_timer_stop(loop, w); // already connected to preferred host
}
else // connected, but not to preferred host, so check if preferred host is up for connection
{
client->probe_and_connect_to(client->schema, client->preferred_authority);
}
}
void probe_writecb(struct ev_loop* loop, ev_io* w, int revents)
{
auto client = static_cast<libev_client*>(w->data);
ev_io_stop(loop, w);
if (util::check_socket_connected(client->probe_skt_fd))
{
client->on_prefered_host_up();
}
}
void ares_socket_state_cb(void* data, int s, int read, int write)
{
libev_client* client = static_cast<libev_client*>(data);
auto worker = static_cast<libev_worker*>(client->worker);
if (read != 0 || write != 0)
{
if (client->ares_io_watchers.find(s) == client->ares_io_watchers.end())
{
ev_io watcher;
watcher.data = client;
client->ares_io_watchers[s] = watcher;
ev_init(&client->ares_io_watchers[s], ares_io_cb);
}
ev_io_set(&client->ares_io_watchers[s], s, (read ? EV_READ : 0) | (write ? EV_WRITE : 0));
ev_io_start(worker->loop, &client->ares_io_watchers[s]);
}
else if (client->ares_io_watchers.find(s) != client->ares_io_watchers.end())
{
ev_io_stop(worker->loop, &client->ares_io_watchers[s]);
client->ares_io_watchers.erase(s);
}
}
#endif
bool recorded(const std::chrono::steady_clock::time_point& t)
{
return std::chrono::steady_clock::duration::zero() != t.time_since_epoch();
}
std::string get_reqline(const char* uri, const http_parser_url& u)
{
std::string reqline;
if (util::has_uri_field(u, UF_PATH))
{
reqline = util::get_uri_field(uri, u, UF_PATH).str();
}
else
{
reqline = "/";
}
if (util::has_uri_field(u, UF_QUERY))
{
reqline += '?';
reqline += util::get_uri_field(uri, u, UF_QUERY);
}
return reqline;
}
void print_server_tmp_key(SSL* ssl)
{
// libressl does not have SSL_get_server_tmp_key
#if OPENSSL_VERSION_NUMBER >= 0x10002000L && defined(SSL_get_server_tmp_key)
EVP_PKEY* key;
if (!SSL_get_server_tmp_key(ssl, &key))
{
return;
}
auto key_del = defer(EVP_PKEY_free, key);
std::cerr << "Server Temp Key: ";
auto pkey_id = EVP_PKEY_id(key);
switch (pkey_id)
{
case EVP_PKEY_RSA:
std::cerr << "RSA " << EVP_PKEY_bits(key) << " bits" << std::endl;
break;
case EVP_PKEY_DH:
std::cerr << "DH " << EVP_PKEY_bits(key) << " bits" << std::endl;
break;
case EVP_PKEY_EC:
{
auto ec = EVP_PKEY_get1_EC_KEY(key);
auto ec_del = defer(EC_KEY_free, ec);
auto nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
auto cname = EC_curve_nid2nist(nid);
if (!cname)
{
cname = OBJ_nid2sn(nid);
}
std::cerr << "ECDH " << cname << " " << EVP_PKEY_bits(key) << " bits"
<< std::endl;
break;
}
default:
std::cerr << OBJ_nid2sn(pkey_id) << " " << EVP_PKEY_bits(key) << " bits"
<< std::endl;
break;
}
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
}
// Returns percentage of number of samples within mean +/- sd.
double within_sd(const std::vector<double>& samples, double mean, double sd)
{
if (samples.size() == 0)
{
return 0.0;
}
auto lower = mean - sd;
auto upper = mean + sd;
auto m = std::count_if(
std::begin(samples), std::end(samples),
[&lower, &upper](double t)
{
return lower <= t && t <= upper;
});
return (m / static_cast<double>(samples.size())) * 100;
}
// Computes statistics using |samples|. The min, max, mean, sd, and
// percentage of number of samples within mean +/- sd are computed.
// If |sampling| is true, this computes sample variance. Otherwise,
// population variance.
h2load::SDStat compute_time_stat(const std::vector<double>& samples,
bool sampling)
{
if (samples.empty())
{
return {0.0, 0.0, 0.0, 0.0, 0.0};
}
// standard deviation calculated using Rapid calculation method:
// https://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
double a = 0, q = 0;
size_t n = 0;
double sum = 0;
auto res = SDStat {std::numeric_limits<double>::max(),
std::numeric_limits<double>::min()
};
for (const auto& t : samples)
{
++n;
res.min = std::min(res.min, t);
res.max = std::max(res.max, t);
sum += t;
auto na = a + (t - a) / n;
q += (t - a) * (t - na);
a = na;
}
assert(n > 0);
res.mean = sum / n;
res.sd = sqrt(q / (sampling && n > 1 ? n - 1 : n));
res.within_sd = within_sd(samples, res.mean, res.sd);
return res;
}
bool parse_base_uri(const StringRef& base_uri, h2load::Config& config)
{
http_parser_url u {};
if (http_parser_parse_url(base_uri.c_str(), base_uri.size(), 0, &u) != 0 ||
!util::has_uri_field(u, UF_SCHEMA) || !util::has_uri_field(u, UF_HOST))
{
return false;
}
config.scheme = util::get_uri_field(base_uri.c_str(), u, UF_SCHEMA).str();
config.host = util::get_uri_field(base_uri.c_str(), u, UF_HOST).str();
config.default_port = util::get_default_port(base_uri.c_str(), u);
if (util::has_uri_field(u, UF_PORT))
{
config.port = u.port;
}
else
{
config.port = config.default_port;
}
return true;
}
// Use std::vector<std::string>::iterator explicitly, without that,
// http_parser_url u{} fails with clang-3.4.
std::vector<std::string> parse_uris(std::vector<std::string>::iterator first,
std::vector<std::string>::iterator last, h2load::Config& config)
{
std::vector<std::string> reqlines;
if (first == last)
{
std::cerr << "no URI available" << std::endl;
exit(EXIT_FAILURE);
}
if (!config.has_base_uri())
{
if (!parse_base_uri(StringRef {*first}, config))
{
std::cerr << "invalid URI: " << *first << std::endl;
exit(EXIT_FAILURE);
}
config.base_uri = *first;
}
for (; first != last; ++first)
{
http_parser_url u {};
auto uri = (*first).c_str();
if (http_parser_parse_url(uri, (*first).size(), 0, &u) != 0)
{
std::cerr << "invalid URI: " << uri << std::endl;
exit(EXIT_FAILURE);
}
reqlines.push_back(get_reqline(uri, u));
}
return reqlines;
}
std::vector<std::string> read_uri_from_file(std::istream& infile)
{
std::vector<std::string> uris;
std::string line_uri;
while (std::getline(infile, line_uri))
{
uris.push_back(line_uri);
}
return uris;
}
h2load::SDStats
process_time_stats(const std::vector<std::shared_ptr<h2load::base_worker>>& workers)
{
auto request_times_sampling = false;
auto client_times_sampling = false;
size_t nrequest_times = 0;
size_t nclient_times = 0;
for (const auto& w : workers)
{
nrequest_times += w->stats.req_stats.size();
request_times_sampling = w->request_times_smp.n > w->stats.req_stats.size();
nclient_times += w->stats.client_stats.size();
client_times_sampling = w->client_smp.n > w->stats.client_stats.size();
}
std::vector<double> request_times;
request_times.reserve(nrequest_times);
std::vector<double> connect_times, ttfb_times, rps_values;
connect_times.reserve(nclient_times);
ttfb_times.reserve(nclient_times);
rps_values.reserve(nclient_times);
for (const auto& w : workers)
{
for (const auto& req_stat : w->stats.req_stats)
{
if (!req_stat.completed)
{
continue;
}
request_times.push_back(
std::chrono::duration_cast<std::chrono::duration<double>>(
req_stat.stream_close_time - req_stat.request_time)
.count());
}
const auto& stat = w->stats;
for (const auto& cstat : stat.client_stats)
{
if (recorded(cstat.client_start_time) &&
recorded(cstat.client_end_time))
{
auto t = std::chrono::duration_cast<std::chrono::duration<double>>(
cstat.client_end_time - cstat.client_start_time)
.count();
if (t > 1e-9)
{
rps_values.push_back(cstat.req_success / t);
}
}
// We will get connect event before FFTB.
if (!recorded(cstat.connect_start_time) ||
!recorded(cstat.connect_time))
{
continue;
}
connect_times.push_back(
std::chrono::duration_cast<std::chrono::duration<double>>(
cstat.connect_time - cstat.connect_start_time)
.count());
if (!recorded(cstat.ttfb))
{
continue;
}
ttfb_times.push_back(
std::chrono::duration_cast<std::chrono::duration<double>>(
cstat.ttfb - cstat.connect_start_time)
.count());
}
}
return {compute_time_stat(request_times, request_times_sampling),
compute_time_stat(connect_times, client_times_sampling),
compute_time_stat(ttfb_times, client_times_sampling),
compute_time_stat(rps_values, client_times_sampling)
};
}
bool is_null_destination(h2load::Config& config)
{
return (!config.base_uri_unix && config.connect_to_host.empty() && config.host.empty());
}
void resolve_host(h2load::Config& config)
{
#ifndef _WINDOWS
if (config.base_uri_unix)
{
auto res = std::make_unique<addrinfo>();
res->ai_family = config.unix_addr.sun_family;
res->ai_socktype = SOCK_STREAM;
res->ai_addrlen = sizeof(config.unix_addr);
res->ai_addr =
static_cast<struct sockaddr*>(static_cast<void*>(&config.unix_addr));
config.addrs = res.release();
return;
};
#endif
int rv;
addrinfo hints {}, *res;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_ADDRCONFIG;
const auto& resolve_host =
config.connect_to_host.empty() ? config.host : config.connect_to_host;
auto port =
config.connect_to_port == 0 ? config.port : config.connect_to_port;
rv =
getaddrinfo(resolve_host.c_str(), util::utos(port).c_str(), &hints, &res);
if (rv != 0)
{
std::cerr << "getaddrinfo() failed: " << gai_strerror(rv) << std::endl;
exit(EXIT_FAILURE);
}
if (res == nullptr)
{
std::cerr << "No address returned" << std::endl;
exit(EXIT_FAILURE);
}
config.addrs = res;
}
#ifndef OPENSSL_NO_NEXTPROTONEG
int client_select_next_proto_cb(SSL* ssl, unsigned char** out,
unsigned char* outlen, const unsigned char* in,
unsigned int inlen, void* arg)
{
h2load::Config* config = static_cast<h2load::Config*>(arg);
if (util::select_protocol(const_cast<const unsigned char**>(out), outlen, in,
inlen, config->npn_list))
{
return SSL_TLSEXT_ERR_OK;
}
// OpenSSL will terminate handshake with fatal alert if we return
// NOACK. So there is no way to fallback.
return SSL_TLSEXT_ERR_NOACK;
}
#endif // !OPENSSL_NO_NEXTPROTONEG
void populate_config_from_json(h2load::Config& config)
{
config.scheme = config.json_config_schema.schema;
config.host = config.json_config_schema.host;
config.port = config.json_config_schema.port;
config.default_port = (config.scheme == "https" ? 443 : 80);
config.ciphers = config.json_config_schema.ciphers;
config.conn_active_timeout = config.json_config_schema.connection_active_timeout;
config.conn_inactivity_timeout = config.json_config_schema.connection_inactivity_timeout;
config.duration = config.json_config_schema.duration;
config.encoder_header_table_size = config.json_config_schema.encoder_header_table_size;
config.header_table_size = config.json_config_schema.header_table_size;
config.max_concurrent_streams = config.json_config_schema.max_concurrent_streams;
config.nclients = config.json_config_schema.clients;
if (config.json_config_schema.no_tls_proto == "h2c")
{
config.no_tls_proto = h2load::Config::PROTO_HTTP2;
}
else
{
config.no_tls_proto = h2load::Config::PROTO_HTTP1_1;
}
config.npn_list = util::parse_config_str_list(StringRef {config.json_config_schema.npn_list.c_str()});
config.nreqs = config.json_config_schema.nreqs;
config.nthreads = config.json_config_schema.threads;
config.rate = config.json_config_schema.rate;
config.rate_period = config.json_config_schema.rate_period;
config.rps = config.json_config_schema.request_per_second;
config.rps_file = config.json_config_schema.rps_file;
config.stream_timeout_in_ms = config.json_config_schema.stream_timeout_in_ms;
config.window_bits = config.json_config_schema.window_bits;
config.connection_window_bits = config.json_config_schema.connection_window_bits;
config.warm_up_time = config.json_config_schema.warm_up_time;
}
void insert_customized_headers_to_Json_scenarios(h2load::Config& config)
{
if (config.json_config_schema.scenarios.size() && config.custom_headers.size())
{
for (auto& header : config.custom_headers)
{
//std::string header_name = header.name;
//util::inp_strlower(header_name);
for (auto& scenario : config.json_config_schema.scenarios)
{
for (auto& request : scenario.requests)
{
request.headers_in_map[header.name] = header.value;
request.additonalHeaders.emplace_back(header.name + ":" + header.value);
}
}
}
}
}
std::string get_tls_error_string()
{
unsigned long error_code = 0;
char error_code_string[2048];
const char* file = 0, *data = 0;
int line = 0, flags = 0;
std::string error_string;
//pthread_t tid = pthread_self();
while ((error_code = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0)
{
ERR_error_string_n(error_code, error_code_string,
sizeof(error_code_string));
std::stringstream strm;
strm << error_code_string << ":" << file << ":" << line << ":additional info...\"" << ((
flags & ERR_TXT_STRING) ? data : "") << "\"\n";
error_string += strm.str();
}
return error_string;
}
void printBacktrace()
{
#ifndef _WINDOWS
void* buffer[64];
int num = backtrace((void**) &buffer, 64);
char** addresses = backtrace_symbols(buffer, num);
for (int i = 0 ; i < num ; ++i)
{
fprintf(stderr, "[%2d]: %s\n", i, addresses[i]);
}
free(addresses);
#endif
}
uint64_t find_common_multiple(std::vector<size_t> input)
{
std::set<size_t> unique_values;
for (auto val : input)
{
unique_values.insert(val);
}
std::set<size_t> final_set;
for (auto iter = unique_values.rbegin(); iter != unique_values.rend(); iter++)
{
auto val = *iter;
auto find_multiple = [val](size_t val_in_set)
{
if ((val_in_set / val >= 1) && (val_in_set % val == 0))
{
return true;
}
return false;
};
if (std::find_if(final_set.begin(), final_set.end(), find_multiple) == final_set.end())
{
final_set.insert(val);
}
}
uint64_t retVal = 1;
for (auto val : final_set)
{
retVal *= val;
}
return retVal;
}
template<typename T>
std::string to_string_with_precision_3(const T a_value)
{
std::ostringstream out;
out.precision(3);
out << std::fixed << a_value;
return out.str();
}
size_t get_request_name_max_width(h2load::Config& config)
{
size_t width = 0;
for (size_t scenario_index = 0; scenario_index < config.json_config_schema.scenarios.size(); scenario_index++)
{
std::string req_name = std::string(config.json_config_schema.scenarios[scenario_index].name).append("_").append(
std::to_string(config.json_config_schema.scenarios[scenario_index].requests.size()));
if (req_name.size() > width)
{
width = req_name.size();
}
}
return width;
}
void output_realtime_stats(h2load::Config& config,
std::vector<std::shared_ptr<h2load::base_worker>>& workers,
std::atomic<bool>& workers_stopped, std::stringstream& dataStream)
{
std::vector<std::vector<size_t>> scenario_req_sent_till_now;
std::vector<std::vector<size_t>> scenario_req_done_till_now;
std::vector<std::vector<size_t>> scenario_req_success_till_now;
std::vector<std::vector<size_t>> scenario_2xx_till_now;
std::vector<std::vector<size_t>> scenario_3xx_till_now;
std::vector<std::vector<size_t>> scenario_4xx_till_now;
std::vector<std::vector<size_t>> scenario_5xx_till_now;
for (size_t scenario_index = 0; scenario_index < config.json_config_schema.scenarios.size(); scenario_index++)
{
std::vector<size_t> req_vec(config.json_config_schema.scenarios[scenario_index].requests.size(), 0);
scenario_req_sent_till_now.push_back(req_vec);
scenario_req_done_till_now.push_back(req_vec);
scenario_req_success_till_now.push_back(req_vec);
scenario_2xx_till_now.push_back(req_vec);
scenario_3xx_till_now.push_back(req_vec);
scenario_4xx_till_now.push_back(req_vec);
scenario_5xx_till_now.push_back(req_vec);
}
auto period_start = std::chrono::steady_clock::now();
while (!workers_stopped)
{
auto scenario_req_sent_till_last_interval = scenario_req_sent_till_now;
auto scenario_req_done_till_last_interval = scenario_req_done_till_now;
auto scenario_req_success_till_last_interval = scenario_req_success_till_now;
auto scenario_2xx_till_last_interval = scenario_2xx_till_now;
auto scenario_3xx_till_last_interval = scenario_3xx_till_now;
auto scenario_4xx_till_last_interval = scenario_4xx_till_now;
auto scenario_5xx_till_last_interval = scenario_5xx_till_now;
std::this_thread::sleep_for(std::chrono::milliseconds(config.json_config_schema.statistics_interval * 1000));
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
auto period_end = std::chrono::steady_clock::now();
auto period_duration = std::chrono::duration_cast<std::chrono::milliseconds>(period_end - period_start).count();;
period_start = period_end;
std::stringstream outputStream;
static uint64_t counter = 0;
if (counter % 10 == 0)
{
outputStream <<
"time, request, sent/s, done/s, success/s, (done/s)/(sent/s), (success/s)/(done/s), delta_2xx, 3xx, 4xx, 5xx, latency-min(ms), max, mean, sd, +/-sd, total-sent, total-done, total-success, done/sent(total), success/done(total)";
outputStream << std::endl;
}
counter++;
static size_t rps_width = 0;
static size_t total_req_width = 0;
static size_t percentage_width = 8;
static size_t latency_width = 5;
static size_t request_name_width = get_request_name_max_width(config);
auto latency_stats = produce_requests_latency_stats(workers);
for (size_t scenario_index = 0; scenario_index < config.json_config_schema.scenarios.size(); scenario_index++)
{
for (size_t request_index = 0; request_index < config.json_config_schema.scenarios[scenario_index].requests.size();
request_index++)
{
scenario_req_sent_till_now[scenario_index][request_index] = 0;
scenario_req_done_till_now[scenario_index][request_index] = 0;
scenario_req_success_till_now[scenario_index][request_index] = 0;
scenario_2xx_till_now[scenario_index][request_index] = 0;
scenario_3xx_till_now[scenario_index][request_index] = 0;
scenario_4xx_till_now[scenario_index][request_index] = 0;
scenario_5xx_till_now[scenario_index][request_index] = 0;
for (auto& w : workers)
{
auto& s = *(w->scenario_stats[scenario_index][request_index]);