bounded_queue_fulness.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include "queue_type.h"
  6. /*
  7. Bounded queue test.
  8. The test checks the behaviour of bounded queue when it is almost full.
  9. Many algorithms says the queue is full when it is not, and vice versa.
  10. */
  11. namespace {
  12. static size_t s_nThreadCount = 8;
  13. static size_t s_nQueueSize = 1024;
  14. static size_t s_nPassCount = 1000000;
  15. class bounded_queue_fulness: public cds_test::stress_fixture
  16. {
  17. typedef cds_test::stress_fixture base_class;
  18. protected:
  19. template <class Queue>
  20. class Strain: public cds_test::thread
  21. {
  22. typedef cds_test::thread base_class;
  23. public:
  24. Queue& m_Queue;
  25. size_t m_nPushError = 0;
  26. size_t m_nPopError = 0;
  27. public:
  28. Strain( cds_test::thread_pool& pool, Queue& q )
  29. : base_class( pool )
  30. , m_Queue( q )
  31. {}
  32. Strain( Strain& src )
  33. : base_class( src )
  34. , m_Queue( src.m_Queue )
  35. {}
  36. virtual thread * clone()
  37. {
  38. return new Strain( *this );
  39. }
  40. virtual void test()
  41. {
  42. for ( size_t i = 0; i < s_nPassCount; ++i ) {
  43. if ( !m_Queue.push( i ))
  44. ++m_nPushError;
  45. size_t item;
  46. if ( !m_Queue.pop( item ))
  47. ++m_nPopError;
  48. }
  49. }
  50. };
  51. public:
  52. static void SetUpTestCase()
  53. {
  54. cds_test::config const& cfg = get_config( "bounded_queue_fulness" );
  55. s_nThreadCount = cfg.get_size_t( "ThreadCount", s_nThreadCount );
  56. s_nQueueSize = cfg.get_size_t( "QueueSize", s_nQueueSize );
  57. s_nPassCount = cfg.get_size_t( "PassCount", s_nPassCount );
  58. if ( s_nThreadCount == 0u )
  59. s_nThreadCount = 1;
  60. if ( s_nQueueSize == 0u )
  61. s_nQueueSize = 1024;
  62. if ( s_nPassCount == 0u )
  63. s_nPassCount = 1;
  64. }
  65. //static void TearDownTestCase();
  66. protected:
  67. template <class Queue>
  68. void analyze( Queue& q )
  69. {
  70. cds_test::thread_pool& pool = get_pool();
  71. size_t nPushError = 0;
  72. size_t nPopError = 0;
  73. for ( size_t i = 0; i < pool.size(); ++i ) {
  74. Strain<Queue>& strain = static_cast<Strain<Queue> &>(pool.get( i ));
  75. nPushError += strain.m_nPushError;
  76. nPopError += strain.m_nPopError;
  77. }
  78. EXPECT_TRUE( !q.empty());
  79. EXPECT_EQ( nPushError, 0u );
  80. EXPECT_EQ( nPopError, 0u );
  81. }
  82. template <class Queue>
  83. void test( Queue& q )
  84. {
  85. cds_test::thread_pool& pool = get_pool();
  86. pool.add( new Strain<Queue>( pool, q ), s_nThreadCount );
  87. size_t nSize = q.capacity() - s_nThreadCount;
  88. for ( size_t i = 0; i < nSize; ++i )
  89. q.push( i );
  90. propout() << std::make_pair( "thread_count", s_nThreadCount )
  91. << std::make_pair( "push_count", s_nQueueSize )
  92. << std::make_pair( "pass_count", s_nPassCount );
  93. std::chrono::milliseconds duration = pool.run();
  94. propout() << std::make_pair( "duration", duration );
  95. analyze( q );
  96. propout() << q.statistics();
  97. }
  98. };
  99. #undef CDSSTRESS_Queue_F
  100. #define CDSSTRESS_Queue_F( test_fixture, type_name ) \
  101. TEST_F( test_fixture, type_name ) \
  102. { \
  103. typedef queue::Types< size_t >::type_name queue_type; \
  104. queue_type queue( s_nQueueSize ); \
  105. test( queue ); \
  106. }
  107. CDSSTRESS_VyukovQueue( bounded_queue_fulness )
  108. #undef CDSSTRESS_Queue_F
  109. } // namespace queue