spsc_queue_stress_test.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (C) 2011-2013 Tim Blechmann
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/lockfree/spsc_queue.hpp>
  7. #include <boost/thread.hpp>
  8. #define BOOST_TEST_MAIN
  9. #ifdef BOOST_LOCKFREE_INCLUDE_TESTS
  10. #include <boost/test/included/unit_test.hpp>
  11. #else
  12. #include <boost/test/unit_test.hpp>
  13. #endif
  14. #include <iostream>
  15. #include <memory>
  16. #include "test_helpers.hpp"
  17. #include "test_common.hpp"
  18. using namespace boost;
  19. using namespace boost::lockfree;
  20. using namespace std;
  21. #ifndef BOOST_LOCKFREE_STRESS_TEST
  22. static const boost::uint32_t nodes_per_thread = 100000;
  23. #else
  24. static const boost::uint32_t nodes_per_thread = 100000000;
  25. #endif
  26. struct spsc_queue_tester
  27. {
  28. spsc_queue<int, capacity<128> > sf;
  29. boost::lockfree::detail::atomic<long> spsc_queue_cnt, received_nodes;
  30. // In VxWorks one RTP just supports 65535 objects
  31. #ifndef __VXWORKS__
  32. static_hashed_set<int, 1<<16 > working_set;
  33. #else
  34. static_hashed_set<int, 1<<15 > working_set;
  35. #endif
  36. spsc_queue_tester(void):
  37. spsc_queue_cnt(0), received_nodes(0)
  38. {}
  39. void add(void)
  40. {
  41. for (boost::uint32_t i = 0; i != nodes_per_thread; ++i) {
  42. int id = generate_id<int>();
  43. working_set.insert(id);
  44. while (sf.push(id) == false)
  45. {}
  46. ++spsc_queue_cnt;
  47. }
  48. running = false;
  49. }
  50. bool get_element(void)
  51. {
  52. int data;
  53. bool success = sf.pop(data);
  54. if (success) {
  55. ++received_nodes;
  56. --spsc_queue_cnt;
  57. bool erased = working_set.erase(data);
  58. assert(erased);
  59. return true;
  60. } else
  61. return false;
  62. }
  63. boost::lockfree::detail::atomic<bool> running;
  64. void get(void)
  65. {
  66. for(;;) {
  67. bool success = get_element();
  68. if (!running && !success)
  69. break;
  70. }
  71. while ( get_element() );
  72. }
  73. void run(void)
  74. {
  75. running = true;
  76. BOOST_REQUIRE(sf.empty());
  77. boost::thread reader(boost::bind(&spsc_queue_tester::get, this));
  78. boost::thread writer(boost::bind(&spsc_queue_tester::add, this));
  79. cout << "reader and writer threads created" << endl;
  80. writer.join();
  81. cout << "writer threads joined. waiting for readers to finish" << endl;
  82. reader.join();
  83. BOOST_REQUIRE_EQUAL(received_nodes, nodes_per_thread);
  84. BOOST_REQUIRE_EQUAL(spsc_queue_cnt, 0);
  85. BOOST_REQUIRE(sf.empty());
  86. BOOST_REQUIRE(working_set.count_nodes() == 0);
  87. }
  88. };
  89. BOOST_AUTO_TEST_CASE( spsc_queue_test_caching )
  90. {
  91. boost::shared_ptr<spsc_queue_tester> test1(new spsc_queue_tester);
  92. test1->run();
  93. }
  94. struct spsc_queue_tester_buffering
  95. {
  96. spsc_queue<int, capacity<128> > sf;
  97. boost::lockfree::detail::atomic<long> spsc_queue_cnt;
  98. // In VxWorks one RTP just supports 65535 objects
  99. #ifndef __VXWORKS__
  100. static_hashed_set<int, 1<<16 > working_set;
  101. #else
  102. static_hashed_set<int, 1<<15 > working_set;
  103. #endif
  104. boost::lockfree::detail::atomic<size_t> received_nodes;
  105. spsc_queue_tester_buffering(void):
  106. spsc_queue_cnt(0), received_nodes(0)
  107. {}
  108. static const size_t buf_size = 5;
  109. void add(void)
  110. {
  111. boost::array<int, buf_size> input_buffer;
  112. for (boost::uint32_t i = 0; i != nodes_per_thread; i+=buf_size) {
  113. for (size_t i = 0; i != buf_size; ++i) {
  114. int id = generate_id<int>();
  115. working_set.insert(id);
  116. input_buffer[i] = id;
  117. }
  118. size_t pushed = 0;
  119. do {
  120. pushed += sf.push(input_buffer.c_array() + pushed,
  121. input_buffer.size() - pushed);
  122. } while (pushed != buf_size);
  123. spsc_queue_cnt+=buf_size;
  124. }
  125. running = false;
  126. }
  127. bool get_elements(void)
  128. {
  129. boost::array<int, buf_size> output_buffer;
  130. size_t popd = sf.pop(output_buffer.c_array(), output_buffer.size());
  131. if (popd) {
  132. received_nodes += popd;
  133. spsc_queue_cnt -= popd;
  134. for (size_t i = 0; i != popd; ++i) {
  135. bool erased = working_set.erase(output_buffer[i]);
  136. assert(erased);
  137. }
  138. return true;
  139. } else
  140. return false;
  141. }
  142. boost::lockfree::detail::atomic<bool> running;
  143. void get(void)
  144. {
  145. for(;;) {
  146. bool success = get_elements();
  147. if (!running && !success)
  148. break;
  149. }
  150. while ( get_elements() );
  151. }
  152. void run(void)
  153. {
  154. running = true;
  155. boost::thread reader(boost::bind(&spsc_queue_tester_buffering::get, this));
  156. boost::thread writer(boost::bind(&spsc_queue_tester_buffering::add, this));
  157. cout << "reader and writer threads created" << endl;
  158. writer.join();
  159. cout << "writer threads joined. waiting for readers to finish" << endl;
  160. reader.join();
  161. BOOST_REQUIRE_EQUAL(received_nodes, nodes_per_thread);
  162. BOOST_REQUIRE_EQUAL(spsc_queue_cnt, 0);
  163. BOOST_REQUIRE(sf.empty());
  164. BOOST_REQUIRE(working_set.count_nodes() == 0);
  165. }
  166. };
  167. BOOST_AUTO_TEST_CASE( spsc_queue_test_buffering )
  168. {
  169. boost::shared_ptr<spsc_queue_tester_buffering> test1(new spsc_queue_tester_buffering);
  170. test1->run();
  171. }