You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue_test.cc 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "gtest/gtest.h"
  18. #include "minddata/dataset/util/task_manager.h"
  19. #include "minddata/dataset/util/queue.h"
  20. #include <atomic>
  21. #include <chrono>
  22. #include <random>
  23. #include "utils/log_adapter.h"
  24. using namespace mindspore::dataset;
  25. using mindspore::LogStream;
  26. using mindspore::ExceptionType::NoExceptionType;
  27. using mindspore::MsLogLevel::INFO;
  28. class MindDataTestQueue : public UT::Common {
  29. public:
  30. MindDataTestQueue() {}
  31. void SetUp() {}
  32. };
  33. int gRefCountDestructorCalled;
  34. class RefCount {
  35. public:
  36. RefCount() : v_(nullptr) {}
  37. explicit RefCount(int x) : v_(std::make_shared<int>(x)) {}
  38. RefCount(const RefCount &o) : v_(o.v_) {}
  39. ~RefCount() {
  40. MS_LOG(DEBUG) << "Destructor of RefCount called" << std::endl;
  41. gRefCountDestructorCalled++;
  42. }
  43. RefCount &operator=(const RefCount &o) {
  44. v_ = o.v_;
  45. return *this;
  46. }
  47. std::shared_ptr<int> v_;
  48. };
  49. TEST_F(MindDataTestQueue, Test1) {
  50. // Passing shared pointer along the queue
  51. Queue<std::shared_ptr<int>> que(3);
  52. std::shared_ptr<int> a = std::make_shared<int>(20);
  53. Status rc = que.Add(a);
  54. ASSERT_TRUE(rc.IsOk());
  55. // Use count should be 2 right now. a plus the one in the queue.
  56. ASSERT_EQ(a.use_count(), 2);
  57. std::shared_ptr<int> b;
  58. rc = que.PopFront(&b);
  59. ASSERT_TRUE(rc.IsOk());
  60. ASSERT_EQ(*b, 20);
  61. // Use count should remain 2. a and b. No copy in the queue.
  62. ASSERT_EQ(a.use_count(), 2);
  63. a.reset(new int(5));
  64. ASSERT_EQ(a.use_count(), 1);
  65. // Push again but expect a is nullptr after push
  66. rc = que.Add(std::move(a));
  67. ASSERT_TRUE(rc.IsOk());
  68. ASSERT_EQ(a.use_count(), 0);
  69. rc = que.PopFront(&b);
  70. ASSERT_TRUE(rc.IsOk());
  71. ASSERT_EQ(*b, 5);
  72. ASSERT_EQ(b.use_count(), 1);
  73. // Test construct in place
  74. rc = que.EmplaceBack(std::make_shared<int>(100));
  75. ASSERT_TRUE(rc.IsOk());
  76. rc = que.PopFront(&b);
  77. ASSERT_TRUE(rc.IsOk());
  78. ASSERT_EQ(*b, 100);
  79. ASSERT_EQ(b.use_count(), 1);
  80. // Test the destructor of the Queue by add an element in the queue without popping it and let the queue go
  81. // out of scope.
  82. rc = que.EmplaceBack(std::make_shared<int>(2000));
  83. ASSERT_TRUE(rc.IsOk());
  84. }
  85. TEST_F(MindDataTestQueue, Test2) {
  86. // Passing status object
  87. Queue<Status> que(3);
  88. Status rc_send(StatusCode::kUnexpectedError, __LINE__, __FILE__, "Oops");
  89. Status rc = que.Add(rc_send);
  90. ASSERT_TRUE(rc.IsOk());
  91. Status rc_recv;
  92. rc = que.PopFront(&rc_recv);
  93. ASSERT_TRUE(rc.IsOk());
  94. ASSERT_EQ(rc_recv, rc_send);
  95. rc = que.EmplaceBack(StatusCode::kOutOfMemory, "Test emplace");
  96. ASSERT_TRUE(rc.IsOk());
  97. Status rc_recv2;
  98. rc = que.PopFront(&rc_recv2);
  99. ASSERT_TRUE(rc.IsOk());
  100. ASSERT_TRUE(rc_recv2.IsOutofMemory());
  101. }
  102. TEST_F(MindDataTestQueue, Test3) {
  103. Queue<std::unique_ptr<int>> que(3);
  104. std::unique_ptr<int> a(new int(3));
  105. Status rc = que.Add(std::move(a));
  106. ASSERT_TRUE(rc.IsOk());
  107. ASSERT_EQ(a.get(), nullptr);
  108. std::unique_ptr<int> b;
  109. rc = que.PopFront(&b);
  110. ASSERT_TRUE(rc.IsOk());
  111. ASSERT_EQ(*b, 3);
  112. rc = que.EmplaceBack(new int(40));
  113. ASSERT_TRUE(rc.IsOk());
  114. rc = que.PopFront(&b);
  115. ASSERT_TRUE(rc.IsOk());
  116. ASSERT_EQ(*b, 40);
  117. }
  118. void test4() {
  119. gRefCountDestructorCalled = 0;
  120. // Pass a structure along the queue.
  121. Queue<RefCount> que(3);
  122. RefCount a(3);
  123. Status rc = que.Add(a);
  124. ASSERT_TRUE(rc.IsOk());
  125. RefCount b;
  126. rc = que.PopFront(&b);
  127. ASSERT_TRUE(rc.IsOk());
  128. ASSERT_EQ(b.v_.use_count(), 2);
  129. ASSERT_EQ(*(b.v_.get()), 3);
  130. // Test the destructor of the Queue by adding an element without popping.
  131. rc = que.EmplaceBack(10);
  132. ASSERT_TRUE(rc.IsOk());
  133. }
  134. TEST_F(MindDataTestQueue, Test4) { test4(); }
  135. TEST_F(MindDataTestQueue, Test5) {
  136. test4();
  137. // Assume we have run Test4. The destructor of the RefCount should be called 4 times.
  138. // One for a. One for b. One for line 125 when we pop. One for the stale element in the queue.
  139. ASSERT_EQ(gRefCountDestructorCalled, 4);
  140. }
  141. TEST_F(MindDataTestQueue, Test6) {
  142. // Create a list of queues
  143. QueueList<std::unique_ptr<int>> my_list_of_queues;
  144. const int chosen_queue_index = 2;
  145. const int num_queues = 4;
  146. const int queue_capacity = 3;
  147. my_list_of_queues.Init(num_queues, queue_capacity);
  148. // Now try to insert a number into a specific queue and pop it
  149. std::unique_ptr<int> a(new int(99));
  150. Status rc = my_list_of_queues[chosen_queue_index]->Add(std::move(a));
  151. ASSERT_TRUE(rc.IsOk());
  152. std::unique_ptr<int> pepped_value;
  153. rc = my_list_of_queues[chosen_queue_index]->PopFront(&pepped_value);
  154. ASSERT_TRUE(rc.IsOk());
  155. MS_LOG(INFO) << "Popped value " << *pepped_value << " from queue index " << chosen_queue_index;
  156. ASSERT_EQ(*pepped_value, 99);
  157. }
  158. using namespace std::chrono;
  159. template <typename QueueType, typename PayloadType>
  160. void Perf(int n, int p, std::string name) {
  161. auto payload = std::vector<PayloadType>(n, PayloadType(p));
  162. auto queue = QueueType(n);
  163. auto t0 = high_resolution_clock::now();
  164. auto check = 0;
  165. for (int i = 0; i < queue.capacity(); i++) {
  166. queue.Add(PayloadType(p));
  167. }
  168. check = queue.size();
  169. for (int i = 0; i < queue.capacity(); i++) {
  170. queue.PopFront(&payload[i]);
  171. }
  172. auto t1 = high_resolution_clock::now();
  173. std::cout << name << " queue filled size: " << queue.size() << " " << check << std::endl;
  174. auto t2 = high_resolution_clock::now();
  175. for (int i = 0; i < queue.capacity(); i++) {
  176. queue.Add(PayloadType(p));
  177. }
  178. check = queue.size();
  179. for (int i = 0; i < queue.capacity(); i++) {
  180. queue.PopFront(&payload[i]);
  181. }
  182. auto t3 = high_resolution_clock::now();
  183. auto d = duration_cast<milliseconds>(t3 - t2 + t1 - t0).count();
  184. std::cout << name << " queue emptied size: " << queue.size() << " " << check << std::endl;
  185. std::cout << name << " "
  186. << " ran in " << d << "ms" << std::endl;
  187. }
  188. template <typename QueueType, typename PayloadType>
  189. void Fuzz(int n, int p, std::string name) {
  190. std::mt19937 gen(1);
  191. auto payload = std::vector<PayloadType>(n, PayloadType(p));
  192. auto queue = QueueType(n);
  193. auto dist = std::uniform_int_distribution<int>(0, 2);
  194. std::cout << "###" << std::endl;
  195. for (auto i = 0; i < n; i++) {
  196. auto v = dist(gen);
  197. if (v == 0 && queue.size() < n - 1) {
  198. queue.Add(std::move(payload[i]));
  199. }
  200. if (v == 1 && queue.size() > 0) {
  201. queue.PopFront(&payload[i]);
  202. } else {
  203. queue.Reset();
  204. }
  205. }
  206. std::cout << name << " fuzz ran " << queue.size() << std::endl;
  207. }
  208. TEST_F(MindDataTestQueue, TestPerf) {
  209. try {
  210. int kSz = 1000000;
  211. // std::cout << "enter size" << std::endl;
  212. // std::cin >> kSz;
  213. Perf<Queue<std::vector<int>>, std::vector<int>>(kSz, 1, "old queue, vector of size 1");
  214. } catch (const std::exception &e) {
  215. std::cout << e.what() << std::endl;
  216. }
  217. std::cout << "Test Reset" << std::endl;
  218. std::cout << "Enter fuzz size" << std::endl;
  219. int fs = 1000;
  220. // std::cin >> fs;
  221. Fuzz<Queue<std::vector<int>>, std::vector<int>>(fs, 1, "New queue");
  222. }