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_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
12 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
13 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
15 class Map_InsDel_Item_string: public CppUnitMini::TestCase
17 static size_t c_nMapSize; // map size
18 static size_t c_nThreadCount; // thread count
19 static size_t c_nGoalItem;
20 static size_t c_nAttemptCount; // count of SUCCESS insert/delete for each thread
21 static size_t c_nMaxLoadFactor;// maximum load factor
22 static bool c_bPrintGCState;
24 typedef CppUnitMini::TestCase Base;
25 typedef std::string key_type;
26 typedef size_t value_type;
28 const std::vector<std::string> * m_parrString;
31 class Inserter: public CppUnitMini::TestThread
35 virtual Inserter * clone()
37 return new Inserter( *this );
40 size_t m_nInsertSuccess;
41 size_t m_nInsertFailed;
44 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
45 : CppUnitMini::TestThread( pool )
48 Inserter( Inserter& src )
49 : CppUnitMini::TestThread( src )
53 Map_InsDel_Item_string& getTest()
55 return reinterpret_cast<Map_InsDel_Item_string&>( m_Pool.m_Test );
58 virtual void init() { cds::threading::Manager::attachThread() ; }
59 virtual void fini() { cds::threading::Manager::detachThread() ; }
68 size_t nGoalItem = c_nGoalItem;
69 std::string strGoal = (*getTest().m_parrString)[nGoalItem];
71 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
72 if ( rMap.insert( strGoal, nGoalItem )) {
83 class Deleter: public CppUnitMini::TestThread
87 struct erase_cleaner {
88 void operator ()(std::pair<typename Map::key_type const, typename Map::mapped_type>& val )
92 // for boost::container::flat_map
93 void operator ()(std::pair< typename std::remove_const< typename Map::key_type >::type, typename Map::mapped_type>& val )
97 // for BronsonAVLTreeMap
98 void operator()( typename Map::key_type const& /*key*/, typename Map::mapped_type& val )
104 virtual Deleter * clone()
106 return new Deleter( *this );
109 size_t m_nDeleteSuccess;
110 size_t m_nDeleteFailed;
113 Deleter( CppUnitMini::ThreadPool& pool, Map& rMap )
114 : CppUnitMini::TestThread( pool )
117 Deleter( Deleter& src )
118 : CppUnitMini::TestThread( src )
122 Map_InsDel_Item_string& getTest()
124 return reinterpret_cast<Map_InsDel_Item_string&>( m_Pool.m_Test );
127 virtual void init() { cds::threading::Manager::attachThread() ; }
128 virtual void fini() { cds::threading::Manager::detachThread() ; }
137 size_t nGoalItem = c_nGoalItem;
138 std::string strGoal = (*getTest().m_parrString)[nGoalItem];
140 for ( size_t nAttempt = 0; nAttempt < c_nAttemptCount; ) {
141 if ( rMap.erase( strGoal, erase_cleaner() )) {
154 void do_test( Map& testMap )
156 typedef Inserter<Map> InserterThread;
157 typedef Deleter<Map> DeleterThread;
158 cds::OS::Timer timer;
161 CPPUNIT_MSG( " Fill map (" << c_nMapSize << " items)...");
163 for ( size_t i = 0; i < c_nMapSize; ++i ) {
164 CPPUNIT_ASSERT_EX( testMap.insert( (*m_parrString)[i], i ), i );
166 CPPUNIT_MSG( " Duration=" << timer.duration() );
168 CPPUNIT_MSG( " Insert/delete the key " << c_nGoalItem << " (" << c_nAttemptCount << " successful times)...");
169 CppUnitMini::ThreadPool pool( *this );
170 pool.add( new InserterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
171 pool.add( new DeleterThread( pool, testMap ), (c_nThreadCount + 1) / 2 );
173 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
175 size_t nInsertSuccess = 0;
176 size_t nInsertFailed = 0;
177 size_t nDeleteSuccess = 0;
178 size_t nDeleteFailed = 0;
179 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
180 InserterThread * pThread = dynamic_cast<InserterThread *>( *it );
182 CPPUNIT_CHECK( pThread->m_nInsertSuccess == c_nAttemptCount );
183 nInsertSuccess += pThread->m_nInsertSuccess;
184 nInsertFailed += pThread->m_nInsertFailed;
187 DeleterThread * p = static_cast<DeleterThread *>( *it );
188 CPPUNIT_CHECK( p->m_nDeleteSuccess == c_nAttemptCount );
189 nDeleteSuccess += p->m_nDeleteSuccess;
190 nDeleteFailed += p->m_nDeleteFailed;
193 CPPUNIT_CHECK_EX( nInsertSuccess == nDeleteSuccess, "nInsertSuccess=" << nInsertSuccess << ", nDeleteSuccess=" << nDeleteSuccess );
194 CPPUNIT_MSG( " Totals: Ins fail=" << nInsertFailed << " Del fail=" << nDeleteFailed );
196 // Check if the map contains all items
197 CPPUNIT_MSG( " Check if the map contains all items" );
199 for ( size_t i = 0; i < c_nMapSize; ++i ) {
200 CPPUNIT_CHECK_EX( testMap.find( (*m_parrString)[i] ), "Key \"" << (*m_parrString)[i] << "\" not found" );
202 CPPUNIT_MSG( " Duration=" << timer.duration() );
204 check_before_cleanup( testMap );
207 additional_check( testMap );
208 print_stat( testMap );
209 additional_cleanup( testMap );
215 m_parrString = &CppUnitMini::TestCase::getTestStrings();
216 if ( c_nMapSize > m_parrString->size() )
217 c_nMapSize = m_parrString->size();
218 if ( c_nGoalItem > m_parrString->size() )
219 c_nGoalItem = m_parrString->size() / 2;
221 CPPUNIT_MSG( "Thread count= " << c_nThreadCount
222 << " pass count=" << c_nAttemptCount
223 << " map size=" << c_nMapSize
226 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
227 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
228 Map testMap( c_nMapSize, nLoadFactor );
230 if ( c_bPrintGCState )
235 template <typename Map>
238 m_parrString = &CppUnitMini::TestCase::getTestStrings();
239 if ( c_nMapSize > m_parrString->size() )
240 c_nMapSize = m_parrString->size();
241 if ( c_nGoalItem > m_parrString->size() )
242 c_nGoalItem = m_parrString->size() / 2;
244 CPPUNIT_MSG( "Thread count= " << c_nThreadCount
245 << " pass count=" << c_nAttemptCount
246 << " map size=" << c_nMapSize
251 if ( c_bPrintGCState )
255 void setUpParams( const CppUnitMini::TestCfg& cfg );
257 void run_MichaelMap(const char *in_name, bool invert = false);
258 void run_SplitList(const char *in_name, bool invert = false);
259 void run_SkipListMap(const char *in_name, bool invert = false);
260 void run_StripedMap(const char *in_name, bool invert = false);
261 void run_RefinableMap(const char *in_name, bool invert = false);
262 void run_CuckooMap(const char *in_name, bool invert = false);
263 void run_EllenBinTreeMap(const char *in_name, bool invert = false);
264 void run_BronsonAVLTreeMap(const char *in_name, bool invert = false);
266 virtual void myRun(const char *in_name, bool invert = false);
268 # include "map2/map_defs.h"
269 CDSUNIT_DECLARE_MichaelMap
270 CDSUNIT_DECLARE_SplitList
271 CDSUNIT_DECLARE_SkipListMap
272 CDSUNIT_DECLARE_EllenBinTreeMap
273 CDSUNIT_DECLARE_BronsonAVLTreeMap
274 CDSUNIT_DECLARE_StripedMap
275 CDSUNIT_DECLARE_RefinableMap
276 CDSUNIT_DECLARE_CuckooMap
277 //CDSUNIT_DECLARE_StdMap // very slow!