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_nMultiLevelMap_HeadBits = 10;
26 size_t c_nMultiLevelMap_ArrayBits = 4;
28 bool c_bPrintGCState = true;
30 size_t c_nLoadFactor; // current load factor
33 typedef CppUnitMini::TestCase Base;
34 typedef size_t key_type;
35 typedef size_t value_type;
37 typedef std::vector<key_type> key_array;
38 key_array m_arrValues;
41 class Inserter: public CppUnitMini::TestThread
45 virtual Inserter * clone()
47 return new Inserter( *this );
50 size_t m_nInsertSuccess;
51 size_t m_nInsertFailed;
54 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
55 : CppUnitMini::TestThread( pool )
58 Inserter( Inserter& src )
59 : CppUnitMini::TestThread( src )
63 Map_InsDel_int& getTest()
65 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
68 virtual void init() { cds::threading::Manager::attachThread() ; }
69 virtual void fini() { cds::threading::Manager::detachThread() ; }
77 key_array const& arr = getTest().m_arrValues;
79 size_t const nPassCount = getTest().c_nThreadPassCount;
81 if ( m_nThreadNo & 1 ) {
82 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
83 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
84 if ( rMap.insert( *it, *it * 8 ) )
92 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
93 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
94 if ( rMap.insert( *it, *it * 8 ) )
105 class Deleter: public CppUnitMini::TestThread
109 virtual Deleter * clone()
111 return new Deleter( *this );
114 size_t m_nDeleteSuccess;
115 size_t m_nDeleteFailed;
118 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
119 : CppUnitMini::TestThread( pool )
122 Deleter( Deleter& src )
123 : CppUnitMini::TestThread( src )
127 Map_InsDel_int& getTest()
129 return reinterpret_cast<Map_InsDel_int&>( m_Pool.m_Test );
132 virtual void init() { cds::threading::Manager::attachThread() ; }
133 virtual void fini() { cds::threading::Manager::detachThread() ; }
141 key_array const& arr = getTest().m_arrValues;
143 size_t const nPassCount = getTest().c_nThreadPassCount;
145 if ( m_nThreadNo & 1 ) {
146 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
147 for ( key_array::const_iterator it = arr.begin(), itEnd = arr.end(); it != itEnd; ++it ) {
148 if ( rMap.erase( *it ) )
156 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
157 for ( key_array::const_reverse_iterator it = arr.rbegin(), itEnd = arr.rend(); it != itEnd; ++it ) {
158 if ( rMap.erase( *it ) )
170 void do_test( Map& testMap )
172 typedef Inserter<Map> InserterThread;
173 typedef Deleter<Map> DeleterThread;
174 cds::OS::Timer timer;
176 CppUnitMini::ThreadPool pool( *this );
177 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
178 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
180 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
182 size_t nInsertSuccess = 0;
183 size_t nInsertFailed = 0;
184 size_t nDeleteSuccess = 0;
185 size_t nDeleteFailed = 0;
186 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
187 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
189 nInsertSuccess += pThread->m_nInsertSuccess;
190 nInsertFailed += pThread->m_nInsertFailed;
193 DeleterThread * p = static_cast<DeleterThread *>( *it );
194 nDeleteSuccess += p->m_nDeleteSuccess;
195 nDeleteFailed += p->m_nDeleteFailed;
199 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
200 << " Del succ=" << nDeleteSuccess << "\n"
201 << " : Ins fail=" << nInsertFailed
202 << " Del fail=" << nDeleteFailed
203 << " Map size=" << testMap.size()
206 check_before_cleanup( testMap );
208 CPPUNIT_MSG( " Clear map (single-threaded)..." );
210 for ( size_t nItem = 0; nItem < c_nMapSize; ++nItem ) {
211 testMap.erase( nItem );
213 CPPUNIT_MSG( " Duration=" << timer.duration() );
214 CPPUNIT_ASSERT_EX( testMap.empty(), ((long long) testMap.size()) );
216 additional_check( testMap );
217 print_stat( testMap );
218 additional_cleanup( testMap );
224 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
225 << " delete=" << c_nDeleteThreadCount
226 << " pass count=" << c_nThreadPassCount
227 << " map size=" << c_nMapSize
230 if ( Map::c_bLoadFactorDepended ) {
231 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
232 CPPUNIT_MSG( "Load factor=" << c_nLoadFactor );
233 Map testMap( *this );
235 if ( c_bPrintGCState )
240 Map testMap( *this );
242 if ( c_bPrintGCState )
247 void setUpParams( const CppUnitMini::TestCfg& cfg );
249 # include "map2/map_defs.h"
250 CDSUNIT_DECLARE_MichaelMap
251 CDSUNIT_DECLARE_SplitList
252 CDSUNIT_DECLARE_SkipListMap
253 CDSUNIT_DECLARE_EllenBinTreeMap
254 CDSUNIT_DECLARE_BronsonAVLTreeMap
255 CDSUNIT_DECLARE_MultiLevelHashMap
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_MultiLevelHashMap
268 CDSUNIT_TEST_CuckooMap
269 CDSUNIT_TEST_StripedMap
270 CDSUNIT_TEST_RefinableMap
272 CPPUNIT_TEST_SUITE_END();