mkql_vector_spiller_adapter_ut.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "mkql_vector_spiller_adapter.h"
  2. #include "mock_spiller_ut.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. namespace NKikimr::NMiniKQL {
  5. namespace {
  6. template <typename T>
  7. std::vector<T, TMKQLAllocator<T>> CreateSimpleVectorOfSize(size_t size) {
  8. std::vector<T, TMKQLAllocator<T>> v;
  9. v.reserve(size);
  10. for (size_t i = 0; i < size; ++i) {
  11. v.push_back(i);
  12. }
  13. return v;
  14. }
  15. template <typename T>
  16. void SaveRestoreAndCompareVectors(const std::vector<std::vector<T, TMKQLAllocator<T>>>& vectors, size_t spillerChunkSizeInBytes) {
  17. auto spiller = TVectorSpillerAdapter<T, TMKQLAllocator<T>>(CreateMockSpiller(), spillerChunkSizeInBytes);
  18. for (const auto& vec : vectors) {
  19. auto copiedVector = vec;
  20. spiller.AddData(std::move(copiedVector));
  21. while (!spiller.IsAcceptingData()) {
  22. spiller.Update();
  23. }
  24. }
  25. spiller.Finalize();
  26. while (!spiller.IsAcceptingDataRequests()) {
  27. spiller.Update();
  28. }
  29. for (const auto& vec : vectors) {
  30. spiller.RequestNextVector();
  31. while (!spiller.IsDataReady()) {
  32. spiller.Update();
  33. }
  34. auto extractedVector = spiller.ExtractVector();
  35. UNIT_ASSERT_VALUES_EQUAL(vec, extractedVector);
  36. }
  37. }
  38. template <typename T>
  39. void RunTestForSingleVector(size_t vectorSize, size_t chunkSize, bool sizeInBytes) {
  40. std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
  41. size_t chunkSizeInBytes = sizeInBytes ? chunkSize : chunkSize * sizeof(T);
  42. SaveRestoreAndCompareVectors<T>({v}, chunkSizeInBytes);
  43. }
  44. }
  45. Y_UNIT_TEST_SUITE(TVectorSpillerAdapterTest_SingleVector) {
  46. Y_UNIT_TEST(VectorOfExactChunkSize) {
  47. TScopedAlloc Alloc(__LOCATION__);
  48. size_t vectorSize = 5;
  49. RunTestForSingleVector<int>(vectorSize, vectorSize, false);
  50. RunTestForSingleVector<char>(vectorSize, vectorSize, false);
  51. }
  52. Y_UNIT_TEST(VectorLargerThanChunkSize) {
  53. TScopedAlloc Alloc(__LOCATION__);
  54. size_t vectorSize = 10;
  55. size_t chunkSize = 3;
  56. RunTestForSingleVector<int>(vectorSize, chunkSize, false);
  57. RunTestForSingleVector<char>(vectorSize, chunkSize, false);
  58. }
  59. Y_UNIT_TEST(VectorLargerThanChunkSizePrime) {
  60. TScopedAlloc Alloc(__LOCATION__);
  61. size_t vectorSize = 10;
  62. size_t chunkSizeBytes = 7;
  63. RunTestForSingleVector<int>(vectorSize, chunkSizeBytes, true);
  64. RunTestForSingleVector<char>(vectorSize, chunkSizeBytes, true);
  65. }
  66. Y_UNIT_TEST(VectorLessThanChunkSize) {
  67. TScopedAlloc Alloc(__LOCATION__);
  68. size_t vectorSize = 5;
  69. size_t chunkSize = 10;
  70. RunTestForSingleVector<int>(vectorSize, chunkSize, false);
  71. RunTestForSingleVector<char>(vectorSize, chunkSize, false);
  72. }
  73. }
  74. Y_UNIT_TEST_SUITE(TVectorSpillerAdapterTest_MultipleVectors) {
  75. template <typename T>
  76. void ManyDifferentSizes_TestRunner() {
  77. std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
  78. for (int vectorSize = 0; vectorSize <= 100; ++vectorSize) {
  79. vectors.push_back(CreateSimpleVectorOfSize<T>(vectorSize));
  80. }
  81. SaveRestoreAndCompareVectors<T>(vectors, 20);
  82. }
  83. Y_UNIT_TEST(ManyDifferentSizes) {
  84. TScopedAlloc Alloc(__LOCATION__);
  85. ManyDifferentSizes_TestRunner<int>();
  86. ManyDifferentSizes_TestRunner<char>();
  87. }
  88. template <typename T>
  89. void ManyDifferentSizesReversed_TestRunner() {
  90. std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
  91. for (int vectorSize = 100; vectorSize >= 0; --vectorSize) {
  92. vectors.push_back(CreateSimpleVectorOfSize<T>(vectorSize));
  93. }
  94. SaveRestoreAndCompareVectors<T>(vectors, 20);
  95. }
  96. Y_UNIT_TEST(ManyDifferentSizesReversed) {
  97. TScopedAlloc Alloc(__LOCATION__);
  98. ManyDifferentSizesReversed_TestRunner<int>();
  99. ManyDifferentSizesReversed_TestRunner<char>();
  100. }
  101. template <typename T>
  102. void VectorsInOneChunk_TestRunner() {
  103. std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
  104. size_t totalSize = 0;
  105. for (int vectorSize = 1; vectorSize < 5; ++vectorSize) {
  106. std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
  107. totalSize += vectorSize;
  108. vectors.push_back(v);
  109. }
  110. SaveRestoreAndCompareVectors<T>(vectors, totalSize * sizeof(int) + 10);
  111. }
  112. Y_UNIT_TEST(VectorsInOneChunk) {
  113. TScopedAlloc Alloc(__LOCATION__);
  114. VectorsInOneChunk_TestRunner<int>();
  115. VectorsInOneChunk_TestRunner<char>();
  116. }
  117. template <typename T>
  118. void EmptyVectorsInTheMiddle_TestRunner() {
  119. std::vector<std::vector<T,TMKQLAllocator<T>>> vectors;
  120. size_t totalSize = 0;
  121. for (int vectorSize = 1; vectorSize < 5; ++vectorSize) {
  122. std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
  123. totalSize += vectorSize;
  124. vectors.push_back(v);
  125. }
  126. vectors.push_back({});
  127. vectors.push_back({});
  128. for (int vectorSize = 1; vectorSize < 5; ++vectorSize) {
  129. std::vector v = CreateSimpleVectorOfSize<T>(vectorSize);
  130. totalSize += vectorSize;
  131. vectors.push_back(v);
  132. }
  133. SaveRestoreAndCompareVectors<T>(vectors, totalSize * sizeof(T) + 10);
  134. }
  135. Y_UNIT_TEST(EmptyVectorsInTheMiddle) {
  136. TScopedAlloc Alloc(__LOCATION__);
  137. EmptyVectorsInTheMiddle_TestRunner<int>();
  138. EmptyVectorsInTheMiddle_TestRunner<char>();
  139. }
  140. template <typename T>
  141. void RequestedVectorPartlyInMemory_TestRunner() {
  142. std::vector<std::vector<T, TMKQLAllocator<T>>> vectors;
  143. std::vector<T, TMKQLAllocator<T>> small = CreateSimpleVectorOfSize<T>(1);
  144. std::vector<T, TMKQLAllocator<T>> big = CreateSimpleVectorOfSize<T>(10);
  145. vectors.push_back(small);
  146. vectors.push_back(big);
  147. // small vector will also load most of big vector to memory
  148. size_t chunkSizeBytes = (big.size() - small.size()) * sizeof(T);
  149. SaveRestoreAndCompareVectors<T>(vectors, chunkSizeBytes);
  150. }
  151. Y_UNIT_TEST(RequestedVectorPartlyInMemory) {
  152. TScopedAlloc Alloc(__LOCATION__);
  153. RequestedVectorPartlyInMemory_TestRunner<int>();
  154. RequestedVectorPartlyInMemory_TestRunner<char>();
  155. }
  156. }
  157. } //namespace namespace NKikimr::NMiniKQL