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;
54 // for BronsonAVLTreeMap
55 void operator()( bool bNew, key_type key, value_type& val )
63 size_t m_nInsertSuccess;
64 size_t m_nInsertFailed;
67 Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
68 : CppUnitMini::TestThread( pool )
71 Inserter( Inserter& src )
72 : CppUnitMini::TestThread( src )
76 Map_InsDel_Item_int& getTest()
78 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
81 virtual void init() { cds::threading::Manager::attachThread() ; }
82 virtual void fini() { cds::threading::Manager::detachThread() ; }
91 size_t nGoalItem = c_nGoalItem;
92 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
93 if ( nAttempt % 2 == 0 ) {
94 if ( rMap.insert( nGoalItem, nGoalItem )) {
102 std::pair<bool, bool> ensureResult = rMap.ensure( nGoalItem, ensure_func() );
103 if ( ensureResult.second ) {
115 class Deleter: public CppUnitMini::TestThread
119 virtual Deleter * clone()
121 return new Deleter( *this );
124 size_t m_nDeleteSuccess;
125 size_t m_nDeleteFailed;
128 Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
129 : CppUnitMini::TestThread( pool )
132 Deleter( Deleter& src )
133 : CppUnitMini::TestThread( src )
137 Map_InsDel_Item_int& getTest()
139 return reinterpret_cast<Map_InsDel_Item_int&>( m_Pool.m_Test );
142 virtual void init() { cds::threading::Manager::attachThread() ; }
143 virtual void fini() { cds::threading::Manager::detachThread() ; }
152 size_t nGoalItem = c_nGoalItem;
153 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
154 if ( rMap.erase( nGoalItem )) {
167 void do_test( MAP& testMap )
169 typedef Inserter<MAP> InserterThread;
170 typedef Deleter<MAP> DeleterThread;
171 cds::OS::Timer timer;
174 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
177 std::vector<key_type> v;
178 v.reserve( c_nMapSize );
179 for ( size_t i = 0; i < c_nMapSize; ++i )
181 std::random_shuffle( v.begin(), v.end() );
182 for ( size_t i = 0; i < v.size(); ++i ) {
183 CPPUNIT_ASSERT( testMap.insert( v[i], v[i] ));
186 CPPUNIT_MSG( " Duration=" << timer.duration() );
188 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
189 CppUnitMini::ThreadPool pool( *this );
190 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
191 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
193 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
195 size_t nInsertSuccess = 0;
196 size_t nInsertFailed = 0;
197 size_t nDeleteSuccess = 0;
198 size_t nDeleteFailed = 0;
199 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
200 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
202 CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
203 nInsertSuccess += pThread->m_nInsertSuccess;
204 nInsertFailed += pThread->m_nInsertFailed;
207 DeleterThread * p = static_cast<DeleterThread *>( *it );
208 CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
209 nDeleteSuccess += p->m_nDeleteSuccess;
210 nDeleteFailed += p->m_nDeleteFailed;
213 CPPUNIT_CHECK( nInsertSuccess == nDeleteSuccess );
214 size_t nGoalItem = c_nGoalItem;
215 CPPUNIT_CHECK( testMap.find( nGoalItem ));
218 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
220 // Check if the map contains all items
221 CPPUNIT_MSG( " Check if the map contains all items" );
223 for ( size_t i = 0; i < c_nMapSize; ++i ) {
224 CPPUNIT_CHECK_EX( testMap.find( i ), "key " << i );
226 CPPUNIT_MSG( " Duration=" << timer.duration() );
228 check_before_cleanup( testMap );
231 additional_check( testMap );
232 print_stat( testMap );
233 additional_cleanup( testMap );
239 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
240 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
241 MAP testMap( c_nMapSize, nLoadFactor );
243 if ( c_bPrintGCState )
252 do_test<MAP>( testMap );
253 if ( c_bPrintGCState )
257 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
258 c_nThreadCount = cfg.getULong("ThreadCount", 8 ) ; // thread count
259 c_nMapSize = cfg.getULong("MapSize", 1000000 );
260 c_nGoalItem = cfg.getULong("GoalItem", (unsigned long) (c_nMapSize / 2) );
261 c_nAttemptCount = cfg.getULong("AttemptCount", 100000 );
262 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
263 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
266 # include "map2/map_defs.h"
267 CDSUNIT_DECLARE_MichaelMap
268 CDSUNIT_DECLARE_SplitList
269 CDSUNIT_DECLARE_SkipListMap
270 CDSUNIT_DECLARE_EllenBinTreeMap
271 CDSUNIT_DECLARE_BronsonAVLTreeMap
272 CDSUNIT_DECLARE_StripedMap
273 CDSUNIT_DECLARE_RefinableMap
274 CDSUNIT_DECLARE_CuckooMap
275 CDSUNIT_DECLARE_StdMap
277 CPPUNIT_TEST_SUITE( Map_InsDel_Item_int )
278 CDSUNIT_TEST_MichaelMap
279 CDSUNIT_TEST_SplitList
280 CDSUNIT_TEST_SkipListMap
281 CDSUNIT_TEST_EllenBinTreeMap
282 CDSUNIT_TEST_BronsonAVLTreeMap
283 CDSUNIT_TEST_StripedMap
284 CDSUNIT_TEST_RefinableMap
285 CDSUNIT_TEST_CuckooMap
286 //CDSUNIT_TEST_StdMap // very slow!!!
287 CPPUNIT_TEST_SUITE_END()
291 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_Item_int );