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_EXTRACT(X) TEST_MAP(X)
12 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::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_int: public CppUnitMini::TestCase
26 typedef size_t key_type;
27 typedef size_t value_type;
29 typedef std::vector<key_type> key_array;
30 key_array m_arrValues;
33 class Inserter: public CppUnitMini::TestThread
37 virtual Inserter * clone()
39 return new Inserter( *this );
42 size_t m_nInsertSuccess;
43 size_t m_nInsertFailed;
46 Inserter( CppUnitMini::ThreadPool& pool, MAP& rMap )
47 : CppUnitMini::TestThread( pool )
50 Inserter( Inserter& src )
51 : CppUnitMini::TestThread( src )
55 Map_InsDel_int& getTest()
57 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
60 virtual void init() { cds::threading::Manager::attachThread() ; }
61 virtual void fini() { cds::threading::Manager::detachThread() ; }
69 key_array const& arr = getTest().m_arrValues;
71 if ( m_nThreadNo & 1 ) {
72 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
73 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
74 if ( rMap.insert( *it, *it * 8 ) )
82 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
83 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
84 if ( rMap.insert( *it, *it * 8 ) )
95 class Deleter: public CppUnitMini::TestThread
99 virtual Deleter * clone()
101 return new Deleter( *this );
104 size_t m_nDeleteSuccess;
105 size_t m_nDeleteFailed;
108 Deleter( CppUnitMini::ThreadPool& pool, MAP& rMap )
109 : CppUnitMini::TestThread( pool )
112 Deleter( Deleter& src )
113 : CppUnitMini::TestThread( src )
117 Map_InsDel_int& getTest()
119 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
122 virtual void init() { cds::threading::Manager::attachThread() ; }
123 virtual void fini() { cds::threading::Manager::detachThread() ; }
131 key_array const& arr = getTest().m_arrValues;
133 if ( m_nThreadNo & 1 ) {
134 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
135 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
136 if ( rMap.erase( *it ) )
144 for ( size_t nPass = 0; nPass < c_nThreadPassCount; ++nPass ) {
145 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
146 if ( rMap.erase( *it ) )
158 void do_test( MAP& testMap )
160 typedef Inserter<MAP> InserterThread;
161 typedef Deleter<MAP> DeleterThread;
162 cds::OS::Timer timer;
165 m_arrValues.reserve( c_nMapSize );
166 for ( size_t i = 0; i < c_nMapSize; ++i )
167 m_arrValues.push_back( i );
168 std::random_shuffle( m_arrValues.begin(), m_arrValues.end() );
170 CppUnitMini::ThreadPool pool( *this );
171 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
172 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
174 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
176 size_t nInsertSuccess = 0;
177 size_t nInsertFailed = 0;
178 size_t nDeleteSuccess = 0;
179 size_t nDeleteFailed = 0;
180 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
181 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
183 nInsertSuccess += pThread->m_nInsertSuccess;
184 nInsertFailed += pThread->m_nInsertFailed;
187 DeleterThread * p = static_cast<DeleterThread *>( *it );
188 nDeleteSuccess += p->m_nDeleteSuccess;
189 nDeleteFailed += p->m_nDeleteFailed;
193 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
194 << " Del succ=" << nDeleteSuccess << "\n"
195 << " : Ins fail=" << nInsertFailed
196 << " Del fail=" << nDeleteFailed
197 << " Map size=" << testMap.size()
201 CPPUNIT_MSG( " Clear map (single-threaded)..." );
203 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
204 testMap.erase( nItem );
206 CPPUNIT_MSG( " Duration=" << timer.duration() );
207 CPPUNIT_ASSERT_EX( testMap.empty(), ((long long) testMap.size()) );
209 additional_check( testMap );
210 print_stat( testMap );
211 additional_cleanup( testMap );
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 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
236 << " delete=" << c_nDeleteThreadCount
237 << " pass count=" << c_nThreadPassCount
238 << " map size=" << c_nMapSize
243 if ( c_bPrintGCState )
247 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
248 c_nInsertThreadCount = cfg.getULong("InsertThreadCount", 4 );
249 c_nDeleteThreadCount = cfg.getULong("DeleteThreadCount", 4 );
250 c_nThreadPassCount = cfg.getULong("ThreadPassCount", 4 );
251 c_nMapSize = cfg.getULong("MapSize", 1000000 );
252 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
253 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
256 # include "map2/map_defs.h"
257 CDSUNIT_DECLARE_MichaelMap
258 CDSUNIT_DECLARE_SplitList
259 CDSUNIT_DECLARE_SkipListMap
260 CDSUNIT_DECLARE_EllenBinTreeMap
261 CDSUNIT_DECLARE_BronsonAVLTreeMap
262 CDSUNIT_DECLARE_StripedMap
263 CDSUNIT_DECLARE_RefinableMap
264 CDSUNIT_DECLARE_CuckooMap
265 CDSUNIT_DECLARE_StdMap
267 CPPUNIT_TEST_SUITE( Map_InsDel_int )
268 CDSUNIT_TEST_MichaelMap
269 CDSUNIT_TEST_SplitList
270 CDSUNIT_TEST_SkipListMap
271 CDSUNIT_TEST_EllenBinTreeMap
272 CDSUNIT_TEST_BronsonAVLTreeMap
273 CDSUNIT_TEST_StripedMap
274 CDSUNIT_TEST_RefinableMap
275 CDSUNIT_TEST_CuckooMap
277 CPPUNIT_TEST_SUITE_END()
281 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsDel_int );