3 #include "map2/map_types.h"
4 #include "cppunit/thread.h"
7 #include <algorithm> // random_shuffle
11 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
12 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
13 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
14 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
17 static size_t c_nMapSize = 1000000 ; // map size
18 static size_t c_nThreadCount = 4 ; // thread count
19 static size_t c_nGoalItem = c_nMapSize / 2;
20 static size_t c_nAttemptCount = 100000 ; // count of SUCCESS insert/delete for each thread
21 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
22 static bool c_bPrintGCState = true;
25 class Map_InsDel_Item_int: public CppUnitMini::TestCase
27 typedef size_t key_type;
28 typedef size_t value_type;
31 class Inserter: public CppUnitMini::TestThread
35 virtual Inserter * clone()
37 return new Inserter( *this );
42 void operator()( bool bNew, std::pair<key_type const, value_type>& item )
45 item.second = item.first;
47 // for boost::container::flat_map
48 void operator()( bool bNew, std::pair<key_type, value_type>& item )
51 item.second = item.first;
56 size_t m_nInsertSuccess;
57 size_t m_nInsertFailed;
60 Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
61 : CppUnitMini::TestThread( pool )
64 Inserter( Inserter& src )
65 : CppUnitMini::TestThread( src )
69 Map_InsDel_Item_int& getTest()
71 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
74 virtual void init() { cds::threading::Manager::attachThread() ; }
75 virtual void fini() { cds::threading::Manager::detachThread() ; }
84 size_t nGoalItem = c_nGoalItem;
85 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
86 if ( nAttempt % 2 == 0 ) {
87 if ( rMap.insert( nGoalItem, nGoalItem )) {
95 std::pair<bool, bool> ensureResult = rMap.ensure( nGoalItem, ensure_func() );
96 if ( ensureResult.second ) {
108 class Deleter: public CppUnitMini::TestThread
112 virtual Deleter * clone()
114 return new Deleter( *this );
117 size_t m_nDeleteSuccess;
118 size_t m_nDeleteFailed;
121 Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
122 : CppUnitMini::TestThread( pool )
125 Deleter( Deleter& src )
126 : CppUnitMini::TestThread( src )
130 Map_InsDel_Item_int& getTest()
132 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
135 virtual void init() { cds::threading::Manager::attachThread() ; }
136 virtual void fini() { cds::threading::Manager::detachThread() ; }
145 size_t nGoalItem = c_nGoalItem;
146 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
147 if ( rMap.erase( nGoalItem )) {
160 void do_test( MAP& testMap )
162 typedef Inserter<MAP> InserterThread;
163 typedef Deleter<MAP> DeleterThread;
164 cds::OS::Timer timer;
167 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
170 std::vector<key_type> v;
171 v.reserve( c_nMapSize );
172 for ( size_t i = 0; i < c_nMapSize; ++i )
174 std::random_shuffle( v.begin(), v.end() );
175 for ( size_t i = 0; i < v.size(); ++i ) {
176 CPPUNIT_ASSERT( testMap.insert( v[i], v[i] ));
179 CPPUNIT_MSG( " Duration=" << timer.duration() );
181 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
182 CppUnitMini::ThreadPool pool( *this );
183 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
184 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
186 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
188 size_t nInsertSuccess = 0;
189 size_t nInsertFailed = 0;
190 size_t nDeleteSuccess = 0;
191 size_t nDeleteFailed = 0;
192 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
193 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
195 CPPUNIT_ASSERT( pThread->m_nInsertSuccess == c_nAttemptCount );
196 nInsertSuccess += pThread->m_nInsertSuccess;
197 nInsertFailed += pThread->m_nInsertFailed;
200 DeleterThread * p = static_cast<DeleterThread *>( *it );
201 CPPUNIT_ASSERT( p->m_nDeleteSuccess == c_nAttemptCount );
202 nDeleteSuccess += p->m_nDeleteSuccess;
203 nDeleteFailed += p->m_nDeleteFailed;
206 CPPUNIT_ASSERT( nInsertSuccess == nDeleteSuccess );
207 size_t nGoalItem = c_nGoalItem;
208 CPPUNIT_ASSERT( testMap.find( nGoalItem ));
211 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
213 // Check if the map contains all items
214 CPPUNIT_MSG( " Check if the map contains all items" );
216 for ( size_t i = 0; i < c_nMapSize; ++i ) {
217 CPPUNIT_ASSERT( testMap.find( i ));
219 CPPUNIT_MSG( " Duration=" << timer.duration() );
222 additional_check( testMap );
223 print_stat( testMap );
224 additional_cleanup( testMap );
230 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
231 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
232 MAP testMap( c_nMapSize, nLoadFactor );
234 if ( c_bPrintGCState )
243 do_test<MAP>( testMap );
244 if ( c_bPrintGCState )
248 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
249 c_nThreadCount = cfg.getULong("ThreadCount", 8 ) ; // thread count
250 c_nMapSize = cfg.getULong("MapSize", 1000000 );
251 c_nGoalItem = cfg.getULong("GoalItem", (unsigned long) (c_nMapSize / 2) );
252 c_nAttemptCount = cfg.getULong("AttemptCount", 100000 );
253 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
254 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
257 # include "map2/map_defs.h"
258 CDSUNIT_DECLARE_MichaelMap
259 CDSUNIT_DECLARE_SplitList
260 CDSUNIT_DECLARE_SkipListMap
261 CDSUNIT_DECLARE_EllenBinTreeMap
262 CDSUNIT_DECLARE_StripedMap
263 CDSUNIT_DECLARE_RefinableMap
264 CDSUNIT_DECLARE_CuckooMap
265 CDSUNIT_DECLARE_StdMap
267 CPPUNIT_TEST_SUITE( Map_InsDel_Item_int )
268 CDSUNIT_TEST_MichaelMap
269 CDSUNIT_TEST_SplitList
270 CDSUNIT_TEST_SkipListMap
271 CDSUNIT_TEST_EllenBinTreeMap
272 CDSUNIT_TEST_StripedMap
273 CDSUNIT_TEST_RefinableMap
274 CDSUNIT_TEST_CuckooMap
275 //CDSUNIT_TEST_StdMap // very slow!!!
276 CPPUNIT_TEST_SUITE_END()
280 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_Item_int );