Adds a few single-threaded test cases for queue, stack, and set
[libcds.git] / test / stress / sequential / sequential-set / insdel_find / set_insdelfind.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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 "set_type.h"
32
33 namespace set {
34
35     class Set_InsDelFind: public cds_test::stress_fixture
36     {
37     public:
38         static size_t s_nSetSize;           // initial set size
39         static size_t s_nThreadCount;       // thread count
40         static size_t s_nMaxLoadFactor;     // maximum load factor
41         static unsigned int s_nInsertPercentage;
42         static unsigned int s_nDeletePercentage;
43         static unsigned int s_nDuration;   // test duration, seconds
44
45         static size_t  s_nCuckooInitialSize;        // initial size for CuckooSet
46         static size_t  s_nCuckooProbesetSize;       // CuckooSet probeset size (only for list-based probeset)
47         static size_t  s_nCuckooProbesetThreshold;  // CUckooSet probeset threshold (0 - use default)
48
49         static size_t s_nFeldmanSet_HeadBits;
50         static size_t s_nFeldmanSet_ArrayBits;
51
52         static size_t s_nLoadFactor;
53
54         static void SetUpTestCase();
55         //static void TearDownTestCase();
56
57     public:
58         enum actions
59         {
60             do_find,
61             do_insert,
62             do_delete
63         };
64         static const unsigned int c_nShuffleSize = 100;
65         static actions s_arrShuffle[c_nShuffleSize];
66
67     protected:
68         typedef size_t  key_type;
69         typedef size_t  value_type;
70
71         template <class Set>
72         class Worker: public cds_test::thread
73         {
74             typedef cds_test::thread base_class;
75             Set&     m_Set;
76
77         public:
78             size_t  m_nInsertSuccess = 0;
79             size_t  m_nInsertFailed = 0;
80             size_t  m_nDeleteSuccess = 0;
81             size_t  m_nDeleteFailed = 0;
82             size_t  m_nFindSuccess = 0;
83             size_t  m_nFindFailed = 0;
84
85         public:
86             Worker( cds_test::thread_pool& pool, Set& set )
87                 : base_class( pool )
88                 , m_Set( set )
89             {}
90
91             Worker( Worker& src )
92                 : base_class( src )
93                 , m_Set( src.m_Set )
94             {}
95
96             virtual thread * clone()
97             {
98                 return new Worker( *this );
99             }
100
101             virtual void test()
102             {
103                 Set& rSet = m_Set;
104                 Set_InsDelFind& fixture = pool().template fixture<Set_InsDelFind>();
105
106                 unsigned int i = 0;
107                 size_t const nNormalize = size_t(-1) / ( fixture.s_nSetSize * 2);
108
109                 size_t nRand = 0;
110                 while ( !time_elapsed()) {
111                     nRand = cds::bitop::RandXorShift(nRand);
112                     size_t n = nRand / nNormalize;
113                     switch ( s_arrShuffle[i] ) {
114                     case do_find:
115                         if ( rSet.contains( n ))
116                             ++m_nFindSuccess;
117                         else
118                             ++m_nFindFailed;
119                         break;
120                     case do_insert:
121                         if ( rSet.insert( n ))
122                             ++m_nInsertSuccess;
123                         else
124                             ++m_nInsertFailed;
125                         break;
126                     case do_delete:
127                         if ( rSet.erase( n ))
128                             ++m_nDeleteSuccess;
129                         else
130                             ++m_nDeleteFailed;
131                         break;
132                     }
133
134                     if ( ++i >= c_nShuffleSize )
135                         i = 0;
136                 }
137             }
138         };
139
140     protected:
141         template <class Set>
142         void do_test( Set& testSet )
143         {
144             typedef Worker<Set> work_thread;
145
146             // fill map - only odd number
147             {
148                 size_t * pInitArr = new size_t[ s_nSetSize ];
149                 size_t * pEnd = pInitArr + s_nSetSize;
150                 for ( size_t i = 0; i < s_nSetSize; ++i )
151                     pInitArr[i] = i * 2 + 1;
152                 shuffle( pInitArr, pEnd );
153                 for ( size_t * p = pInitArr; p < pEnd; ++p )
154                     testSet.insert( typename Set::value_type( *p, *p ));
155                 delete [] pInitArr;
156             }
157
158             cds_test::thread_pool& pool = get_pool();
159             pool.add( new work_thread( pool, testSet ), s_nThreadCount );
160
161             propout() << std::make_pair( "thread_count", s_nThreadCount )
162                 << std::make_pair( "set_size", s_nSetSize )
163                 << std::make_pair( "insert_percentage", s_nInsertPercentage )
164                 << std::make_pair( "delete_percentage", s_nDeletePercentage )
165                 << std::make_pair( "total_duration", s_nDuration );
166
167             std::chrono::milliseconds duration = pool.run( std::chrono::seconds( s_nDuration ));
168
169             propout() << std::make_pair( "duration", duration );
170
171             size_t nInsertSuccess = 0;
172             size_t nInsertFailed = 0;
173             size_t nDeleteSuccess = 0;
174             size_t nDeleteFailed = 0;
175             size_t nFindSuccess = 0;
176             size_t nFindFailed = 0;
177             for ( size_t i = 0; i < pool.size(); ++i ) {
178                 work_thread& thr = static_cast<work_thread&>( pool.get( i ));
179                 nInsertSuccess += thr.m_nInsertSuccess;
180                 nInsertFailed  += thr.m_nInsertFailed;
181                 nDeleteSuccess += thr.m_nDeleteSuccess;
182                 nDeleteFailed  += thr.m_nDeleteFailed;
183                 nFindSuccess   += thr.m_nFindSuccess;
184                 nFindFailed    += thr.m_nFindFailed;
185             }
186
187             propout()
188                 << std::make_pair( "insert_success", nInsertSuccess )
189                 << std::make_pair( "insert_failed", nInsertFailed )
190                 << std::make_pair( "delete_success", nDeleteSuccess )
191                 << std::make_pair( "delete_failed", nDeleteFailed )
192                 << std::make_pair( "find_success", nFindSuccess )
193                 << std::make_pair( "find_failed", nFindFailed );
194
195             {
196                 ASSERT_TRUE( std::chrono::duration_cast<std::chrono::seconds>(duration).count() > 0 );
197                 size_t nTotalOps = nInsertSuccess + nInsertFailed + nDeleteSuccess + nDeleteFailed + nFindSuccess + nFindFailed;
198                 propout() << std::make_pair( "avg_speed", nTotalOps / std::chrono::duration_cast<std::chrono::seconds>(duration).count());
199             }
200
201
202             testSet.clear();
203             EXPECT_TRUE( testSet.empty()) << "set size=" << testSet.size();
204
205             additional_check( testSet );
206             print_stat( propout(), testSet );
207             additional_cleanup( testSet );
208         }
209
210         template <class Set>
211         void run_test()
212         {
213             Set s( *this );
214             do_test( s );
215         }
216     };
217
218     class Set_InsDelFind_LF: public Set_InsDelFind
219         , public ::testing::WithParamInterface<size_t>
220     {
221     public:
222         template <class Set>
223         void run_test()
224         {
225             s_nLoadFactor = GetParam();
226             propout() << std::make_pair( "load_factor", s_nLoadFactor );
227             Set_InsDelFind::run_test<Set>();
228         }
229
230         static std::vector<size_t> get_load_factors();
231     };
232
233 } // namespace set