unordered_ut.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <util/system/thread.h>
  3. #include <algorithm>
  4. #include <util/generic/vector.h>
  5. #include <util/random/fast.h>
  6. #include "ut_helpers.h"
  7. template <typename TQueueType>
  8. class TTestUnorderedQueue: public TTestBase {
  9. private:
  10. using TLink = TIntrusiveLink;
  11. UNIT_TEST_SUITE_DEMANGLE(TTestUnorderedQueue<TQueueType>);
  12. UNIT_TEST(Push1M_Pop1M_Unordered)
  13. UNIT_TEST_SUITE_END();
  14. public:
  15. void Push1M_Pop1M_Unordered() {
  16. constexpr int REPEAT = 1000000;
  17. TQueueType queue;
  18. TLink msg[REPEAT];
  19. auto pmsg = queue.Pop();
  20. UNIT_ASSERT_VALUES_EQUAL(pmsg, nullptr);
  21. for (int i = 0; i < REPEAT; ++i) {
  22. queue.Push(&msg[i]);
  23. }
  24. TVector<TLink*> popped;
  25. popped.reserve(REPEAT);
  26. for (int i = 0; i < REPEAT; ++i) {
  27. popped.push_back((TLink*)queue.Pop());
  28. }
  29. pmsg = queue.Pop();
  30. UNIT_ASSERT_VALUES_EQUAL(pmsg, nullptr);
  31. std::sort(popped.begin(), popped.end());
  32. for (int i = 0; i < REPEAT; ++i) {
  33. UNIT_ASSERT_VALUES_EQUAL(&msg[i], popped[i]);
  34. }
  35. }
  36. };
  37. template <typename TQueueType>
  38. class TTestWeakQueue: public TTestBase {
  39. private:
  40. UNIT_TEST_SUITE_DEMANGLE(TTestWeakQueue<TQueueType>);
  41. UNIT_TEST(Threads8_Rnd_Exchange)
  42. UNIT_TEST_SUITE_END();
  43. public:
  44. template <ui16 COUNT = 48, ui32 MSG_COUNT = 10000>
  45. void ManyThreadsRndExchange() {
  46. TQueueType queues[COUNT];
  47. class TWorker: public ISimpleThread {
  48. public:
  49. TWorker(
  50. TQueueType* queues_,
  51. ui16 mine,
  52. TAtomic* pushDone)
  53. : Queues(queues_)
  54. , MineQueue(mine)
  55. , PushDone(pushDone)
  56. {
  57. }
  58. TQueueType* Queues;
  59. ui16 MineQueue;
  60. TVector<uintptr_t> Received;
  61. TAtomic* PushDone;
  62. void* ThreadProc() override {
  63. TReallyFastRng32 rng(GetCycleCount());
  64. Received.reserve(MSG_COUNT * 2);
  65. for (ui32 loop = 1; loop <= MSG_COUNT; ++loop) {
  66. for (;;) {
  67. auto msg = Queues[MineQueue].Pop();
  68. if (msg == nullptr) {
  69. break;
  70. }
  71. Received.push_back((uintptr_t)msg);
  72. }
  73. ui16 rnd = rng.GenRand64() % COUNT;
  74. ui64 msg = ((ui64)MineQueue << 32) + loop;
  75. while (!Queues[rnd].Push((void*)msg)) {
  76. }
  77. }
  78. AtomicIncrement(*PushDone);
  79. for (;;) {
  80. bool isItLast = AtomicGet(*PushDone) == COUNT;
  81. auto msg = Queues[MineQueue].Pop();
  82. if (msg != nullptr) {
  83. Received.push_back((uintptr_t)msg);
  84. } else {
  85. if (isItLast) {
  86. break;
  87. }
  88. SpinLockPause();
  89. }
  90. }
  91. for (ui64 last = 0;;) {
  92. auto msg = Queues[MineQueue].UnsafeScanningPop(&last);
  93. if (msg == nullptr) {
  94. break;
  95. }
  96. Received.push_back((uintptr_t)msg);
  97. }
  98. return nullptr;
  99. }
  100. };
  101. TVector<TAutoPtr<TWorker>> workers;
  102. TAtomic pushDone = 0;
  103. for (ui32 i = 0; i < COUNT; ++i) {
  104. workers.emplace_back(new TWorker(&queues[0], i, &pushDone));
  105. workers.back()->Start();
  106. }
  107. TVector<uintptr_t> all;
  108. for (ui32 i = 0; i < COUNT; ++i) {
  109. workers[i]->Join();
  110. all.insert(all.begin(),
  111. workers[i]->Received.begin(), workers[i]->Received.end());
  112. }
  113. std::sort(all.begin(), all.end());
  114. auto iter = all.begin();
  115. for (ui32 i = 0; i < COUNT; ++i) {
  116. for (ui32 k = 1; k <= MSG_COUNT; ++k) {
  117. UNIT_ASSERT_VALUES_EQUAL(((ui64)i << 32) + k, *iter);
  118. ++iter;
  119. }
  120. }
  121. }
  122. void Threads8_Rnd_Exchange() {
  123. ManyThreadsRndExchange<8>();
  124. }
  125. };
  126. REGISTER_TESTS_FOR_ALL_UNORDERED_QUEUES(TTestUnorderedQueue);
  127. UNIT_TEST_SUITE_REGISTRATION(TTestWeakQueue<TMPMCUnorderedRing>);