-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathbench.cpp
More file actions
1229 lines (1151 loc) · 52.6 KB
/
bench.cpp
File metadata and controls
1229 lines (1151 loc) · 52.6 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
// Copyright Takatoshi Kondo 2020
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <mqtt/config.hpp>
#include <mqtt/setup_log.hpp>
#include <mqtt/async_client.hpp>
#include <thread>
#include <fstream>
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include "locked_cout.hpp"
namespace as = boost::asio;
// moved to global to avoid MSVC error
enum class phase {
connect,
sub_delay,
subscribe,
pub_delay,
idle,
pub_after_idle_delay,
publish
};
int main(int argc, char **argv) {
try {
boost::program_options::options_description desc;
constexpr std::size_t min_payload = 15;
std::string payload_size_desc =
"payload bytes. must be greater than " + std::to_string(min_payload);
boost::program_options::options_description general_desc("General options");
general_desc.add_options()
("help", "produce help message")
(
"cfg",
boost::program_options::value<std::string>()->default_value("bench.conf"),
"Load configuration file"
)
(
"host",
boost::program_options::value<std::string>(),
"mqtt broker's hostname to connect"
)
(
"port",
boost::program_options::value<std::uint16_t>()->default_value(1883),
"mqtt broker's port to connect"
)
(
"protocol",
boost::program_options::value<std::string>()->default_value("mqtt"),
"mqtt mqtts ws wss"
)
(
"mqtt_version",
boost::program_options::value<std::string>()->default_value("v5"),
"MQTT version v5 or v3.1.1"
)
(
"qos",
boost::program_options::value<unsigned int>()->default_value(0),
"QoS 0, 1, or 2"
)
(
"payload_size",
boost::program_options::value<std::size_t>()->default_value(1024),
payload_size_desc.c_str()
)
(
"compare",
boost::program_options::value<bool>()->default_value(false),
"compare send/receive payloads"
)
(
"retain",
boost::program_options::value<bool>()->default_value(false),
"set retain flag to publish"
)
(
"clean_start",
boost::program_options::value<bool>()->default_value(true),
"set clean_start flag to client"
)
(
"sei",
boost::program_options::value<std::uint32_t>()->default_value(0),
"set session expiry interval to client"
)
(
"start_index",
boost::program_options::value<std::size_t>()->default_value(0),
"start index of clients and topics"
)
(
"times",
boost::program_options::value<std::size_t>()->default_value(1000),
"number of publishes for each client"
)
(
"username",
boost::program_options::value<std::string>(),
"username for all clients"
)
(
"password",
boost::program_options::value<std::string>(),
"password for all clients"
)
(
"cid_prefix",
boost::program_options::value<std::string>()->default_value(""),
"client_id prefix. client_id is cid_prefix00000000 cid_prefix00000001 ..."
)
(
"topic_prefix",
boost::program_options::value<std::string>()->default_value(""),
"topic_id prefix. topic is topic_prefix00000000 topic_prefix00000001 ..."
)
(
"limit_ms",
boost::program_options::value<std::size_t>()->default_value(0),
"Output time over message if round trip time is greater than limit_ms. 0 means no limit"
)
(
"iocs",
boost::program_options::value<std::size_t>()->default_value(1),
"Number of io_context. If set 0 then automatically decided by hardware_concurrency()."
)
(
"threads_per_ioc",
boost::program_options::value<std::size_t>()->default_value(1),
"Number of worker threads for each io_context."
)
(
"clients",
boost::program_options::value<std::size_t>()->default_value(1),
"Number of clients."
)
(
"con_interval_ms",
boost::program_options::value<std::size_t>()->default_value(10),
"connect interval (ms)"
)
(
"sub_delay_ms",
boost::program_options::value<std::size_t>()->default_value(1000),
"subscribe delay after all connected (ms)"
)
(
"sub_interval_ms",
boost::program_options::value<std::size_t>()->default_value(10),
"subscribe interval (ms)"
)
(
"pub_delay_ms",
boost::program_options::value<std::size_t>()->default_value(1000),
"publish delay after all subscribed (ms)"
)
(
"pub_after_idle_delay_ms",
boost::program_options::value<std::size_t>()->default_value(1000),
"publish delay after idle publishes are finished (ms)"
)
(
"pub_interval_ms",
boost::program_options::value<std::size_t>()->default_value(10),
"publish interval for each clients (ms)"
)
(
"detail_report",
boost::program_options::value<bool>()->default_value(false),
"report for each client's max mid avg min"
)
(
"pub_idle_count",
boost::program_options::value<std::size_t>()->default_value(1),
"ideling publish count. it is useful to ignore authorization cache."
)
(
"progress_timer_sec",
boost::program_options::value<std::size_t>()->default_value(10),
"report progress timer for each given seconds."
)
#if defined(MQTT_USE_LOG)
(
"verbose",
boost::program_options::value<unsigned int>()->default_value(1),
"set verbose level, possible values:\n 0 - Fatal\n 1 - Error\n 2 - Warning\n 3 - Info\n 4 - Debug\n 5 - Trace"
)
#endif // defined(MQTT_USE_LOG)
(
"cacert",
boost::program_options::value<std::string>(),
"CA Certificate file to verify server certificate for mqtts and wss connections"
)
(
"ws_path",
boost::program_options::value<std::string>(),
"Web-Socket path for ws and wss connections"
)
;
desc.add(general_desc);
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
std::string config_file = vm["cfg"].as<std::string>();
if (!config_file.empty()) {
std::ifstream input(vm["cfg"].as<std::string>());
if (input.good()) {
boost::program_options::store(boost::program_options::parse_config_file(input, desc), vm);
} else
{
std::cerr << "Configuration file '"
<< config_file
<< "' not found, bench doesn't use configuration file." << std::endl;
}
}
boost::program_options::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return 1;
}
std::cout << "Set options:" << std::endl;
for (auto const& e : vm) {
std::cout << boost::format("%-16s") % e.first.c_str() << " : ";
if (auto p = boost::any_cast<std::string>(&e.second.value())) {
if (e.first.c_str() == std::string("password")) {
std::cout << "********";
}
else {
std::cout << *p;
}
}
else if (auto p = boost::any_cast<std::size_t>(&e.second.value())) {
std::cout << *p;
}
else if (auto p = boost::any_cast<std::uint32_t>(&e.second.value())) {
std::cout << *p;
}
else if (auto p = boost::any_cast<std::uint16_t>(&e.second.value())) {
std::cout << *p;
}
else if (auto p = boost::any_cast<unsigned int>(&e.second.value())) {
std::cout << *p;
}
else if (auto p = boost::any_cast<bool>(&e.second.value())) {
std::cout << std::boolalpha << *p;
}
std::cout << std::endl;
}
#if defined(MQTT_USE_LOG)
switch (vm["verbose"].as<unsigned int>()) {
case 5:
MQTT_NS::setup_log(MQTT_NS::severity_level::trace);
break;
case 4:
MQTT_NS::setup_log(MQTT_NS::severity_level::debug);
break;
case 3:
MQTT_NS::setup_log(MQTT_NS::severity_level::info);
break;
case 2:
MQTT_NS::setup_log(MQTT_NS::severity_level::warning);
break;
default:
MQTT_NS::setup_log(MQTT_NS::severity_level::error);
break;
case 0:
MQTT_NS::setup_log(MQTT_NS::severity_level::fatal);
break;
}
#else
MQTT_NS::setup_log();
#endif
if (!vm.count("host")) {
std::cerr << "host must be set" << std::endl;
return -1;
}
std::chrono::steady_clock::time_point tp_start = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point tp_sub_delay;
std::chrono::steady_clock::time_point tp_subscribe;
std::chrono::steady_clock::time_point tp_pub_delay;
std::chrono::steady_clock::time_point tp_idle;
std::chrono::steady_clock::time_point tp_pub_after_idle_delay;
std::chrono::steady_clock::time_point tp_publish;
auto detail_report = vm["detail_report"].as<bool>();
auto host = vm["host"].as<std::string>();
auto port = vm["port"].as<std::uint16_t>();
auto protocol = vm["protocol"].as<std::string>();
auto mqtt_version = vm["mqtt_version"].as<std::string>();
auto qos = static_cast<MQTT_NS::qos>(vm["qos"].as<unsigned int>());
auto retain =
[&] () -> MQTT_NS::retain {
if (vm["retain"].as<bool>()) {
return MQTT_NS::retain::yes;
}
return MQTT_NS::retain::no;
} ();
auto clean_start = vm["clean_start"].as<bool>();
auto sei = vm["sei"].as<std::uint32_t>();
auto payload_size = vm["payload_size"].as<std::size_t>();
if (payload_size <= min_payload) {
std::cout
<< "payload_size must be greater than "
<< std::to_string(min_payload)
<< ". payload_size:" << payload_size
<< std::endl;
return -1;
}
auto compare = vm["compare"].as<bool>();
auto clients = vm["clients"].as<std::size_t>();
auto start_index = vm["start_index"].as<std::size_t>();
auto times = vm["times"].as<std::size_t>();
if (times == 0) {
std::cout << "times must be greater than 0" << std::endl;
return -1;
}
auto pub_idle_count = vm["pub_idle_count"].as<std::size_t>();
times += pub_idle_count;
auto username =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("username")) {
return vm["username"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
auto password =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("password")) {
return vm["password"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
auto cid_prefix = vm["cid_prefix"].as<std::string>();
auto topic_prefix = vm["topic_prefix"].as<std::string>();
auto cacert =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("cacert")) {
return vm["cacert"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
auto ws_path =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("ws_path")) {
return vm["ws_path"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
auto limit_ms = vm["limit_ms"].as<std::size_t>();
auto con_interval_ms = vm["con_interval_ms"].as<std::size_t>();
auto sub_delay_ms = vm["sub_delay_ms"].as<std::size_t>();
auto sub_interval_ms = vm["sub_interval_ms"].as<std::size_t>();
auto pub_delay_ms = vm["pub_delay_ms"].as<std::size_t>();
auto pub_after_idle_delay_ms = vm["pub_after_idle_delay_ms"].as<std::size_t>();
auto pub_interval_ms = vm["pub_interval_ms"].as<std::size_t>();
auto progress_timer_sec = vm["progress_timer_sec"].as<std::size_t>();
std::uint64_t pub_interval_us = pub_interval_ms * 1000;
std::cout << "pub_interval:" << pub_interval_us << " us" << std::endl;
std::uint64_t all_interval_ns = pub_interval_us * 1000 / static_cast<std::uint64_t>(clients);
std::cout << "all_interval:" << all_interval_ns << " ns" << std::endl;
std::cout << (double(1) * 1000 * 1000 * 1000 / static_cast<double>(all_interval_ns)) << " publish/sec" << std::endl;
auto num_of_iocs =
[&] () -> std::size_t {
if (vm.count("iocs")) {
return vm["iocs"].as<std::size_t>();
}
return 1;
} ();
if (num_of_iocs == 0) {
num_of_iocs = std::thread::hardware_concurrency();
std::cout << "iocs set to auto decide (0). Automatically set to " << num_of_iocs << std::endl;;
}
auto threads_per_ioc =
[&] () -> std::size_t {
if (vm.count("threads_per_ioc")) {
return vm["threads_per_ioc"].as<std::size_t>();
}
return 1;
} ();
if (threads_per_ioc == 0) {
threads_per_ioc = std::min(std::size_t(std::thread::hardware_concurrency()), std::size_t(4));
std::cout << "threads_per_ioc set to auto decide (0). Automatically set to " << threads_per_ioc << std::endl;
}
std::cout
<< "iocs:" << num_of_iocs
<< " threads_per_ioc:" << threads_per_ioc
<< " total threads:" << num_of_iocs * threads_per_ioc
<< std::endl;
std::vector<as::io_context> iocs(num_of_iocs);
BOOST_ASSERT(!iocs.empty());
std::vector<
as::executor_work_guard<
as::io_context::executor_type
>
> guard_iocs;
guard_iocs.reserve(iocs.size());
for (auto& ioc : iocs) {
guard_iocs.emplace_back(ioc.get_executor());
}
MQTT_NS::protocol_version version =
[&] {
if (mqtt_version == "v5" || mqtt_version == "5" || mqtt_version == "v5.0" || mqtt_version == "5.0") {
return MQTT_NS::protocol_version::v5;
}
else if (mqtt_version == "v3.1.1" || mqtt_version == "3.1.1") {
return MQTT_NS::protocol_version::v3_1_1;
}
else {
std::cerr << "invalid mqtt_version:" << mqtt_version << " it should be v5 or v3.1.1" << std::endl;
return MQTT_NS::protocol_version::undetermined;
}
} ();
if (version != MQTT_NS::protocol_version::v5 &&
version != MQTT_NS::protocol_version::v3_1_1) {
return -1;
}
auto bench_proc =
[&](auto& cis) {
std::atomic<phase> ph{phase::connect};
as::io_context ioc_progress_timer;
auto tim_progress = std::make_shared<as::steady_timer>(ioc_progress_timer);
as::io_context ioc_timer;
as::executor_work_guard<as::io_context::executor_type> guard_ioc_timer(ioc_timer.get_executor());
as::steady_timer tim_delay{ioc_timer};
std::atomic<std::size_t> rest_connect{clients};
std::atomic<std::size_t> rest_sub{clients};
std::atomic<std::size_t> rest_idle{pub_idle_count * clients};
std::atomic<std::uint64_t> rest_times{times * clients};
// ==== begin local lambda expressions
auto sub_proc =
[&] {
ph.store(phase::sub_delay);
tp_sub_delay = std::chrono::steady_clock::now();
tim_delay.expires_after(std::chrono::milliseconds(sub_delay_ms));
tim_delay.async_wait(
[&] (boost::system::error_code const& ec) {
if (ec) {
std::cout << "timer error:" << ec.message() << std::endl;
return;
}
ph.store(phase::subscribe);
tp_subscribe = std::chrono::steady_clock::now();
std::cout << "Subscribe" << std::endl;
std::size_t index = 0;
for (auto& ci : cis) {
ci.tim->expires_after(std::chrono::milliseconds(sub_interval_ms) * ++index);
ci.tim->async_wait(
[&] (boost::system::error_code const& ec) {
if (ec) {
std::cout << "timer error:" << ec.message() << std::endl;
return;
}
ci.c->async_subscribe(
topic_prefix + ci.index_str,
qos,
[&](MQTT_NS::error_code ec) {
if (ec) {
std::cout << "sub error:" << ec.message() << std::endl;
}
}
);
}
);
}
}
);
};
using ci_t = typename std::remove_reference_t<decltype(cis.front())>;
std::function <void(ci_t&)> async_wait_pub;
async_wait_pub =
[&] (ci_t& ci) {
ci.tim->async_wait(
[&] (boost::system::error_code const& ec) {
if (ec && ec != as::error::operation_aborted) {
std::cout << "timer error:" << ec.message() << std::endl;
}
else {
MQTT_NS::publish_options opts = qos | retain;
ci.sent.at(ci.send_times - 1) = std::chrono::steady_clock::now();
ci.c->async_publish(
MQTT_NS::allocate_buffer(topic_prefix + ci.index_str),
ci.send_payload(),
opts,
[&](MQTT_NS::error_code ec) {
if (ec) {
locked_cout() << "pub error:" << ec.message() << std::endl;
}
}
);
BOOST_ASSERT(ci.send_times != 0);
--ci.send_times;
switch (ph.load()) {
case phase::idle:
BOOST_ASSERT(ci.send_idle_count != 0);
if (--ci.send_idle_count != 0) {
ci.tim->expires_at(
ci.tim->expiry() +
std::chrono::milliseconds(pub_interval_ms)
);
async_wait_pub(ci);
}
break;
case phase::publish:
if (ci.send_times != 0) {
ci.tim->expires_at(
ci.tim->expiry() +
std::chrono::milliseconds(pub_interval_ms)
);
async_wait_pub(ci);
}
break;
default:
BOOST_ASSERT(false);
break;
};
}
}
);
};
auto pub_idle_proc =
[&] {
ph.store(phase::idle);
tp_idle = std::chrono::steady_clock::now();
locked_cout() << "Publish (idle)" << std::endl;
std::size_t index = 0;
for (auto& ci : cis) {
auto tp =
std::chrono::nanoseconds(all_interval_ns) * index++;
ci.tim->expires_after(tp);
async_wait_pub(ci);
}
};
auto pub_idle_delay_proc =
[&] {
locked_cout() << "Publish (idle) delay" << std::endl;
ph.store(phase::pub_delay);
tp_pub_delay = std::chrono::steady_clock::now();
tim_delay.expires_after(std::chrono::milliseconds(pub_delay_ms));
tim_delay.async_wait(
[&] (boost::system::error_code const& ec) {
if (ec) {
locked_cout() << "pub_idle_delay timer error:" << ec.message() << std::endl;
return;
}
pub_idle_proc();
}
);
};
auto pub_proc =
[&] {
ph.store(phase::publish);
tp_publish = std::chrono::steady_clock::now();
locked_cout() << "Publish (measure)" << std::endl;
std::size_t index = 0;
for (auto& ci : cis) {
auto tp =
std::chrono::milliseconds(pub_after_idle_delay_ms) +
std::chrono::nanoseconds(all_interval_ns) * index++;
ci.tim->expires_after(tp);
async_wait_pub(ci);
}
};
auto pub_after_idle_delay_proc =
[&] {
ph.store(phase::pub_after_idle_delay);
tp_pub_after_idle_delay = std::chrono::steady_clock::now();
tp_pub_after_idle_delay = std::chrono::steady_clock::now();
locked_cout() << "Publish (measure) delay" << std::endl;
tim_delay.expires_after(std::chrono::milliseconds(pub_after_idle_delay_ms));
tim_delay.async_wait(
[&] (boost::system::error_code const& ec) {
if (ec) {
locked_cout() << "pub_after_idle_delay timer error:" << ec.message() << std::endl;
return;
}
pub_proc();
}
);
};
auto finish_proc =
[&] {
locked_cout() << "Report" << std::endl;
std::size_t maxmax = 0;
std::string maxmax_cid;
std::size_t maxmid = 0;
std::string maxmid_cid;
std::size_t maxavg = 0;
std::string maxavg_cid;
std::size_t maxmin = 0;
std::string maxmin_cid;
for (auto& ci : cis) {
std::sort(ci.rtt_us.begin(), ci.rtt_us.end());
std::string cid = ci.c->get_client_id();
std::size_t max = ci.rtt_us.back();
std::size_t mid = ci.rtt_us.at(ci.rtt_us.size() / 2);
std::size_t avg = std::accumulate(
ci.rtt_us.begin(),
ci.rtt_us.end(),
std::size_t(0)
) / ci.rtt_us.size();
std::size_t min = ci.rtt_us.front();
if (maxmax < max) {
maxmax = max;
maxmax_cid = cid;
}
if (maxmid < mid) {
maxmid = mid;
maxmid_cid = cid;
}
if (maxavg < avg) {
maxavg = avg;
maxavg_cid = cid;
}
if (maxmin < min) {
maxmin = min;
maxmin_cid = cid;
}
if (detail_report) {
locked_cout()
<< cid << " :"
<< " max:" << boost::format("%+12d") % max << " us | "
<< " mid:" << boost::format("%+12d") % mid << " us | "
<< " avg:" << boost::format("%+12d") % avg << " us | "
<< " min:" << boost::format("%+12d") % min << " us | "
<< std::endl;
}
}
locked_cout()
<< "maxmax:" << boost::format("%+12d") % maxmax << " us "
<< "(" << boost::format("%+8d") % (maxmax / 1000) << " ms ) "
<< "client_id:" << maxmax_cid << std::endl;
locked_cout()
<< "maxmid:" << boost::format("%+12d") % maxmid << " us "
<< "(" << boost::format("%+8d") % (maxmid / 1000) << " ms ) "
<< "client_id:" << maxmid_cid << std::endl;
locked_cout()
<< "maxavg:" << boost::format("%+12d") % maxavg << " us "
<< "(" << boost::format("%+8d") % (maxavg / 1000) << " ms ) "
<< "client_id:" << maxavg_cid << std::endl;
locked_cout()
<< "maxmin:" << boost::format("%+12d") % maxmin << " us "
<< "(" << boost::format("%+8d") % (maxmin / 1000) << " ms ) "
<< "client_id:" << maxmin_cid << std::endl;
for (auto& ci : cis) {
ci.c->async_force_disconnect();
}
locked_cout() << "Finish" << std::endl;
for (auto& guard_ioc : guard_iocs) guard_ioc.reset();
guard_ioc_timer.reset();
};
using packet_id_t = typename std::remove_reference_t<decltype(*cis.front().c)>::packet_id_t;
auto publish_handler =
[&](auto& ci,
MQTT_NS::optional<packet_id_t> /*packet_id*/,
MQTT_NS::publish_options pubopts,
MQTT_NS::buffer topic_name,
MQTT_NS::buffer contents,
MQTT_NS::v5::properties /*props*/) {
if (pubopts.get_retain() == MQTT_NS::retain::yes) {
locked_cout() << "retained publish received and ignored topic:" << topic_name << std::endl;
return;
}
BOOST_ASSERT(rest_times > 0);
--rest_times;
if (rest_idle > 0) {
--ci.recv_times;
if (--rest_idle == 0) pub_after_idle_delay_proc();
}
else {
auto recv = std::chrono::steady_clock::now();
auto dur_us = std::chrono::duration_cast<std::chrono::microseconds>(
recv - ci.sent.at(ci.recv_times - 1)
).count();
if (limit_ms != 0 && static_cast<unsigned long>(dur_us) > limit_ms * 1000) {
std::cout << "RTT:" << (dur_us / 1000) << "ms over " << limit_ms << "ms" << std::endl;
}
if (compare) {
if (contents != ci.recv_payload()) {
locked_cout() << "received payload doesn't match to sent one" << std::endl;
locked_cout() << " expected: " << ci.recv_payload() << std::endl;
locked_cout() << " received: " << contents << std::endl;;
}
}
if (topic_name != topic_prefix + ci.index_str) {
locked_cout() << "topic doesn't match" << std::endl;
locked_cout() << " expected: " << topic_prefix + ci.index_str << std::endl;
locked_cout() << " received: " << topic_name << std::endl;
}
ci.rtt_us.emplace_back(dur_us);
BOOST_ASSERT(ci.recv_times != 0);
--ci.recv_times;
if (rest_times == 0) finish_proc();
}
};
// ==== end local lambda expressions
for (auto& ci : cis) {
ci.c->set_auto_pub_response(true);
ci.c->set_async_operation(true);
ci.c->set_clean_start(clean_start);
if (username) ci.c->set_user_name(username.value());
if (password) ci.c->set_password(password.value());
ci.c->set_client_id(cid_prefix + ci.index_str);
ci.c->set_connack_handler(
[&]
(bool /*sp*/, MQTT_NS::connect_return_code connack_return_code) {
if (connack_return_code == MQTT_NS::connect_return_code::accepted) {
if (--rest_connect == 0) sub_proc();
}
else {
std::cout << "connack error:" << connack_return_code << std::endl;
}
}
);
ci.c->set_v5_connack_handler(
[&]
(bool /*sp*/, MQTT_NS::v5::connect_reason_code reason_code, MQTT_NS::v5::properties /*props*/) {
if (reason_code == MQTT_NS::v5::connect_reason_code::success) {
if (--rest_connect == 0) sub_proc();
}
else {
std::cout << "connack error:" << reason_code << std::endl;
}
}
);
ci.c->set_suback_handler(
[&]
(packet_id_t /*packet_id*/, std::vector<MQTT_NS::suback_return_code> results) {
BOOST_ASSERT(results.size() == 1);
if (results.front() == MQTT_NS::suback_return_code::success_maximum_qos_0 ||
results.front() == MQTT_NS::suback_return_code::success_maximum_qos_1 ||
results.front() == MQTT_NS::suback_return_code::success_maximum_qos_2) {
if (--rest_sub == 0) {
if (pub_idle_count == 0) {
pub_after_idle_delay_proc();
}
else {
pub_idle_delay_proc();
}
}
}
}
);
ci.c->set_v5_suback_handler(
[&]
(packet_id_t /*packet_id*/,
std::vector<MQTT_NS::v5::suback_reason_code> reasons,
MQTT_NS::v5::properties /*props*/) {
BOOST_ASSERT(reasons.size() == 1);
if (reasons.front() == MQTT_NS::v5::suback_reason_code::granted_qos_0 ||
reasons.front() == MQTT_NS::v5::suback_reason_code::granted_qos_1 ||
reasons.front() == MQTT_NS::v5::suback_reason_code::granted_qos_2) {
if (--rest_sub == 0) {
if (pub_idle_count == 0) {
pub_after_idle_delay_proc();
}
else {
pub_idle_delay_proc();
}
}
}
}
);
ci.c->set_publish_handler(
[&]
(MQTT_NS::optional<packet_id_t> packet_id,
MQTT_NS::publish_options pubopts,
MQTT_NS::buffer topic_name,
MQTT_NS::buffer contents) {
publish_handler(ci, packet_id, pubopts, topic_name, contents, MQTT_NS::v5::properties{});
}
);
ci.c->set_v5_publish_handler(
[&]
(MQTT_NS::optional<packet_id_t> packet_id,
MQTT_NS::publish_options pubopts,
MQTT_NS::buffer topic_name,
MQTT_NS::buffer contents,
MQTT_NS::v5::properties props) {
publish_handler(ci, packet_id, pubopts, topic_name, contents, MQTT_NS::force_move(props));
}
);
}
std::function <void()> tim_progress_proc;
tim_progress_proc =
[&, wp = std::weak_ptr<as::steady_timer>(tim_progress)] {
if (auto sp = wp.lock()) {
sp->expires_after(std::chrono::seconds(progress_timer_sec));
sp->async_wait(
[&] (boost::system::error_code const& ec) {
if (!ec) {
std::string ph_str;
auto now = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point tp_base;
switch(ph.load()) {
case phase::connect:
ph_str = "connect";
tp_base = tp_start;
break;
case phase::sub_delay:
ph_str = "sub_delay";
tp_base = tp_sub_delay;
break;
case phase::subscribe:
ph_str = "subscribe";
tp_base = tp_subscribe;
break;
case phase::pub_delay:
ph_str = "pub_delay";
tp_base = tp_pub_delay;
break;
case phase::idle:
ph_str = "idle";
tp_base = tp_idle;
break;
case phase::pub_after_idle_delay:
ph_str = "pub_after_idle_delay";
tp_base = tp_pub_after_idle_delay;
break;
case phase::publish:
ph_str = "publish";
tp_base = tp_publish;
break;
default:
BOOST_ASSERT(false);
break;
}
auto dur_ph = std::chrono::duration_cast<std::chrono::seconds>(
now - tp_base
).count();
auto dur_total = std::chrono::duration_cast<std::chrono::seconds>(
now - tp_start
).count();
locked_cout()
<< "[progress] "
<< (
boost::format("%-24s: %3d:%02d | total: %3d:%02d") %
ph_str %
(dur_ph / 60) % (dur_ph % 60) %
(dur_total / 60) % (dur_total % 60)
)
<< std::endl;
tim_progress_proc();
}
}
);
}
};
if (progress_timer_sec > 0) {
tim_progress_proc();
}
std::thread th_progress_timer {
[&] {
ioc_progress_timer.run();
}
};
std::size_t index = 0;
for (auto& ci : cis) {
auto tim = std::make_shared<as::steady_timer>(ioc_timer);
tim->expires_after(std::chrono::milliseconds(con_interval_ms) * ++index);
tim->async_wait(
[&, tim] (boost::system::error_code const& ec) {
if (ec) {
std::cout << "timer error:" << ec.message() << std::endl;
return;
}
MQTT_NS::v5::properties props;
if (sei != 0) {
props.emplace_back(
MQTT_NS::v5::property::session_expiry_interval(sei)
);
}
ci.c->async_connect(
MQTT_NS::force_move(props),
[&](MQTT_NS::error_code ec) {
if (ec) {
std::cerr << "async_connect error: " << ec.message() << std::endl;
}
ci.init_timer(ci.c->get_executor());
}
);
}
);
}
std::thread th_timer {
[&] {
ioc_timer.run();
}
};
std::vector<std::thread> ths;
ths.reserve(num_of_iocs * threads_per_ioc);
for (auto& ioc : iocs) {
for (std::size_t i = 0; i != threads_per_ioc; ++i) {
ths.emplace_back(
[&] {
ioc.run();
}
);
}
}
as::io_context ioc_signal;
as::signal_set signals{ioc_signal, SIGINT, SIGTERM};
signals.async_wait(
[] (
boost::system::error_code const& ec,
int num
) {
if (!ec) {
std::cerr << "Signal " << num << " received. exit program" << std::endl;
exit(-1);
}
}
);
std::thread th_signal {
[&] {
ioc_signal.run();
}
};
for (auto& th : ths) th.join();
th_timer.join();
tim_progress->cancel();
th_progress_timer.join();
signals.cancel();
th_signal.join();
};
std::cout << "Prepare clients" << std::endl;
std::cout << " protocol:" << protocol << std::endl;
struct client_info_base {
client_info_base(std::size_t index, std::size_t payload_size, std::size_t times, std::size_t idle_count)
:index_str{(boost::format("%08d") % index).str()},
send_times{times},
recv_times{times},
send_idle_count{idle_count},
recv_idle_count{idle_count}
{