-
Notifications
You must be signed in to change notification settings - Fork 13
/
RifleVampireTests.cpp
2254 lines (2051 loc) · 74.9 KB
/
RifleVampireTests.cpp
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 <czmq.h>
#include <boost/thread.hpp>
#include "RifleVampireTests.h"
#include "Death.h"
#include "FileIO.h"
#include <TimeStats.h>
#include <TriggerTimeStats.h>
#include <g3log/g3log.hpp>
#include <QAPI.h>
#include <q/spsc.hpp>
#include <q/mpmc.hpp>
#include <future>
#include <QueueNadoMacros.h>
#include <limits>
namespace {
const int kNoWaitTimeMs = 0;
const int kWaitTimeMs = 500;
const int kLongWaitTimeMs = 1500;
}
std::atomic<int> RifleVampireTests::mShotsDeleted;
void TestDeleteString(void*, void* data) {
std::string* theString = reinterpret_cast<std::string*> (data);
RifleVampireTests::mShotsDeleted++; // yes this is threadsafe
delete theString;
}
/**
* Used to call shutdown just for test.
* @param location
*/
class TestVampire : public Vampire {
public:
TestVampire(std::string location) : Vampire(location) {
}
void Destroy() {
Vampire::Destroy();
}
};
/**
* Used to call shutdown just for test.
* @param location
*/
class TestRifle : public Rifle {
public:
TestRifle(std::string location) : Rifle(location) {
}
void Destroy() {
Rifle::Destroy();
}
};
std::string RifleVampireTests::GetTcpLocation() {
int max = 9000;
int min = 7000;
int port = (rand() % (max - min)) + min;
std::string tcpLocation("tcp://127.0.0.1:");
tcpLocation.append(std::to_string(port));
return tcpLocation;
}
std::string RifleVampireTests::GetIpcLocation() {
int pid = getpid();
std::string ipcLocation("ipc:///tmp/");
ipcLocation.append("RifleVampireTests");
ipcLocation.append(std::to_string(pid));
ipcLocation.append(".ipc");
return ipcLocation;
}
void RifleVampireTests::RifleThread(int numberOfMessages,
std::string& location, std::string& exampleData, int hwm,
int ioThreads, bool ownSocket) {
Rifle rifle(location);
rifle.SetHighWater(hwm);
rifle.SetIOThreads(ioThreads);
rifle.SetOwnSocket(ownSocket);
rifle.Aim();
std::stringstream ss;
TimeStats stats;
for (int i = 0; i < numberOfMessages; i++) {
TriggerTimeStats trigger(stats);
bool result = rifle.Fire(exampleData);
if (!result) {
trigger.Skip();
std::cout << "failed to fire" << std::endl;
}
// std::cout << "shot fired:" << result << std::endl;
// boost::this_thread::sleep(boost::posix_time::microseconds(sleepInterval));
}
//std::cout << "Done Shooting" << std::endl;
while (!zctx_interrupted) {
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
std::cout << "Rifle : " << stats.FlushAsString() << std::endl;
}
void RifleVampireTests::ShootStakeThread(int numberOfMessages,
std::string& location, std::vector<std::pair<void*, unsigned int> >& exampleData,
bool ownSocket) {
Rifle rifle(location);
rifle.SetHighWater(1);
rifle.SetIOThreads(1);
if (!ownSocket) {
rifle.SetOwnSocket(false);
} else {
rifle.SetOwnSocket(true);
}
rifle.Aim();
std::stringstream ss;
TimeStats stats;
for (int i = 0; i < numberOfMessages; i++) {
TriggerTimeStats trigger(stats);
bool result = rifle.FireStakes(exampleData);
if (!result) {
trigger.Skip();
std::cout << "failed to fire" << std::endl;
}
// std::cout << "shot fired:" << result << std::endl;
// boost::this_thread::sleep(boost::posix_time::microseconds(sleepInterval));
}
std::cout << "Rifle : " << stats.FlushAsString() << std::endl;
while (!zctx_interrupted) {
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
void RifleVampireTests::ShootZeroCopyThread(int numberOfMessages,
std::string& location, std::string& exampleData, int hwm, int ioThreads,
bool ownSocket) {
Rifle rifle(location);
rifle.SetHighWater(hwm);
rifle.SetIOThreads(ioThreads);
if (!ownSocket) {
rifle.SetOwnSocket(false);
} else {
rifle.SetOwnSocket(true);
}
rifle.Aim();
std::stringstream ss;
TimeStats stats;
for (int i = 0; i < numberOfMessages && !zctx_interrupted; i++) {
std::string* zero = new std::string(exampleData);
TriggerTimeStats trigger(stats);
bool result = rifle.FireZeroCopy(zero, zero->size(), TestDeleteString, 10000);
if (!result) {
trigger.Skip();
std::cout << "failed to fire" << std::endl;
}
// std::cout << "shot fired:" << result << std::endl;
// boost::this_thread::sleep(boost::posix_time::microseconds(sleepInterval));
}
std::cout << "Rifle : " << stats.FlushAsString() << std::endl;
while (!zctx_interrupted) {
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
/**
* Start a vampire thread and pull off the set number of messages.
* @param numberOfMessages
* @param location
* @param exampleData
* @param hwm
* @param ioThreads
* @param ownSocket
*/
void RifleVampireTests::VampireThread(int numberOfMessages,
std::string& location, std::string& exampleData, int hwm,
int ioThreads, bool ownSocket, int waitTimeMs) {
Vampire vampire(location);
vampire.SetHighWater(hwm);
vampire.SetIOThreads(ioThreads);
vampire.SetOwnSocket(ownSocket);
ASSERT_TRUE(vampire.PrepareToBeShot());
TimeStats stats;
for (int i = 0; i < numberOfMessages && !zctx_interrupted; i++) {
std::string bullet;
TriggerTimeStats trigger(stats);
if (vampire.GetShot(bullet, waitTimeMs)) {
EXPECT_EQ(bullet, exampleData);
} else {
trigger.Skip();
//no shot in time try again
i--;
}
}
std::cout << "Vampire : " << stats.FlushAsString() << std::endl;
while (!zctx_interrupted) {
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
void RifleVampireTests::StakeAVampireThread(int numberOfMessages,
std::string& location,
std::vector<std::pair<void*, unsigned int> >& exampleData,
int hwm, int ioThreads, int waitTimeMs) {
Vampire vampire(location);
vampire.SetHighWater(hwm);
vampire.SetIOThreads(ioThreads);
ASSERT_TRUE(vampire.PrepareToBeShot());
TimeStats stats;
for (int i = 0; i < numberOfMessages && !zctx_interrupted; i++) {
std::vector<std::pair<void*, unsigned int> > data;
TriggerTimeStats trigger(stats);
if (vampire.GetStakes(data, waitTimeMs)) {
auto it = data.begin();
auto jt = exampleData.begin();
for (; it != data.end() && !zctx_interrupted; it++, jt++) {
EXPECT_EQ(it->first, jt->first);
EXPECT_EQ(it->second, jt->second);
}
} else {
trigger.Skip();
//no shot in time try again
i--;
}
}
std::cout << "Vampire : " << stats.FlushAsString() << std::endl;
while (!zctx_interrupted) {
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
void RifleVampireTests::OneRifleNVampiresBenchmark(int nVampires, int nIOThreads,
int rifleHWM, int vampireHWM, std::string& location, int dataSize, int nShotsPerVampire, int expectedSpeed, int waitTimeMs) {
bool bRifleOwnSocket = true;
Rifle* rifle = new Rifle(location);
rifle->SetHighWater(rifleHWM);
rifle->SetIOThreads(nIOThreads);
rifle->SetOwnSocket(bRifleOwnSocket);
EXPECT_TRUE(rifle->Aim());
#if RIFLE_VAMPIRE_PRODUCTION == 0
delete rifle;
return;
#endif
std::string exampleData(dataSize, 'a');
std::vector<boost::thread*> theVampires;
for (int i = 0; i < nVampires && !zctx_interrupted; i++) {
boost::thread* aShooter = new boost::thread(
&RifleVampireTests::VampireThread, this, nShotsPerVampire, location,
exampleData, vampireHWM, nIOThreads, !bRifleOwnSocket, waitTimeMs);
theVampires.push_back(aShooter);
}
sleep(2);
//Send more then we can handle, break if we can't send any more.
int fullSize = (nShotsPerVampire * nVampires) + ((vampireHWM * (nVampires)) * 2) + rifleHWM;
SetExpectedTime(fullSize, exampleData.size() * sizeof (char), expectedSpeed, 20000L);
StartTimedSection();
TimeStats stats;
for (int i = 0; i < fullSize && !zctx_interrupted; i++) {
TriggerTimeStats trigger(stats);
if (!rifle->Fire(exampleData, waitTimeMs)) {
trigger.Skip();
std::cout << "Failed to fire... Vampires might all be dead..." << std::endl;
break;
}
}
std::cout << "One Rifle --> #" << nVampires << " Vampires: " << stats.FlushAsString() << std::endl;
for (auto it = theVampires.begin();
it != theVampires.end() && !zctx_interrupted; it++) {
(*it)->interrupt();
(*it)->join();
delete *it;
}
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
delete rifle;
}
template <typename Sender>
size_t Push(Sender q, int dataSize, int stop, std::atomic<bool>& stopRunning) {
using namespace std::chrono_literals;
std::vector<std::string> expected;
std::string exampleData(dataSize, 'a');
size_t pushed = 0;
using namespace std::chrono_literals;
for (auto i = 0; i < stop; ++i) {
std::string sendData = exampleData;
if (stopRunning.load()) {
return pushed;
}
while (false == q.push(sendData)) {
std::this_thread::sleep_for(1us); // // yield is too aggressive
if (stopRunning.load()) {
return pushed;
}
}
++pushed;
}
std::cout << q.mStats.FlushAsString() << std::endl;
return pushed;
}
template <typename Receiver>
size_t Get(Receiver q, int dataSize, int stop, std::atomic<bool>& stopRunning) {
using namespace std::chrono_literals;
std::vector<std::string> expected;
const std::string exampleData(dataSize, 'a');
size_t received = 0;
using namespace std::chrono_literals;
for (auto i = 0; i < stop; ++i) {
if (stopRunning.load()) {
return received;
}
std::string incoming;
while (false == q.pop(incoming)) {
std::this_thread::sleep_for(1us); // // yield is too aggressive
if (stopRunning.load()) {
return received;
}
}
++received;
if (!stopRunning.load()) {
EXPECT_EQ(exampleData, incoming);
}
}
std::cout << q.mStats.FlushAsString() << std::endl;
return received;
}
void RifleVampireTests::QueueSPSCBenchmark(int dataSize, int nHowMany, int expectedSpeed) {
const size_t kQueueSize = 100;
auto queue = QAPI::CreateQueue<spsc::flexible::circular_fifo<std::string>>(kQueueSize);
const std::string exampleData(dataSize, 'a');
SetExpectedTime(nHowMany, exampleData.size() * sizeof (char), expectedSpeed, 20000L);
auto producer = std::get <QAPI::index::sender>(queue);
auto consumer = std::get <QAPI::index::receiver>(queue);
StartTimedSection();
std::atomic<bool> stopRunning{false};
auto sentResult = std::async(std::launch::async, Push<decltype(producer)>,
producer, dataSize, nHowMany, std::ref(stopRunning));
auto receivedResult = std::async(std::launch::async, Get<decltype(consumer)>,
consumer, dataSize, nHowMany, std::ref(stopRunning));
auto sent = sentResult.get();
auto received = receivedResult.get();
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
EXPECT_GE(received + kQueueSize, sent);
}
void RifleVampireTests::QueueMPMCBenchmark(int numSenders, int numReceivers, int dataSize, int nHowMany, int expectedSpeed) {
const size_t kQueueSize = 100;
auto queue = QAPI::CreateQueue<mpmc::flexible_lock_queue<std::string>>(kQueueSize);
const std::string exampleData(dataSize, 'a');
SetExpectedTime(nHowMany, exampleData.size() * sizeof (char), expectedSpeed, 20000L);
auto producer = std::get <QAPI::index::sender>(queue);
auto consumer = std::get <QAPI::index::receiver>(queue);
StartTimedSection();
std::vector<std::future<size_t>> sentResult;
sentResult.reserve(numSenders);
std::vector<std::future<size_t>> receiveResult;
receiveResult.reserve(numReceivers);
std::atomic<bool> stopRunning{false};
for (int i = 0; i < numReceivers; ++i) {
receiveResult.emplace_back(std::async(std::launch::async, Get<decltype(consumer)>,
consumer, dataSize, nHowMany, std::ref(stopRunning)));
}
for (int i = 0; i < numSenders; ++i) {
sentResult.emplace_back(std::async(std::launch::async, Push<decltype(producer)>,
producer, dataSize, nHowMany, std::ref(stopRunning)));
}
StopWatch timecheck;
size_t totalSent = 0;
for (int i = 0; i < numSenders; ++i) {
totalSent = sentResult[i].get();
}
stopRunning.store(true);
size_t totalReceived = 0;
for (int i = 0; i < numSenders; ++i) {
totalReceived = receiveResult[i].get();
}
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
std::cout << "Actual throughput: " << totalReceived * dataSize << " bytes" << std::endl;
std::cout << "Total time elapsed: " << timecheck.ElapsedSec() << std::endl;
std::cout << "Transaction/second: " << totalReceived/timecheck.ElapsedSec() << std::endl;
std::cout << "Transaction speed: " << ((totalReceived * dataSize / 1000)/timecheck.ElapsedSec()) << " Mbps" << std::endl;
EXPECT_GE(totalReceived + kQueueSize, totalSent);
}
void RifleVampireTests::QueueMPMCBenchmark_1Minute(int numSenders, int numReceivers, int dataSize, int nHowMany, int expectedSpeed) {
const size_t kQueueSize = 100;
auto queue = QAPI::CreateQueue<mpmc::flexible_lock_queue<std::string>>(kQueueSize);
const std::string exampleData(dataSize, 'a');
SetExpectedTime(nHowMany, exampleData.size() * sizeof (char), expectedSpeed, 20000L);
auto producer = std::get <QAPI::index::sender>(queue);
auto consumer = std::get <QAPI::index::receiver>(queue);
StartTimedSection();
std::vector<std::future<size_t>> sentResult;
sentResult.reserve(numSenders);
std::vector<std::future<size_t>> receiveResult;
receiveResult.reserve(numReceivers);
std::atomic<bool> stopRunning{false};
for (int i = 0; i < numReceivers; ++i) {
receiveResult.emplace_back(std::async(std::launch::async, Get<decltype(consumer)>,
consumer, dataSize, nHowMany, std::ref(stopRunning)));
}
for (int i = 0; i < numSenders; ++i) {
sentResult.emplace_back(std::async(std::launch::async, Push<decltype(producer)>,
producer, dataSize, nHowMany, std::ref(stopRunning)));
}
StopWatch timecheck;
using namespace std::chrono_literals;
while(timecheck.ElapsedSec() < 60) {
std::this_thread::sleep_for(10ms);
}
stopRunning.store(true);
size_t totalSent = 0;
for (int i = 0; i < numSenders; ++i) {
totalSent = sentResult[i].get();
}
stopRunning.store(true);
size_t totalReceived = 0;
for (int i = 0; i < numSenders; ++i) {
totalReceived = receiveResult[i].get();
}
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
std::cout << "Actual throughput: " << ((totalReceived * dataSize)/(1024 * 1024)) << " MBytes" << std::endl;
std::cout << "Total time elapsed: " << timecheck.ElapsedSec() << std::endl;
std::cout << "Transaction/second: " << totalReceived/timecheck.ElapsedSec() << std::endl;
std::cout << "Transaction speed: " << ((totalReceived * dataSize / (1024 * 1024))/timecheck.ElapsedSec()) << " Mbyte/s" << std::endl;
EXPECT_GE(totalReceived + kQueueSize, totalSent);
}
void RifleVampireTests::OneRifleNVampiresStakeBenchmark(int nVampires, int nIOThreads,
int rifleHWM, int vampireHWM, std::string& location, int dataSize, int nShotsPerVampire, int expectedSpeed, int waitTimeMs) {
Rifle* rifle = new Rifle(location);
rifle->SetHighWater(rifleHWM);
rifle->SetIOThreads(nIOThreads);
rifle->SetOwnSocket(true);
EXPECT_TRUE(rifle->Aim());
#if RIFLE_VAMPIRE_PRODUCTION == 0
delete rifle;
return;
#endif
std::string exampleString(dataSize, 'a');
std::vector<std::pair< void*, unsigned int> > exampleData;
for (int i = 0; i < SIZE_OF_STAKE_BUNDLE && !zctx_interrupted; i++) {
exampleData.push_back(make_pair(&exampleString, 1234));
}
std::vector<boost::thread*> theVampires;
for (int i = 0; i < nVampires && !zctx_interrupted; i++) {
boost::thread* aShooter = new boost::thread(&RifleVampireTests::StakeAVampireThread,
this, nShotsPerVampire, location, exampleData, vampireHWM, nIOThreads, waitTimeMs);
theVampires.push_back(aShooter);
}
sleep(2);
//Send more then we can handle, break if we can't send any more.
int fullSize = (nShotsPerVampire * nVampires) + ((vampireHWM * (nVampires)) * 2) + rifleHWM;
SetExpectedTime(fullSize, exampleData.size() * sizeof (char) * SIZE_OF_STAKE_BUNDLE, expectedSpeed, 20000L);
StartTimedSection();
TimeStats stats;
for (int i = 0; i < fullSize && !zctx_interrupted; i++) {
TriggerTimeStats trigger(stats);
if (!rifle->FireStakes(exampleData, waitTimeMs)) {
std::cout << "Failed to fire at shot " << i << " of fullSize " << fullSize << std::endl;
std::cout << " ... Vampires might all be dead..." << std::endl;
trigger.Skip();
break;
}
}
std::cout << "One Rifle --> #" << nVampires << " Vampires. " << stats.FlushAsString() << std::endl;
for (auto it = theVampires.begin();
it != theVampires.end() && !zctx_interrupted; it++) {
(*it)->interrupt();
(*it)->join();
delete *it;
}
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
delete rifle;
}
void RifleVampireTests::NRiflesOneVampireBenchmarkZeroCopy(int nRifles, int nIOThreads,
int rifleHWM, int vampireHWM, std::string& location, int dataSize,
int nShotsPerRifle, int expectedSpeed, int waitTimeMs) {
bool bVampireOwnSocket = true;
mShotsDeleted.store(0);
Vampire vampire(location);
vampire.SetHighWater(vampireHWM);
vampire.SetIOThreads(nIOThreads);
vampire.SetOwnSocket(bVampireOwnSocket);
ASSERT_TRUE(vampire.PrepareToBeShot());
#if RIFLE_VAMPIRE_PRODUCTION == 0
return;
#endif
std::string exampleData(dataSize, 'z');
std::vector<boost::thread*> theRifles;
for (int i = 0; i < nRifles && !zctx_interrupted; i++) {
boost::thread* aShooter = new boost::thread(
&RifleVampireTests::ShootZeroCopyThread, this, nShotsPerRifle, location,
exampleData, rifleHWM, nIOThreads, !bVampireOwnSocket);
theRifles.push_back(aShooter);
}
SetExpectedTime((nShotsPerRifle * nRifles), // numberOfPackets
(exampleData.size() * sizeof (char)), // packetSize
expectedSpeed, // RateInMbps
20000L); //transationsPerSection
StartTimedSection();
std::string bullet;
TimeStats stats;
for (int pktCount = 0; pktCount < (nShotsPerRifle * nRifles) && !zctx_interrupted; pktCount++) {
TriggerTimeStats trigger(stats);
if (vampire.GetShot(bullet, waitTimeMs)) {
EXPECT_EQ(bullet, exampleData);
} else {
trigger.Skip();
pktCount--; // Packet not received in time, try again.
}
}
std::cout << "#" << nRifles << " Rifles --> one Vampire. " << stats.FlushAsString() << std::endl;
for (auto it = theRifles.begin();
it != theRifles.end() && !zctx_interrupted; it++) {
(*it)->interrupt();
(*it)->join();
delete *it;
}
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
EXPECT_EQ(nShotsPerRifle * nRifles, mShotsDeleted);
}
void RifleVampireTests::NRiflesOneVampireBenchmark(int nRifles, int nIOThreads,
int rifleHWM, int vampireHWM, std::string& location, int dataSize,
int nShotsPerRifle, int expectedSpeed, int waitTimeMs) {
bool bVampireOwnSocket = true;
Vampire vampire(location);
vampire.SetHighWater(vampireHWM);
vampire.SetIOThreads(nIOThreads);
vampire.SetOwnSocket(bVampireOwnSocket);
ASSERT_TRUE(vampire.PrepareToBeShot());
#if RIFLE_VAMPIRE_PRODUCTION == 0
return;
#endif
std::string exampleData(dataSize, 'z');
std::vector<boost::thread*> theRifles;
for (int i = 0; i < nRifles && !zctx_interrupted; i++) {
boost::thread* aShooter = new boost::thread(
&RifleVampireTests::RifleThread, this, nShotsPerRifle, location,
exampleData, rifleHWM, nIOThreads, !bVampireOwnSocket);
theRifles.push_back(aShooter);
}
SetExpectedTime((nShotsPerRifle * nRifles), // numberOfPackets
(exampleData.size() * sizeof (char)), // packetSize
expectedSpeed, // RateInMbps
20000L); //transationsPerSection
StartTimedSection();
std::string bullet;
TimeStats stats;
for (int pktCount = 0; pktCount < (nShotsPerRifle * nRifles) && !zctx_interrupted; pktCount++) {
TriggerTimeStats trigger(stats);
if (vampire.GetShot(bullet, waitTimeMs)) {
EXPECT_EQ(bullet, exampleData);
} else {
pktCount--; // Packet not received in time, try again.
trigger.Skip();
}
}
std::cout << "#" << nRifles << " Rifles --> one Vampire. " << stats.FlushAsString() << std::endl;
for (auto it = theRifles.begin();
it != theRifles.end() && !zctx_interrupted; it++) {
(*it)->interrupt();
(*it)->join();
delete *it;
}
EndTimedSection();
EXPECT_TRUE(TimedSectionPassed());
}
TEST_F(RifleVampireTests, ipcFilesCleanedOnNormalExitRifleOwner) {
std::string target("ipc:///rifleVampireExit");
std::string addressRealPath(target, target.find("ipc://") + 6);
{
Rifle stick{target};
{
Vampire vamp{target};
ASSERT_TRUE(stick.Aim());
ASSERT_TRUE(vamp.PrepareToBeShot());
ASSERT_TRUE(FileIO::DoesFileExist(addressRealPath));
}
// Vampire falls out of scope and ipc file is not deleted
ASSERT_TRUE(FileIO::DoesFileExist(addressRealPath)) << "ipc file unexpectedly deleted: " << addressRealPath;
}
// Rifle falls out of scope and ipc file is cleaned up
ASSERT_FALSE(FileIO::DoesFileExist(addressRealPath)) << "Rifle did not clean up ipc file: " << addressRealPath;
}
TEST_F(RifleVampireTests, ipcFilesCleanedOnNormalExitVampireOwner) {
std::string target("ipc:///rifleVampireExit");
std::string addressRealPath(target, target.find("ipc://") + 6);
{
Vampire vamp{target};
vamp.SetOwnSocket(true);
{
Rifle stick{target};
stick.SetOwnSocket(false);
ASSERT_TRUE(stick.Aim());
ASSERT_TRUE(vamp.PrepareToBeShot());
ASSERT_TRUE(FileIO::DoesFileExist(addressRealPath));
}
// Rifle falls out of scope and ipc file is not deleted
ASSERT_TRUE(FileIO::DoesFileExist(addressRealPath)) << "ipc file unexpectedly deleted: " << addressRealPath;
}
// Vampire falls out of scope and ipc file is cleaned up
ASSERT_FALSE(FileIO::DoesFileExist(addressRealPath)) << "Vampire did not clean up ipc file: " << addressRealPath;
}
TEST_F(RifleVampireTests, ipcFilesCleanedOnFatal) {
std::string target("ipc:///rifleVampireDeath");
Rifle stick{target};
Vampire vamp{target};
std::string addressRealPath(target, target.find("ipc://") + 6);
Death::SetupExitHandler();
ASSERT_TRUE(stick.Aim());
ASSERT_TRUE(vamp.PrepareToBeShot());
ASSERT_TRUE(FileIO::DoesFileExist(addressRealPath));
CHECK(false);
ASSERT_FALSE(FileIO::DoesFileExist(addressRealPath));
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleOneVampireIPCLargeSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 1;
int nIOThreads = 1;
int rifleHWM = 1000;
int vampireHWM = 500;
int dataSize = 56554;
int nShotsPerVampire = 200000;
int expectedSpeed = 1000;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleOneVampireIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 1;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
#if 0
/**
*
* The Rifle Vampire performance tests are NOT measuring correctly how much the throughput is and what the actual
* time it took to process them. For that reason all the performance tests are disabled with #if 0
*
* For the time being they are being left in case we want to revisit similar performance tests later for the new
* queues that we are doing.
*
* At that time these obsolete tests should be replaced and deleted
*/
TEST_F(RifleVampireTests, SPSC) {
int dataSize = 100;
int howMany = 1000000;
int expectedSpeed = 50;
QueueSPSCBenchmark(dataSize, howMany, expectedSpeed);
}
TEST_F(RifleVampireTests, SPSCWithLargeTransfer) {
int dataSize = 65554;
int howMany = 200000;
int expectedSpeed = 1000;
QueueSPSCBenchmark(dataSize, howMany, expectedSpeed);
}
TEST_F(RifleVampireTests, OneRifleOneVampirePointers) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 1;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresStakeBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kLongWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketOneRifleOneVampireIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nRifles = 1;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 100000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmark(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketOneRifleOneVampireIPCSmallSizeZeroCopy) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nRifles = 1;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 100000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmarkZeroCopy(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleTwoVampiresIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 2;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, OneRifleTwoVampiresPointers) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 2;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresStakeBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kLongWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketTwoRiflesOneVampireIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nRifles = 2;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 100000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmark(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketTwoRiflesOneVampireIPCSmallSizeZeroCopy) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nRifles = 2;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 100000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmarkZeroCopy(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleThreeVampiresIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 3;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, OneRifleThreeVampiresPointers) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 3;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresStakeBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kLongWaitTimeMs);
}
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleFourVampiresIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 4;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, OneRifleFourVampiresPointers) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nVampires = 4;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 1000000;
int expectedSpeed = 50;
OneRifleNVampiresStakeBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kLongWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketFiveRiflesOneVampireIPCSmallSize) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nRifles = 5;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 100000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmark(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketFiveRiflesOneVampireIPCSmallSizeZeroCopy) {
if (geteuid() == 0) {
std::string location = GetIpcLocation();
int nRifles = 5;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 100000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmarkZeroCopy(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleOneVampireTCPSmallSize) {
if (geteuid() == 0) {
std::string location = GetTcpLocation();
int nVampires = 1;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 3500000;
int expectedSpeed = 50;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketOneRifleOneVampireTCPSmallSize) {
if (geteuid() == 0) {
std::string location = GetTcpLocation();
int nRifles = 1;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 350000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmark(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketOneRifleOneVampireTCPSmallSizeZeroCopy) {
if (geteuid() == 0) {
std::string location = GetTcpLocation();
int nRifles = 1;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 350000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmarkZeroCopy(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, RifleOwnsSocketOneRifleTwoVampiresTCPSmallSize) {
if (geteuid() == 0) {
std::string location = GetTcpLocation();
int nVampires = 2;
int nIOThreads = 1;
int rifleHWM = 120000;
int vampireHWM = 30000;
int dataSize = 100;
int nShotsPerVampire = 2000000;
int expectedSpeed = 50;
OneRifleNVampiresBenchmark(nVampires, nIOThreads, rifleHWM, vampireHWM, location, dataSize, nShotsPerVampire, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketTwoRiflesOneVampireTCPSmallSize) {
if (geteuid() == 0) {
std::string location = GetTcpLocation();
int nRifles = 2;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 350000;
int expectedSpeed = 50;
NRiflesOneVampireBenchmark(nRifles, nIOThreads, rifleHWM, vampireHWM,
location, dataSize, nShotsPerRifle, expectedSpeed, kWaitTimeMs);
}
}
TEST_F(RifleVampireTests, VampireOwnsSocketTwoRiflesOneVampireTCPSmallSizeZeroCopy) {
if (geteuid() == 0) {
std::string location = GetTcpLocation();
int nRifles = 2;
int nIOThreads = 1;
int rifleHWM = 30000;
int vampireHWM = 120000;
int dataSize = 100;
int nShotsPerRifle = 350000;
int expectedSpeed = 50;