4 #include "map2/map_types.h"
5 #include "cppunit/thread.h"
7 #include <cds/lock/spinlock.h>
9 #include <algorithm> // random_shuffle
13 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
14 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
15 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
16 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
19 static size_t c_nMapSize = 1000000 ; // map size
20 static size_t c_nInsertThreadCount = 4; // count of insertion thread
21 static size_t c_nDeleteThreadCount = 4; // count of deletion thread
22 static size_t c_nEnsureThreadCount = 4; // count of ensure thread
23 static size_t c_nThreadPassCount = 4 ; // pass count for each thread
24 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
25 static bool c_bPrintGCState = true;
28 class Map_InsDel_func: public CppUnitMini::TestCase
30 typedef size_t key_type;
34 atomics::atomic<size_t> nEnsureCall;
35 atomics::atomic<bool> bInitialized;
36 cds::OS::ThreadId threadId ; // insert thread id
38 typedef cds::lock::Spinlock< cds::backoff::pause > lock_type;
39 mutable lock_type m_access;
45 , bInitialized( false )
46 , threadId( cds::OS::getCurrentThreadId() )
49 value_type( value_type const& s )
52 , nEnsureCall(s.nEnsureCall.load(atomics::memory_order_relaxed))
53 , bInitialized( s.bInitialized.load(atomics::memory_order_relaxed) )
54 , threadId( cds::OS::getCurrentThreadId() )
57 // boost::container::flat_map requires operator =
58 value_type& operator=( value_type const& v )
62 nEnsureCall.store( v.nEnsureCall.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed );
63 bInitialized.store(v.bInitialized.load(atomics::memory_order_relaxed), atomics::memory_order_relaxed);
69 typedef std::vector<key_type> key_array;
70 key_array m_arrValues;
73 class Inserter: public CppUnitMini::TestThread
76 typedef typename Map::value_type pair_type;
78 virtual Inserter * clone()
80 return new Inserter( *this );
83 struct insert_functor {
84 size_t nTestFunctorRef;
90 void operator()( pair_type& val )
92 cds::lock::scoped_lock< typename value_type::lock_type> ac( val.second.m_access );
94 val.second.nKey = val.first;
95 val.second.nData = val.first * 8;
98 val.second.bInitialized.store( true, atomics::memory_order_relaxed);
103 size_t m_nInsertSuccess;
104 size_t m_nInsertFailed;
106 size_t m_nTestFunctorRef;
109 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
110 : CppUnitMini::TestThread( pool )
113 Inserter( Inserter& src )
114 : CppUnitMini::TestThread( src )
118 Map_InsDel_func& getTest()
120 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
123 virtual void init() { cds::threading::Manager::attachThread() ; }
124 virtual void fini() { cds::threading::Manager::detachThread() ; }
132 m_nTestFunctorRef = 0;
134 // func is passed by reference
136 key_array const& arr = getTest().m_arrValues;
138 if ( m_nThreadNo & 1 ) {
139 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
140 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
141 if ( rMap.insert_key( *it, std::ref(func) ) )
149 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
150 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
151 if ( rMap.insert_key( *it, std::ref(func) ) )
159 m_nTestFunctorRef = func.nTestFunctorRef;
164 class Ensurer: public CppUnitMini::TestThread
167 typedef typename Map::value_type pair_type;
169 virtual Ensurer * clone()
171 return new Ensurer( *this );
174 struct ensure_functor {
183 void operator()( bool bNew, pair_type& val )
185 cds::lock::scoped_lock<typename value_type::lock_type> ac( val.second.m_access );
188 val.second.nKey = val.first;
189 val.second.nData = val.first * 8;
190 val.second.bInitialized.store( true, atomics::memory_order_relaxed);
193 val.second.nEnsureCall.fetch_add( 1, atomics::memory_order_relaxed );
198 ensure_functor(const ensure_functor& );
202 size_t m_nEnsureFailed;
203 size_t m_nEnsureCreated;
204 size_t m_nEnsureExisted;
205 size_t m_nFunctorCreated;
206 size_t m_nFunctorModified;
209 Ensurer( CppUnitMini::ThreadPool& pool, Map& rMap )
210 : CppUnitMini::TestThread( pool )
213 Ensurer( Ensurer& src )
214 : CppUnitMini::TestThread( src )
218 Map_InsDel_func& getTest()
220 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
223 virtual void init() { cds::threading::Manager::attachThread() ; }
224 virtual void fini() { cds::threading::Manager::detachThread() ; }
236 key_array const& arr = getTest().m_arrValues;
238 if ( m_nThreadNo & 1 ) {
239 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
240 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
241 //for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
242 std::pair<bool, bool> ret = rMap.ensure( *it, std::ref( func ) );
255 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
256 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
257 //for ( size_t nItem = c_nMapSize; nItem > 0; --nItem ) {
258 std::pair<bool, bool> ret = rMap.ensure( *it, std::ref( func ) );
271 m_nFunctorCreated = func.nCreated;
272 m_nFunctorModified = func.nModified;
277 class Deleter: public CppUnitMini::TestThread
280 typedef typename Map::mapped_type value_type;
281 typedef typename Map::value_type pair_type;
283 virtual Deleter * clone()
285 return new Deleter( *this );
288 struct value_container
301 struct erase_functor {
302 value_container m_cnt;
304 void operator ()( pair_type& item )
307 if ( item.second.bInitialized.load( atomics::memory_order_relaxed )) {
308 cds::lock::scoped_lock< typename value_type::lock_type> ac( item.second.m_access );
310 if ( m_cnt.nKeyExpected == item.second.nKey && m_cnt.nKeyExpected * 8 == item.second.nData )
311 ++m_cnt.nSuccessItem;
315 item.second.nKey = 0;
319 cds::backoff::yield()();
325 size_t m_nDeleteSuccess;
326 size_t m_nDeleteFailed;
328 size_t m_nValueSuccess;
329 size_t m_nValueFailed;
332 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
333 : CppUnitMini::TestThread( pool )
336 Deleter( Deleter& src )
337 : CppUnitMini::TestThread( src )
341 Map_InsDel_func& getTest()
343 return reinterpret_cast<Map_InsDel_func&>( m_Pool.m_Test );
346 virtual void init() { cds::threading::Manager::attachThread() ; }
347 virtual void fini() { cds::threading::Manager::detachThread() ; }
357 key_array const& arr = getTest().m_arrValues;
359 if ( m_nThreadNo & 1 ) {
360 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
361 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
362 func.m_cnt.nKeyExpected = *it;
363 if ( rMap.erase( *it, std::ref(func) ))
371 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
372 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
373 func.m_cnt.nKeyExpected = *it;
374 if ( rMap.erase( *it, std::ref(func) ))
382 m_nValueSuccess = func.m_cnt.nSuccessItem;
383 m_nValueFailed = func.m_cnt.nFailedItem;
390 void do_test( Map& testMap )
392 typedef Inserter<Map> InserterThread;
393 typedef Deleter<Map> DeleterThread;
394 typedef Ensurer<Map> EnsurerThread;
395 cds::OS::Timer timer;
398 m_arrValues.reserve( c_nMapSize );
399 for ( size_t i = 0; i < c_nMapSize; ++i )
400 m_arrValues.push_back( i );
401 std::random_shuffle( m_arrValues.begin(), m_arrValues.end() );
403 CppUnitMini::ThreadPool pool( *this );
404 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
405 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
406 pool.add( new EnsurerThread( pool, testMap ), c_nEnsureThreadCount );
408 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
410 size_t nInsertSuccess = 0;
411 size_t nInsertFailed = 0;
412 size_t nDeleteSuccess = 0;
413 size_t nDeleteFailed = 0;
414 size_t nDelValueSuccess = 0;
415 size_t nDelValueFailed = 0;
416 size_t nEnsureFailed = 0;
417 size_t nEnsureCreated = 0;
418 size_t nEnsureModified = 0;
419 size_t nEnsFuncCreated = 0;
420 size_t nEnsFuncModified = 0;
421 size_t nTestFunctorRef = 0;
423 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
424 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
426 nInsertSuccess += pThread->m_nInsertSuccess;
427 nInsertFailed += pThread->m_nInsertFailed;
428 nTestFunctorRef += pThread->m_nTestFunctorRef;
431 DeleterThread * p = dynamic_cast<DeleterThread *>( *it );
433 nDeleteSuccess += p->m_nDeleteSuccess;
434 nDeleteFailed += p->m_nDeleteFailed;
435 nDelValueSuccess += p->m_nValueSuccess;
436 nDelValueFailed += p->m_nValueFailed;
439 EnsurerThread * pEns = static_cast<EnsurerThread *>( *it );
440 nEnsureCreated += pEns->m_nEnsureCreated;
441 nEnsureModified += pEns->m_nEnsureExisted;
442 nEnsureFailed += pEns->m_nEnsureFailed;
443 nEnsFuncCreated += pEns->m_nFunctorCreated;
444 nEnsFuncModified += pEns->m_nFunctorModified;
449 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
450 << " Del succ=" << nDeleteSuccess << "\n"
451 << " : Ins fail=" << nInsertFailed
452 << " Del fail=" << nDeleteFailed << "\n"
453 << " : Ensure succ=" << (nEnsureCreated + nEnsureModified) << " fail=" << nEnsureFailed
454 << " create=" << nEnsureCreated << " modify=" << nEnsureModified << "\n"
455 << " Map size=" << testMap.size()
458 CPPUNIT_CHECK_EX( nDelValueFailed == 0, "Functor del failed=" << nDelValueFailed );
459 CPPUNIT_CHECK_EX( nDelValueSuccess == nDeleteSuccess, "Delete success=" << nDeleteSuccess << " functor=" << nDelValueSuccess );
461 CPPUNIT_CHECK( nEnsureFailed == 0 );
463 CPPUNIT_CHECK_EX( nEnsureCreated == nEnsFuncCreated, "Ensure created=" << nEnsureCreated << " functor=" << nEnsFuncCreated );
464 CPPUNIT_CHECK_EX( nEnsureModified == nEnsFuncModified, "Ensure modified=" << nEnsureModified << " functor=" << nEnsFuncModified );
466 // nTestFunctorRef is call count of insert functor
467 CPPUNIT_CHECK_EX( nTestFunctorRef == nInsertSuccess, "nInsertSuccess=" << nInsertSuccess << " functor nTestFunctorRef=" << nTestFunctorRef );
469 CPPUNIT_MSG( " Clear map (single-threaded)..." );
471 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
472 testMap.erase( nItem );
474 CPPUNIT_MSG( " Duration=" << timer.duration() );
475 CPPUNIT_CHECK( testMap.empty() );
477 additional_check( testMap );
478 print_stat( testMap );
479 additional_cleanup( testMap );
485 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
486 << " delete=" << c_nDeleteThreadCount
487 << " ensure=" << c_nEnsureThreadCount
488 << " pass count=" << c_nThreadPassCount
489 << " map size=" << c_nMapSize
492 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
493 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
494 Map testMap( c_nMapSize, nLoadFactor );
496 if ( c_bPrintGCState )
505 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
506 << " delete=" << c_nDeleteThreadCount
507 << " ensure=" << c_nEnsureThreadCount
508 << " pass count=" << c_nThreadPassCount
509 << " map size=" << c_nMapSize
514 if ( c_bPrintGCState )
518 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
519 c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
520 c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
521 c_nEnsureThreadCount = cfg.getULong("EnsureThreadCount", 4 );
522 c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
523 c_nMapSize = cfg.getULong("MapSize", 1000000 );
524 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
525 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
528 # include "map2/map_defs.h"
529 CDSUNIT_DECLARE_MichaelMap
530 CDSUNIT_DECLARE_SplitList
531 CDSUNIT_DECLARE_SkipListMap
532 CDSUNIT_DECLARE_EllenBinTreeMap
533 CDSUNIT_DECLARE_StripedMap
534 CDSUNIT_DECLARE_RefinableMap
535 CDSUNIT_DECLARE_CuckooMap
537 CPPUNIT_TEST_SUITE( Map_InsDel_func )
538 CDSUNIT_TEST_MichaelMap
539 CDSUNIT_TEST_SplitList
540 CDSUNIT_TEST_SkipListMap
541 CDSUNIT_TEST_EllenBinTreeMap
542 CDSUNIT_TEST_StripedMap
543 CDSUNIT_TEST_RefinableMap
544 CDSUNIT_TEST_CuckooMap
545 CPPUNIT_TEST_SUITE_END()
549 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_func );