2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
35 class Map_InsDel_item_int: public cds_test::stress_fixture
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_nMaxLoadFactor; // maximum load factor
43 static size_t s_nAttemptCount; // count of SUCCESS insert/delete for each thread
44 static size_t s_nGoalItem;
46 static size_t s_nCuckooInitialSize; // initial size for CuckooMap
47 static size_t s_nCuckooProbesetSize; // CuckooMap probeset size (only for list-based probeset)
48 static size_t s_nCuckooProbesetThreshold; // CuckooMap probeset threshold (o - use default)
50 static size_t s_nFeldmanMap_HeadBits;
51 static size_t s_nFeldmanMap_ArrayBits;
53 static size_t s_nLoadFactor; // current load factor
55 static void SetUpTestCase();
56 //static void TearDownTestCase();
58 typedef size_t key_type;
59 typedef size_t value_type;
68 class Inserter: public cds_test::thread
70 typedef cds_test::thread base_class;
75 void operator()( bool bNew, std::pair<key_type const, value_type>& item ) const
78 item.second = item.first;
81 // for boost::container::flat_map
82 void operator()( bool bNew, std::pair<key_type, value_type>& item ) const
85 item.second = item.first;
88 // for BronsonAVLTreeMap
89 void operator()( bool bNew, key_type key, value_type& val ) const
96 void operator()( std::pair<key_type const, value_type>& item, std::pair<key_type const, value_type>* pOld ) const
99 item.second = item.first;
104 size_t m_nInsertSuccess = 0;
105 size_t m_nInsertFailed = 0;
108 Inserter( cds_test::thread_pool& pool, Map& map )
109 : base_class( pool, insert_thread )
113 Inserter( Inserter& src )
118 virtual thread * clone()
120 return new Inserter( *this );
127 size_t const nGoalItem = s_nGoalItem;
128 size_t const nAttemptCount = s_nAttemptCount;
130 for ( size_t nAttempt = 0; nAttempt < nAttemptCount; ) {
131 if ( nAttempt % 2 == 0 ) {
132 if ( rMap.insert( nGoalItem, nGoalItem )) {
140 std::pair<bool, bool> updateResult = rMap.update( nGoalItem, update_func(), true );
141 if ( updateResult.second ) {
153 class Deleter: public cds_test::thread
155 typedef cds_test::thread base_class;
159 size_t m_nDeleteSuccess = 0;
160 size_t m_nDeleteFailed = 0;
163 Deleter( cds_test::thread_pool& pool, Map& map )
164 : base_class( pool, delete_thread )
168 Deleter( Deleter& src )
173 virtual thread * clone()
175 return new Deleter( *this );
182 size_t const nGoalItem = s_nGoalItem;
183 size_t const nAttemptCount = s_nAttemptCount;
184 for ( size_t nAttempt = 0; nAttempt < nAttemptCount; ) {
185 if ( rMap.erase( nGoalItem )) {
198 void do_test( Map& testMap )
200 typedef Inserter<Map> inserter;
201 typedef Deleter<Map> deleter;
205 std::vector<key_type> v;
206 v.reserve( s_nMapSize );
207 for ( size_t i = 0; i < s_nMapSize; ++i )
209 shuffle( v.begin(), v.end());
211 EXPECT_TRUE( testMap.insert( i, i ));
214 cds_test::thread_pool& pool = get_pool();
215 pool.add( new inserter( pool, testMap ), s_nInsertThreadCount );
216 pool.add( new deleter( pool, testMap ), s_nDeleteThreadCount );
218 propout() << std::make_pair( "insert_thread_count", s_nInsertThreadCount )
219 << std::make_pair( "delete_thread_count", s_nDeleteThreadCount )
220 << std::make_pair( "map_size", s_nMapSize )
221 << std::make_pair( "goal_item", s_nGoalItem )
222 << std::make_pair( "attempt_count", s_nAttemptCount );
225 std::chrono::milliseconds duration = pool.run();
227 propout() << std::make_pair( "duration", duration );
229 size_t nInsertSuccess = 0;
230 size_t nInsertFailed = 0;
231 size_t nDeleteSuccess = 0;
232 size_t nDeleteFailed = 0;
234 for ( size_t i = 0; i < pool.size(); ++i ) {
235 cds_test::thread& thr = pool.get( i );
236 switch ( thr.type()) {
239 inserter& t = static_cast<inserter&>(thr);
240 EXPECT_EQ( t.m_nInsertSuccess, s_nAttemptCount ) << "thread=" << t.id();
241 nInsertSuccess += t.m_nInsertSuccess;
242 nInsertFailed += t.m_nInsertFailed;
247 deleter& t = static_cast<deleter&>(thr);
248 EXPECT_EQ( t.m_nDeleteSuccess, s_nAttemptCount ) << "thread=" << t.id();
249 nDeleteSuccess += t.m_nDeleteSuccess;
250 nDeleteFailed += t.m_nDeleteFailed;
258 EXPECT_EQ( nInsertSuccess, nDeleteSuccess );
259 EXPECT_TRUE( testMap.contains( s_nGoalItem ));
260 EXPECT_CONTAINER_SIZE( testMap, s_nMapSize );
263 << std::make_pair( "insert_success", nInsertSuccess )
264 << std::make_pair( "insert_failed", nInsertFailed )
265 << std::make_pair( "delete_success", nDeleteSuccess )
266 << std::make_pair( "delete_failed", nDeleteFailed );
268 // Check if the map contains all items
269 for ( size_t i = 0; i < s_nMapSize; ++i )
270 EXPECT_TRUE( testMap.contains( i )) << "key=" << i;
272 check_before_cleanup( testMap );
275 additional_check( testMap );
276 print_stat( propout(), testMap );
277 additional_cleanup( testMap );
283 Map testMap( *this );
284 do_test<Map>( testMap );
288 class Map_InsDel_item_int_LF: public Map_InsDel_item_int
289 , public ::testing::WithParamInterface<size_t>
295 s_nLoadFactor = GetParam();
296 propout() << std::make_pair( "load_factor", s_nLoadFactor );
297 Map_InsDel_item_int::run_test<Set>();
300 static std::vector<size_t> get_load_factors();