3 #include "map2/map_type.h"
4 #include "cppunit/thread.h"
10 #define TEST_CASE(TAG, X) void X();
12 class Map_InsDel_Item_int: public CppUnitMini::TestCase
15 size_t c_nMapSize = 1000000; // map size
16 size_t c_nThreadCount = 4; // thread count
17 size_t c_nAttemptCount = 100000; // count of SUCCESS insert/delete for each thread
18 size_t c_nMaxLoadFactor = 8; // maximum load factor
19 bool c_bPrintGCState = true;
21 size_t c_nCuckooInitialSize = 1024; // initial size for CuckooMap
22 size_t c_nCuckooProbesetSize = 16; // CuckooMap probeset size (only for list-based probeset)
23 size_t c_nCuckooProbesetThreshold = 0; // CUckooMap probeset threshold (o - use default)
25 size_t c_nMultiLevelMap_HeadBits = 10;
26 size_t c_nMultiLevelMap_ArrayBits = 4;
29 size_t c_nLoadFactor = 2; // current load factor
32 typedef size_t key_type;
33 typedef size_t value_type;
36 class Inserter: public CppUnitMini::TestThread
40 virtual Inserter * clone()
42 return new Inserter( *this );
47 void operator()( bool bNew, std::pair<key_type const, value_type>& item )
50 item.second = item.first;
52 // for boost::container::flat_map
53 void operator()( bool bNew, std::pair<key_type, value_type>& item )
56 item.second = item.first;
59 // for BronsonAVLTreeMap
60 void operator()( bool bNew, key_type key, value_type& val )
66 // for MultiLevelHashMap
67 void operator()( std::pair<key_type const, value_type>& item, std::pair<key_type const, value_type> * pOld )
70 item.second = item.first;
75 size_t m_nInsertSuccess;
76 size_t m_nInsertFailed;
79 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
80 : CppUnitMini::TestThread( pool )
83 Inserter( Inserter& src )
84 : CppUnitMini::TestThread( src )
88 Map_InsDel_Item_int& getTest()
90 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
93 virtual void init() { cds::threading::Manager::attachThread() ; }
94 virtual void fini() { cds::threading::Manager::detachThread() ; }
103 size_t nGoalItem = getTest().c_nGoalItem;
104 size_t const nAttemptCount = getTest().c_nAttemptCount;
106 for ( size_t nAttempt = 0; nAttempt < nAttemptCount; ) {
107 if ( nAttempt % 2 == 0 ) {
108 if ( rMap.insert( nGoalItem, nGoalItem )) {
116 std::pair<bool, bool> updateResult = rMap.update( nGoalItem, update_func(), true );
117 if ( updateResult.second ) {
129 class Deleter: public CppUnitMini::TestThread
133 virtual Deleter * clone()
135 return new Deleter( *this );
138 size_t m_nDeleteSuccess;
139 size_t m_nDeleteFailed;
142 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
143 : CppUnitMini::TestThread( pool )
146 Deleter( Deleter& src )
147 : CppUnitMini::TestThread( src )
151 Map_InsDel_Item_int& getTest()
153 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
156 virtual void init() { cds::threading::Manager::attachThread() ; }
157 virtual void fini() { cds::threading::Manager::detachThread() ; }
166 size_t nGoalItem = getTest().c_nGoalItem;
167 size_t const nAttemptCount = getTest().c_nAttemptCount;
168 for ( size_t nAttempt = 0; nAttempt < nAttemptCount; ) {
169 if ( rMap.erase( nGoalItem )) {
182 void do_test( Map& testMap )
184 typedef Inserter<Map> InserterThread;
185 typedef Deleter<Map> DeleterThread;
186 cds::OS::Timer timer;
189 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
192 std::vector<key_type> v;
193 v.reserve( c_nMapSize );
194 for ( size_t i = 0; i < c_nMapSize; ++i )
196 shuffle( v.begin(), v.end() );
197 for ( size_t i = 0; i < v.size(); ++i ) {
198 CPPUNIT_ASSERT( testMap.insert( v[i], v[i] ));
201 CPPUNIT_MSG( " Duration=" << timer.duration() );
203 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
204 CppUnitMini::ThreadPool pool( *this );
205 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
206 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
208 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
210 size_t nInsertSuccess = 0;
211 size_t nInsertFailed = 0;
212 size_t nDeleteSuccess = 0;
213 size_t nDeleteFailed = 0;
214 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
215 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
217 CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
218 nInsertSuccess += pThread->m_nInsertSuccess;
219 nInsertFailed += pThread->m_nInsertFailed;
222 DeleterThread * p = static_cast<DeleterThread *>( *it );
223 CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
224 nDeleteSuccess += p->m_nDeleteSuccess;
225 nDeleteFailed += p->m_nDeleteFailed;
228 CPPUNIT_CHECK( nInsertSuccess == nDeleteSuccess );
229 size_t nGoalItem = c_nGoalItem;
230 CPPUNIT_CHECK( testMap.contains( nGoalItem ));
233 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
235 // Check if the map contains all items
236 CPPUNIT_MSG( " Check if the map contains all items" );
238 for ( size_t i = 0; i < c_nMapSize; ++i ) {
239 CPPUNIT_CHECK_EX( testMap.contains( i ), "key " << i );
241 CPPUNIT_MSG( " Duration=" << timer.duration() );
243 check_before_cleanup( testMap );
246 additional_check( testMap );
247 print_stat( testMap );
248 additional_cleanup( testMap );
254 if ( Map::c_bLoadFactorDepended ) {
255 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
256 CPPUNIT_MSG( "Load factor=" << c_nLoadFactor );
257 Map testMap( *this );
259 if ( c_bPrintGCState )
264 Map testMap( *this );
265 do_test<Map>( testMap );
266 if ( c_bPrintGCState )
271 void setUpParams( const CppUnitMini::TestCfg& cfg );
273 # include "map2/map_defs.h"
274 CDSUNIT_DECLARE_MichaelMap
275 CDSUNIT_DECLARE_SplitList
276 CDSUNIT_DECLARE_SkipListMap
277 CDSUNIT_DECLARE_EllenBinTreeMap
278 CDSUNIT_DECLARE_BronsonAVLTreeMap
279 CDSUNIT_DECLARE_MultiLevelHashMap
280 CDSUNIT_DECLARE_StripedMap
281 CDSUNIT_DECLARE_RefinableMap
282 CDSUNIT_DECLARE_CuckooMap
283 // CDSUNIT_DECLARE_StdMap // very slow!!
285 CPPUNIT_TEST_SUITE(Map_InsDel_Item_int)
286 CDSUNIT_TEST_MichaelMap
287 CDSUNIT_TEST_SplitList
288 CDSUNIT_TEST_SkipListMap
289 CDSUNIT_TEST_EllenBinTreeMap
290 CDSUNIT_TEST_BronsonAVLTreeMap
291 CDSUNIT_TEST_MultiLevelHashMap
292 CDSUNIT_TEST_CuckooMap
293 CDSUNIT_TEST_StripedMap
294 CDSUNIT_TEST_RefinableMap
295 // CDSUNIT_TEST_StdMap // very slow!!
296 CPPUNIT_TEST_SUITE_END();