3 #include "map2/map_types.h"
4 #include "cppunit/thread.h"
10 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
11 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
12 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
13 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
16 static size_t c_nMapSize = 1000000 ; // map size
17 static size_t c_nThreadCount = 4 ; // thread count
18 static size_t c_nGoalItem = c_nMapSize / 2;
19 static size_t c_nAttemptCount = 100000 ; // count of SUCCESS insert/delete for each thread
20 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
21 static bool c_bPrintGCState = true;
24 class Map_InsDel_Item_string: public CppUnitMini::TestCase
26 typedef std::string key_type;
27 typedef size_t value_type;
29 const std::vector<std::string> * m_parrString;
32 class Inserter: public CppUnitMini::TestThread
36 virtual Inserter * clone()
38 return new Inserter( *this );
41 size_t m_nInsertSuccess;
42 size_t m_nInsertFailed;
45 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
46 : CppUnitMini::TestThread( pool )
49 Inserter( Inserter& src )
50 : CppUnitMini::TestThread( src )
54 Map_InsDel_Item_string& getTest()
56 return reinterpret_cast<Map_InsDel_Item_string&>( m_Pool.m_Test );
59 virtual void init() { cds::threading::Manager::attachThread() ; }
60 virtual void fini() { cds::threading::Manager::detachThread() ; }
69 size_t nGoalItem = c_nGoalItem;
70 std::string strGoal = (*getTest().m_parrString)[nGoalItem];
72 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
73 if ( rMap.insert( strGoal, nGoalItem )) {
84 class Deleter: public CppUnitMini::TestThread
88 struct erase_cleaner {
89 void operator ()(std::pair<typename Map::key_type const, typename Map::mapped_type>& val )
93 // for boost::container::flat_map
94 void operator ()(std::pair< typename std::remove_const< typename Map::key_type >::type, typename Map::mapped_type>& val )
100 virtual Deleter * clone()
102 return new Deleter( *this );
105 size_t m_nDeleteSuccess;
106 size_t m_nDeleteFailed;
109 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
110 : CppUnitMini::TestThread( pool )
113 Deleter( Deleter& src )
114 : CppUnitMini::TestThread( src )
118 Map_InsDel_Item_string& getTest()
120 return reinterpret_cast<Map_InsDel_Item_string&>( m_Pool.m_Test );
123 virtual void init() { cds::threading::Manager::attachThread() ; }
124 virtual void fini() { cds::threading::Manager::detachThread() ; }
133 size_t nGoalItem = c_nGoalItem;
134 std::string strGoal = (*getTest().m_parrString)[nGoalItem];
136 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
137 if ( rMap.erase( strGoal, erase_cleaner() )) {
150 void do_test( Map& testMap )
152 typedef Inserter<Map> InserterThread;
153 typedef Deleter<Map> DeleterThread;
154 cds::OS::Timer timer;
157 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
159 for ( size_t i = 0; i < c_nMapSize; ++i ) {
160 CPPUNIT_ASSERT_EX( testMap.insert( (*m_parrString)[i], i ), i );
162 CPPUNIT_MSG( " Duration=" << timer.duration() );
164 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
165 CppUnitMini::ThreadPool pool( *this );
166 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
167 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
169 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
171 size_t nInsertSuccess = 0;
172 size_t nInsertFailed = 0;
173 size_t nDeleteSuccess = 0;
174 size_t nDeleteFailed = 0;
175 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
176 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
178 CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
179 nInsertSuccess += pThread->m_nInsertSuccess;
180 nInsertFailed += pThread->m_nInsertFailed;
183 DeleterThread * p = static_cast<DeleterThread *>( *it );
184 CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
185 nDeleteSuccess += p->m_nDeleteSuccess;
186 nDeleteFailed += p->m_nDeleteFailed;
189 CPPUNIT_CHECK_EX( nInsertSuccess == nDeleteSuccess, "nInsertSuccess=" << nInsertSuccess << ", nDeleteSuccess=" << nDeleteSuccess );
190 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
192 // Check if the map contains all items
193 CPPUNIT_MSG( " Check if the map contains all items" );
195 for ( size_t i = 0; i < c_nMapSize; ++i ) {
196 CPPUNIT_CHECK_EX( testMap.find( (*m_parrString)[i] ), "Key \"" << (*m_parrString)[i] << "\" not found" );
198 CPPUNIT_MSG( " Duration=" << timer.duration() );
201 additional_check( testMap );
202 print_stat( testMap );
203 additional_cleanup( testMap );
209 m_parrString = &CppUnitMini::TestCase::getTestStrings();
210 if ( c_nMapSize > m_parrString->size() )
211 c_nMapSize = m_parrString->size();
212 if ( c_nGoalItem > m_parrString->size() )
213 c_nGoalItem = m_parrString->size() / 2;
215 CPPUNIT_MSG( "Thread count= " << c_nThreadCount
216 << " pass count=" << c_nAttemptCount
217 << " map size=" << c_nMapSize
220 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
221 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
222 Map testMap( c_nMapSize, nLoadFactor );
224 if ( c_bPrintGCState )
229 template <typename Map>
232 m_parrString = &CppUnitMini::TestCase::getTestStrings();
233 if ( c_nMapSize > m_parrString->size() )
234 c_nMapSize = m_parrString->size();
235 if ( c_nGoalItem > m_parrString->size() )
236 c_nGoalItem = m_parrString->size() / 2;
238 CPPUNIT_MSG( "Thread count= " << c_nThreadCount
239 << " pass count=" << c_nAttemptCount
240 << " map size=" << c_nMapSize
245 if ( c_bPrintGCState )
249 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
250 c_nThreadCount = cfg.getULong("ThreadCount", 8 ) ; // thread count
251 c_nMapSize = cfg.getULong("MapSize", 1000000 );
252 c_nGoalItem = cfg.getULong("GoalItemIndex", (unsigned long) (c_nMapSize / 2) );
253 c_nAttemptCount = cfg.getULong("AttemptCount", 100000 );
254 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
255 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
258 # include "map2/map_defs.h"
259 CDSUNIT_DECLARE_MichaelMap
260 CDSUNIT_DECLARE_SplitList
261 CDSUNIT_DECLARE_SkipListMap
262 CDSUNIT_DECLARE_EllenBinTreeMap
263 CDSUNIT_DECLARE_StripedMap
264 CDSUNIT_DECLARE_RefinableMap
265 CDSUNIT_DECLARE_CuckooMap
266 CDSUNIT_DECLARE_StdMap
268 CPPUNIT_TEST_SUITE( Map_InsDel_Item_string )
269 CDSUNIT_TEST_MichaelMap
270 CDSUNIT_TEST_SplitList
271 CDSUNIT_TEST_SkipListMap
272 CDSUNIT_TEST_EllenBinTreeMap
273 CDSUNIT_TEST_StripedMap
274 CDSUNIT_TEST_RefinableMap
275 CDSUNIT_TEST_CuckooMap
276 //CDSUNIT_TEST_StdMap // very slow!!!
277 CPPUNIT_TEST_SUITE_END()
281 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_Item_string );