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_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 std::string key_type;
34 typedef size_t value_type;
36 const std::vector<std::string> * m_parrString;
39 class Inserter: public CppUnitMini::TestThread
43 virtual Inserter * clone()
45 return new Inserter( *this );
48 size_t m_nInsertSuccess;
49 size_t m_nInsertFailed;
52 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
53 : CppUnitMini::TestThread( pool )
56 Inserter( Inserter& src )
57 : CppUnitMini::TestThread( src )
61 Map_InsDel_string& getTest()
63 return reinterpret_cast<Map_InsDel_string&>( m_Pool.m_Test );
66 virtual void init() { cds::threading::Manager::attachThread() ; }
67 virtual void fini() { cds::threading::Manager::detachThread() ; }
76 const std::vector<std::string>& arrString = *getTest().m_parrString;
77 size_t nArrSize = arrString.size();
78 size_t const nMapSize = getTest().c_nMapSize;
79 size_t const nPassCount = getTest().c_nThreadPassCount;
81 if ( m_nThreadNo & 1 ) {
82 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
83 for ( size_t nItem = 0; nItem < nMapSize; ++nItem ) {
84 if ( rMap.insert( arrString[nItem % nArrSize], nItem * 8 ) )
92 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
93 for ( size_t nItem = nMapSize; nItem > 0; --nItem ) {
94 if ( rMap.insert( arrString[nItem % nArrSize], nItem * 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_string& getTest()
129 return reinterpret_cast<Map_InsDel_string&>( m_Pool.m_Test );
132 virtual void init() { cds::threading::Manager::attachThread() ; }
133 virtual void fini() { cds::threading::Manager::detachThread() ; }
142 const std::vector<std::string>& arrString = *getTest().m_parrString;
143 size_t nArrSize = arrString.size();
144 size_t const nMapSize = getTest().c_nMapSize;
145 size_t const nPassCount = getTest().c_nThreadPassCount;
147 if ( m_nThreadNo & 1 ) {
148 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
149 for ( size_t nItem = 0; nItem < nMapSize; ++nItem ) {
150 if ( rMap.erase( arrString[nItem % nArrSize] ) )
158 for ( size_t nPass = 0; nPass < nPassCount; ++nPass ) {
159 for ( size_t nItem = nMapSize; nItem > 0; --nItem ) {
160 if ( rMap.erase( arrString[nItem % nArrSize] ) )
173 void do_test( Map& testMap )
175 typedef Inserter<Map> InserterThread;
176 typedef Deleter<Map> DeleterThread;
177 cds::OS::Timer timer;
179 CppUnitMini::ThreadPool pool( *this );
180 pool.add( new InserterThread( pool, testMap ), c_nInsertThreadCount );
181 pool.add( new DeleterThread( pool, testMap ), c_nDeleteThreadCount );
183 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
185 size_t nInsertSuccess = 0;
186 size_t nInsertFailed = 0;
187 size_t nDeleteSuccess = 0;
188 size_t nDeleteFailed = 0;
189 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
190 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
192 nInsertSuccess += pThread->m_nInsertSuccess;
193 nInsertFailed += pThread->m_nInsertFailed;
196 DeleterThread * p = static_cast<DeleterThread *>( *it );
197 nDeleteSuccess += p->m_nDeleteSuccess;
198 nDeleteFailed += p->m_nDeleteFailed;
202 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess
203 << " Del succ=" << nDeleteSuccess << "\n"
204 << " : Ins fail=" << nInsertFailed
205 << " Del fail=" << nDeleteFailed
206 << " Map size=" << testMap.size()
209 check_before_cleanup( testMap );
211 CPPUNIT_MSG( " Clear map (single-threaded)..." );
213 for ( size_t i = 0; i < m_parrString->size(); ++i )
214 testMap.erase( (*m_parrString)[i] );
215 CPPUNIT_MSG( " Duration=" << timer.duration() );
216 CPPUNIT_CHECK( testMap.empty() );
217 CPPUNIT_CHECK_EX( testMap.size() == 0, "size() == " << testMap.size() );
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_FeldmanHashMap_city
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_FeldmanHashMap_city
273 CDSUNIT_TEST_CuckooMap
274 CDSUNIT_TEST_StripedMap
275 CDSUNIT_TEST_RefinableMap
277 CPPUNIT_TEST_SUITE_END();