fbae332e874e754a0348b6d5999c1f8b28abfe68
[libcds.git] / test / stress / queue / bounded_queue_fulness.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "queue_type.h"
32
33
34 /*
35     Bounded queue test.
36     The test checks the behaviour of bounded queue when it is almost full.
37     Many algorithms says the queue is full when it is not, and vice versa.
38 */
39 namespace {
40
41     static size_t s_nThreadCount = 8;
42     static size_t s_nQueueSize = 1024;
43     static size_t s_nPassCount = 1000000;
44
45     class bounded_queue_fulness: public cds_test::stress_fixture
46     {
47         typedef cds_test::stress_fixture base_class;
48
49     protected:
50         template <class Queue>
51         class Strain: public cds_test::thread
52         {
53             typedef cds_test::thread base_class;
54
55         public:
56             Queue&              m_Queue;
57             size_t              m_nPushError = 0;
58             size_t              m_nPopError  = 0;
59
60         public:
61             Strain( cds_test::thread_pool& pool, Queue& q )
62                 : base_class( pool )
63                 , m_Queue( q )
64             {}
65
66             Strain( Strain& src )
67                 : base_class( src )
68                 , m_Queue( src.m_Queue )
69             {}
70
71             virtual thread * clone()
72             {
73                 return new Strain( *this );
74             }
75
76             virtual void test()
77             {
78                 for ( size_t i = 0; i < s_nPassCount; ++i ) {
79                     if ( !m_Queue.push( i ))
80                         ++m_nPushError;
81                     size_t item;
82                     if ( !m_Queue.pop( item ))
83                         ++m_nPopError;
84                 }
85             }
86         };
87
88     public:
89         static void SetUpTestCase()
90         {
91             cds_test::config const& cfg = get_config( "bounded_queue_fulness" );
92
93             s_nThreadCount = cfg.get_size_t( "ThreadCount", s_nThreadCount );
94             s_nQueueSize = cfg.get_size_t( "QueueSize", s_nQueueSize );
95             s_nPassCount = cfg.get_size_t( "PassCount", s_nPassCount );
96
97             if ( s_nThreadCount == 0 )
98                 s_nThreadCount = 1;
99             if ( s_nQueueSize == 0 )
100                 s_nQueueSize = 1024;
101             if ( s_nPassCount == 0 )
102                 s_nPassCount = 1;
103         }
104
105         //static void TearDownTestCase();
106
107     protected:
108         template <class Queue>
109         void analyze( Queue& q )
110         {
111             cds_test::thread_pool& pool = get_pool();
112
113             size_t nPushError = 0;
114             size_t nPopError = 0;
115             for ( size_t i = 0; i < pool.size(); ++i ) {
116                 Strain<Queue>& strain = static_cast<Strain<Queue> &>(pool.get( i ));
117                 nPushError += strain.m_nPushError;
118                 nPopError  += strain.m_nPopError;
119             }
120             EXPECT_TRUE( !q.empty());
121             EXPECT_EQ( nPushError, 0 );
122             EXPECT_EQ( nPopError, 0 );
123         }
124
125         template <class Queue>
126         void test( Queue& q )
127         {
128             cds_test::thread_pool& pool = get_pool();
129             pool.add( new Strain<Queue>( pool, q ), s_nThreadCount );
130
131             size_t nSize = q.capacity() - s_nThreadCount;
132             for ( size_t i = 0; i < nSize; ++i )
133                 q.push( i );
134
135             propout() << std::make_pair( "thread_count", s_nThreadCount )
136                 << std::make_pair( "push_count", s_nQueueSize )
137                 << std::make_pair( "pass_count", s_nPassCount );
138
139             std::chrono::milliseconds duration = pool.run();
140             propout() << std::make_pair( "duration", duration );
141
142             analyze( q );
143
144             propout() << q.statistics();
145         }
146     };
147
148 #undef CDSSTRESS_Queue_F
149 #define CDSSTRESS_Queue_F( test_fixture, type_name, level ) \
150     TEST_F( test_fixture, type_name ) \
151     { \
152         /*if ( !check_detail_level( level )) return;*/ \
153         typedef queue::Types< size_t >::type_name queue_type; \
154         queue_type queue( s_nQueueSize ); \
155         test( queue ); \
156     }
157
158     CDSSTRESS_TsigasQueue( bounded_queue_fulness )
159     CDSSTRESS_VyukovQueue( bounded_queue_fulness )
160
161 #undef CDSSTRESS_Queue_F
162
163 } // namespace queue