Updated copyright
[libcds.git] / test / stress / map / insdel_int / map_insdel_int.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 "map_type.h"
32
33 namespace map {
34
35     class Map_InsDel_int: public cds_test::stress_fixture
36     {
37     public:
38         static size_t s_nMapSize;           // map size
39         static size_t s_nInsertThreadCount; // count of insertion thread
40         static size_t s_nDeleteThreadCount; // count of deletion thread
41         static size_t s_nThreadPassCount;   // pass count for each thread
42         static size_t s_nMaxLoadFactor;     // maximum load factor
43
44         static size_t s_nCuckooInitialSize;         // initial size for CuckooMap
45         static size_t s_nCuckooProbesetSize;        // CuckooMap probeset size (only for list-based probeset)
46         static size_t s_nCuckooProbesetThreshold;   // CuckooMap probeset threshold (o - use default)
47
48         static size_t s_nFeldmanMap_HeadBits;
49         static size_t s_nFeldmanMap_ArrayBits;
50
51         static size_t  s_nLoadFactor;  // current load factor
52
53         static void SetUpTestCase();
54         static void TearDownTestCase();
55
56         typedef size_t  key_type;
57         typedef size_t  value_type;
58
59         typedef std::vector<key_type>   key_array;
60         static key_array                s_arrKeys;
61
62     protected:
63         enum {
64             insert_thread,
65             delete_thread
66         };
67
68         template <class Map>
69         class Inserter: public cds_test::thread
70         {
71             typedef cds_test::thread base_class;
72             Map&     m_Map;
73
74         public:
75             size_t  m_nInsertSuccess = 0;
76             size_t  m_nInsertFailed = 0;
77
78         public:
79             Inserter( cds_test::thread_pool& pool, Map& map )
80                 : base_class( pool, insert_thread )
81                 , m_Map( map )
82             {}
83
84             Inserter( Inserter& src )
85                 : base_class( src )
86                 , m_Map( src.m_Map )
87             {}
88
89             virtual thread * clone()
90             {
91                 return new Inserter( *this );
92             }
93
94             virtual void test()
95             {
96                 Map& rMap = m_Map;
97
98                 if ( id() & 1 ) {
99                     for ( size_t nPass = 0; nPass < s_nThreadPassCount; ++nPass ) {
100                         for ( key_array::const_iterator it = s_arrKeys.cbegin(), itEnd = s_arrKeys.cend(); it != itEnd; ++it ) {
101                             if ( rMap.insert( *it, *it * 8 ))
102                                 ++m_nInsertSuccess;
103                             else
104                                 ++m_nInsertFailed;
105                         }
106                     }
107                 }
108                 else {
109                     for ( size_t nPass = 0; nPass < s_nThreadPassCount; ++nPass ) {
110                         for ( key_array::const_reverse_iterator it = s_arrKeys.crbegin(), itEnd = s_arrKeys.crend(); it != itEnd; ++it ) {
111                             if ( rMap.insert( *it, *it * 8 ))
112                                 ++m_nInsertSuccess;
113                             else
114                                 ++m_nInsertFailed;
115                         }
116                     }
117                 }
118             }
119         };
120
121         template <class Map>
122         class Deleter: public cds_test::thread
123         {
124             typedef cds_test::thread base_class;
125             Map&     m_Map;
126         public:
127             size_t  m_nDeleteSuccess = 0;
128             size_t  m_nDeleteFailed = 0;
129
130         public:
131             Deleter( cds_test::thread_pool& pool, Map& map )
132                 : base_class( pool, delete_thread )
133                 , m_Map( map )
134             {}
135
136             Deleter( Deleter& src )
137                 : base_class( src )
138                 , m_Map( src.m_Map )
139             {}
140
141             virtual thread * clone()
142             {
143                 return new Deleter( *this );
144             }
145
146             virtual void test()
147             {
148                 Map& rMap = m_Map;
149
150                 if ( id() & 1 ) {
151                     for ( size_t nPass = 0; nPass < s_nThreadPassCount; ++nPass ) {
152                         for ( key_array::const_iterator it = s_arrKeys.cbegin(), itEnd = s_arrKeys.cend(); it != itEnd; ++it ) {
153                             if ( rMap.erase( *it ))
154                                 ++m_nDeleteSuccess;
155                             else
156                                 ++m_nDeleteFailed;
157                         }
158                     }
159                 }
160                 else {
161                     for ( size_t nPass = 0; nPass < s_nThreadPassCount; ++nPass ) {
162                         for ( key_array::const_reverse_iterator it = s_arrKeys.crbegin(), itEnd = s_arrKeys.crend(); it != itEnd; ++it ) {
163                             if ( rMap.erase( *it ))
164                                 ++m_nDeleteSuccess;
165                             else
166                                 ++m_nDeleteFailed;
167                         }
168                     }
169                 }
170             }
171         };
172
173     protected:
174         template <class Map>
175         void do_test( Map& testMap )
176         {
177             typedef Inserter<Map>       inserter;
178             typedef Deleter<Map>        deleter;
179
180             cds_test::thread_pool& pool = get_pool();
181             pool.add( new inserter( pool, testMap ), s_nInsertThreadCount );
182             pool.add( new deleter( pool, testMap ), s_nDeleteThreadCount );
183
184             propout() << std::make_pair( "insert_thread_count", s_nInsertThreadCount )
185                 << std::make_pair( "delete_thread_count", s_nDeleteThreadCount )
186                 << std::make_pair( "pass_count", s_nThreadPassCount )
187                 << std::make_pair( "map_size", s_nMapSize );
188
189             std::chrono::milliseconds duration = pool.run();
190
191             propout() << std::make_pair( "duration", duration );
192
193             size_t nInsertSuccess = 0;
194             size_t nInsertFailed = 0;
195             size_t nDeleteSuccess = 0;
196             size_t nDeleteFailed = 0;
197
198             for ( size_t i = 0; i < pool.size(); ++i ) {
199                 cds_test::thread& thr = pool.get( i );
200                 switch ( thr.type()) {
201                 case insert_thread:
202                     {
203                         inserter& t = static_cast<inserter&>( thr );
204                         nInsertSuccess += t.m_nInsertSuccess;
205                         nInsertFailed += t.m_nInsertFailed;
206                     }
207                     break;
208                 case delete_thread:
209                     {
210                         deleter& t = static_cast<deleter&>(thr);
211                         nDeleteSuccess += t.m_nDeleteSuccess;
212                         nDeleteFailed += t.m_nDeleteFailed;
213                     }
214                     break;
215                 default:
216                     assert( false );
217                 }
218             }
219
220             propout()
221                 << std::make_pair( "insert_success", nInsertSuccess )
222                 << std::make_pair( "insert_failed", nInsertFailed )
223                 << std::make_pair( "delete_success", nDeleteSuccess )
224                 << std::make_pair( "delete_failed", nDeleteFailed )
225                 << std::make_pair( "finish_map_size", testMap.size());
226
227             check_before_cleanup( testMap );
228
229             testMap.clear();
230
231             EXPECT_TRUE( testMap.empty());
232
233             additional_check( testMap );
234             print_stat( propout(), testMap );
235             additional_cleanup( testMap );
236         }
237
238         template <class Map>
239         void run_test()
240         {
241             Map testMap( *this );
242             do_test( testMap );
243         }
244     };
245
246     class Map_InsDel_int_LF: public Map_InsDel_int
247         , public ::testing::WithParamInterface<size_t>
248     {
249     public:
250         template <class Set>
251         void run_test()
252         {
253             s_nLoadFactor = GetParam();
254             propout() << std::make_pair( "load_factor", s_nLoadFactor );
255             Map_InsDel_int::run_test<Set>();
256         }
257
258         static std::vector<size_t> get_load_factors();
259     };
260
261 } // namespace map