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_nInsertThreadCount = 4; // count of insertion thread
18 static size_t c_nDeleteThreadCount = 4; // count of deletion thread
19 static size_t c_nThreadPassCount = 4 ; // pass count for each thread
20 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
21 static bool c_bPrintGCState = true;
24 class Map_InsDel_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_string& getTest()
56 return reinterpret_cast<Map_InsDel_string&>( m_Pool.m_Test );
59 virtual void init() { cds::threading::Manager::attachThread() ; }
60 virtual void fini() { cds::threading::Manager::detachThread() ; }
69 const std::vector<std::string>& arrString = *getTest().m_parrString;
70 size_t nArrSize = arrString.size();
72 if ( m_nThreadNo & 1 ) {
73 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
74 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
75 if ( rMap.insert( arrString[nItem % nArrSize], nItem * 8 ) )
83 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
84 for ( size_t nItem = c_nMapSize; nItem > 0; --nItem ) {
85 if ( rMap.insert( arrString[nItem % nArrSize], nItem * 8 ) )
96 class Deleter: public CppUnitMini::TestThread
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_string& getTest()
120 return reinterpret_cast<Map_InsDel_string&>( m_Pool.m_Test );
123 virtual void init() { cds::threading::Manager::attachThread() ; }
124 virtual void fini() { cds::threading::Manager::detachThread() ; }
133 const std::vector<std::string>& arrString = *getTest().m_parrString;
134 size_t nArrSize = arrString.size();
136 if ( m_nThreadNo & 1 ) {
137 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
138 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
139 if ( rMap.erase( arrString[nItem % nArrSize] ) )
147 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
148 for ( size_t nItem = c_nMapSize; nItem > 0; --nItem ) {
149 if ( rMap.erase( arrString[nItem % nArrSize] ) )
162 void do_test( MAP& testMap )
164 typedef Inserter<MAP> InserterThread;
165 typedef Deleter<MAP> DeleterThread;
166 cds::OS::Timer timer;
168 CppUnitMini::ThreadPool pool( *this );
169 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
170 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
172 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
174 size_t nInsertSuccess = 0;
175 size_t nInsertFailed = 0;
176 size_t nDeleteSuccess = 0;
177 size_t nDeleteFailed = 0;
178 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
179 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
181 nInsertSuccess += pThread->m_nInsertSuccess;
182 nInsertFailed += pThread->m_nInsertFailed;
185 DeleterThread * p = static_cast<DeleterThread *>( *it );
186 nDeleteSuccess += p->m_nDeleteSuccess;
187 nDeleteFailed += p->m_nDeleteFailed;
191 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
192 << " Del succ=" << nDeleteSuccess << "\n"
193 << " : Ins fail=" << nInsertFailed
194 << " Del fail=" << nDeleteFailed
195 << " Map size=" << testMap.size()
198 check_before_cleanup( testMap );
200 CPPUNIT_MSG( " Clear map (single-threaded)..." );
202 for ( size_t i = 0; i < m_parrString->size(); ++i )
203 testMap.erase( (*m_parrString)[i] );
204 CPPUNIT_MSG( " Duration=" << timer.duration() );
205 CPPUNIT_CHECK( testMap.empty() );
207 additional_check( testMap );
208 print_stat( testMap );
209 additional_cleanup( testMap );
215 m_parrString = &CppUnitMini::TestCase::getTestStrings();
217 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
218 << " delete=" << c_nDeleteThreadCount
219 << " pass count=" << c_nThreadPassCount
220 << " map size=" << c_nMapSize
223 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
224 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
225 MAP testMap( c_nMapSize, nLoadFactor );
227 if ( c_bPrintGCState )
235 m_parrString = &CppUnitMini::TestCase::getTestStrings();
237 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
238 << " delete=" << c_nDeleteThreadCount
239 << " pass count=" << c_nThreadPassCount
240 << " map size=" << c_nMapSize
245 if ( c_bPrintGCState )
249 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
250 c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
251 c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
252 c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
253 c_nMapSize = cfg.getULong("MapSize", 1000000 );
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_BronsonAVLTreeMap
264 CDSUNIT_DECLARE_StripedMap
265 CDSUNIT_DECLARE_RefinableMap
266 CDSUNIT_DECLARE_CuckooMap
267 CDSUNIT_DECLARE_StdMap
269 CPPUNIT_TEST_SUITE( Map_InsDel_string )
270 CDSUNIT_TEST_MichaelMap
271 CDSUNIT_TEST_SplitList
272 CDSUNIT_TEST_SkipListMap
273 CDSUNIT_TEST_EllenBinTreeMap
274 CDSUNIT_TEST_BronsonAVLTreeMap
275 CDSUNIT_TEST_StripedMap
276 CDSUNIT_TEST_RefinableMap
277 CDSUNIT_TEST_CuckooMap
279 CPPUNIT_TEST_SUITE_END()
283 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_string );