3 #include "map2/map_type.h"
4 #include "cppunit/thread.h"
10 #define TEST_CASE(TAG, X) void X();
12 class Map_InsDel_string: 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 std::string key_type;
35 typedef size_t value_type;
37 const std::vector<std::string> * m_parrString;
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_string& getTest()
64 return reinterpret_cast<Map_InsDel_string&>( m_Pool.m_Test );
67 virtual void init() { cds::threading::Manager::attachThread() ; }
68 virtual void fini() { cds::threading::Manager::detachThread() ; }
77 const std::vector<std::string>& arrString = *getTest().m_parrString;
78 size_t nArrSize = arrString.size();
79 size_t const nMapSize = getTest().c_nMapSize;
80 size_t const nPassCount = getTest().c_nThreadPassCount;
82 if ( m_nThreadNo & 1 ) {
83 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
84 for ( size_t nItem = 0; nItem < nMapSize; ++nItem ) {
85 if ( rMap.insert( arrString[nItem % nArrSize], nItem * 8 ) )
93 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
94 for ( size_t nItem = nMapSize; nItem > 0; --nItem ) {
95 if ( rMap.insert( arrString[nItem % nArrSize], nItem * 8 ) )
106 class Deleter: public CppUnitMini::TestThread
110 virtual Deleter * clone()
112 return new Deleter( *this );
115 size_t m_nDeleteSuccess;
116 size_t m_nDeleteFailed;
119 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
120 : CppUnitMini::TestThread( pool )
123 Deleter( Deleter& src )
124 : CppUnitMini::TestThread( src )
128 Map_InsDel_string& getTest()
130 return reinterpret_cast<Map_InsDel_string&>( m_Pool.m_Test );
133 virtual void init() { cds::threading::Manager::attachThread() ; }
134 virtual void fini() { cds::threading::Manager::detachThread() ; }
143 const std::vector<std::string>& arrString = *getTest().m_parrString;
144 size_t nArrSize = arrString.size();
145 size_t const nMapSize = getTest().c_nMapSize;
146 size_t const nPassCount = getTest().c_nThreadPassCount;
148 if ( m_nThreadNo & 1 ) {
149 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
150 for ( size_t nItem = 0; nItem < nMapSize; ++nItem ) {
151 if ( rMap.erase( arrString[nItem % nArrSize] ) )
159 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
160 for ( size_t nItem = nMapSize; nItem > 0; --nItem ) {
161 if ( rMap.erase( arrString[nItem % nArrSize] ) )
174 void do_test( Map& testMap )
176 typedef Inserter<Map> InserterThread;
177 typedef Deleter<Map> DeleterThread;
178 cds::OS::Timer timer;
180 CppUnitMini::ThreadPool pool( *this );
181 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
182 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
184 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
186 size_t nInsertSuccess = 0;
187 size_t nInsertFailed = 0;
188 size_t nDeleteSuccess = 0;
189 size_t nDeleteFailed = 0;
190 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
191 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
193 nInsertSuccess += pThread->m_nInsertSuccess;
194 nInsertFailed += pThread->m_nInsertFailed;
197 DeleterThread * p = static_cast<DeleterThread *>( *it );
198 nDeleteSuccess += p->m_nDeleteSuccess;
199 nDeleteFailed += p->m_nDeleteFailed;
203 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
204 << " Del succ=" << nDeleteSuccess << "\n"
205 << " : Ins fail=" << nInsertFailed
206 << " Del fail=" << nDeleteFailed
207 << " Map size=" << testMap.size()
210 check_before_cleanup( testMap );
212 CPPUNIT_MSG( " Clear map (single-threaded)..." );
214 for ( size_t i = 0; i < m_parrString->size(); ++i )
215 testMap.erase( (*m_parrString)[i] );
216 CPPUNIT_MSG( " Duration=" << timer.duration() );
217 CPPUNIT_CHECK( testMap.empty() );
219 additional_check( testMap );
220 print_stat( testMap );
221 additional_cleanup( testMap );
227 m_parrString = &CppUnitMini::TestCase::getTestStrings();
229 CPPUNIT_MSG( "Thread count: insert=" << c_nInsertThreadCount
230 << " delete=" << c_nDeleteThreadCount
231 << " pass count=" << c_nThreadPassCount
232 << " map size=" << c_nMapSize
235 if ( Map::c_bLoadFactorDepended ) {
236 for ( c_nLoadFactor = 1; c_nLoadFactor <= c_nMaxLoadFactor; c_nLoadFactor *= 2 ) {
237 CPPUNIT_MSG( "Load factor=" << c_nLoadFactor );
238 Map testMap( *this );
240 if ( c_bPrintGCState )
245 Map testMap( *this );
247 if ( c_bPrintGCState )
252 void setUpParams( const CppUnitMini::TestCfg& cfg );
254 # include "map2/map_defs.h"
255 CDSUNIT_DECLARE_MichaelMap
256 CDSUNIT_DECLARE_SplitList
257 CDSUNIT_DECLARE_SkipListMap
258 CDSUNIT_DECLARE_EllenBinTreeMap
259 CDSUNIT_DECLARE_BronsonAVLTreeMap
260 CDSUNIT_DECLARE_MultiLevelHashMap
261 CDSUNIT_DECLARE_StripedMap
262 CDSUNIT_DECLARE_RefinableMap
263 CDSUNIT_DECLARE_CuckooMap
264 CDSUNIT_DECLARE_StdMap
266 CPPUNIT_TEST_SUITE(Map_InsDel_string)
267 CDSUNIT_TEST_MichaelMap
268 CDSUNIT_TEST_SplitList
269 CDSUNIT_TEST_SkipListMap
270 CDSUNIT_TEST_EllenBinTreeMap
271 CDSUNIT_TEST_BronsonAVLTreeMap
272 CDSUNIT_TEST_MultiLevelHashMap
273 CDSUNIT_TEST_CuckooMap
274 CDSUNIT_TEST_StripedMap
275 CDSUNIT_TEST_RefinableMap
277 CPPUNIT_TEST_SUITE_END();