3 #include "map2/map_type.h"
4 #include "cppunit/thread.h"
10 # define TEST_CASE(TAG, X) void X();
12 class Map_InsDel_int: public CppUnitMini::TestCase
15 size_t c_nMapSize = 1000000; // map size
16 size_t c_nInsertThreadCount = 4; // count of insertion thread
17 size_t c_nDeleteThreadCount = 4; // count of deletion thread
18 size_t c_nThreadPassCount = 4; // pass count for each thread
19 size_t c_nMaxLoadFactor = 8; // maximum load factor
21 size_t c_nCuckooInitialSize = 1024;// initial size for CuckooMap
22 size_t c_nCuckooProbesetSize = 16; // CuckooMap probeset size (only for list-based probeset)
23 size_t c_nCuckooProbesetThreshold = 0; // CUckooMap probeset threshold (o - use default)
25 size_t c_nFeldmanMap_HeadBits = 10;
26 size_t c_nFeldmanMap_ArrayBits = 4;
28 bool c_bPrintGCState = true;
30 size_t c_nLoadFactor; // current load factor
33 typedef size_t key_type;
34 typedef size_t value_type;
36 typedef std::vector<key_type> key_array;
37 key_array m_arrValues;
40 class Inserter: public CppUnitMini::TestThread
44 virtual Inserter * clone()
46 return new Inserter( *this );
49 size_t m_nInsertSuccess;
50 size_t m_nInsertFailed;
53 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
54 : CppUnitMini::TestThread( pool )
57 Inserter( Inserter& src )
58 : CppUnitMini::TestThread( src )
62 Map_InsDel_int& getTest()
64 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
67 virtual void init() { cds::threading::Manager::attachThread() ; }
68 virtual void fini() { cds::threading::Manager::detachThread() ; }
76 key_array const& arr = getTest().m_arrValues;
78 size_t const nPassCount = getTest().c_nThreadPassCount;
80 if ( m_nThreadNo & 1 ) {
81 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
82 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
83 if ( rMap.insert( *it, *it * 8 ) )
91 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
92 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
93 if ( rMap.insert( *it, *it * 8 ) )
104 class Deleter: public CppUnitMini::TestThread
108 virtual Deleter * clone()
110 return new Deleter( *this );
113 size_t m_nDeleteSuccess;
114 size_t m_nDeleteFailed;
117 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
118 : CppUnitMini::TestThread( pool )
121 Deleter( Deleter& src )
122 : CppUnitMini::TestThread( src )
126 Map_InsDel_int& getTest()
128 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
131 virtual void init() { cds::threading::Manager::attachThread() ; }
132 virtual void fini() { cds::threading::Manager::detachThread() ; }
140 key_array const& arr = getTest().m_arrValues;
142 size_t const nPassCount = getTest().c_nThreadPassCount;
144 if ( m_nThreadNo & 1 ) {
145 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
146 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
147 if ( rMap.erase( *it ) )
155 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
156 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
157 if ( rMap.erase( *it ) )
169 void do_test( Map& testMap )
171 typedef Inserter<Map> InserterThread;
172 typedef Deleter<Map> DeleterThread;
173 cds::OS::Timer timer;
175 CppUnitMini::ThreadPool pool( *this );
176 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
177 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
179 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
181 size_t nInsertSuccess = 0;
182 size_t nInsertFailed = 0;
183 size_t nDeleteSuccess = 0;
184 size_t nDeleteFailed = 0;
185 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
186 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
188 nInsertSuccess += pThread->m_nInsertSuccess;
189 nInsertFailed += pThread->m_nInsertFailed;
192 DeleterThread * p = static_cast<DeleterThread *>( *it );
193 nDeleteSuccess += p->m_nDeleteSuccess;
194 nDeleteFailed += p->m_nDeleteFailed;
198 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
199 << " Del succ=" << nDeleteSuccess << "\n"
200 << " : Ins fail=" << nInsertFailed
201 << " Del fail=" << nDeleteFailed
202 << " Map size=" << testMap.size()
205 check_before_cleanup( testMap );
207 CPPUNIT_MSG( " Clear map (single-threaded)..." );
209 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
210 testMap.erase( nItem );
212 CPPUNIT_MSG( " Duration=" << timer.duration() );
213 CPPUNIT_ASSERT_EX( testMap.empty(), ((long long) testMap.size()) );
215 additional_check( testMap );
216 print_stat( testMap );
217 additional_cleanup( testMap );
223 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
224 << " delete=" << c_nDeleteThreadCount
225 << " pass count=" << c_nThreadPassCount
226 << " map size=" << c_nMapSize
229 if ( Map::c_bLoadFactorDepended ) {
230 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
231 CPPUNIT_MSG( "Load factor=" << c_nLoadFactor );
232 Map testMap( *this );
234 if ( c_bPrintGCState )
239 Map testMap( *this );
241 if ( c_bPrintGCState )
246 void setUpParams( const CppUnitMini::TestCfg& cfg );
248 # include "map2/map_defs.h"
249 CDSUNIT_DECLARE_MichaelMap
250 CDSUNIT_DECLARE_SplitList
251 CDSUNIT_DECLARE_SkipListMap
252 CDSUNIT_DECLARE_EllenBinTreeMap
253 CDSUNIT_DECLARE_BronsonAVLTreeMap
254 CDSUNIT_DECLARE_FeldmanHashMap_fixed
255 CDSUNIT_DECLARE_FeldmanHashMap_city
256 CDSUNIT_DECLARE_StripedMap
257 CDSUNIT_DECLARE_RefinableMap
258 CDSUNIT_DECLARE_CuckooMap
259 CDSUNIT_DECLARE_StdMap
261 CPPUNIT_TEST_SUITE(Map_InsDel_int)
262 CDSUNIT_TEST_MichaelMap
263 CDSUNIT_TEST_SplitList
264 CDSUNIT_TEST_SkipListMap
265 CDSUNIT_TEST_EllenBinTreeMap
266 CDSUNIT_TEST_BronsonAVLTreeMap
267 CDSUNIT_TEST_FeldmanHashMap_fixed
268 CDSUNIT_TEST_FeldmanHashMap_city
269 CDSUNIT_TEST_CuckooMap
270 CDSUNIT_TEST_StripedMap
271 CDSUNIT_TEST_RefinableMap
273 CPPUNIT_TEST_SUITE_END();