3 // defines concurrent access to map::nonconcurrent_iterator::Sequence::TValue::nAccess field
5 #include "map2/map_types.h"
6 #include "cppunit/thread.h"
9 #include <algorithm> // random_shuffle
11 // find int test in map<int> in mutithreaded mode
14 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
15 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
16 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
17 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
20 static size_t c_nThreadCount = 8 ; // thread count
21 static size_t c_nMapSize = 20000000 ; // map size (count of searching item)
22 static size_t c_nPercentExists = 50 ; // percent of existing keys in searching sequence
23 static size_t c_nPassCount = 2;
24 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
25 static bool c_bPrintGCState = true;
28 class Map_find_int: public CppUnitMini::TestCase
30 typedef size_t key_type;
32 key_type nKey ; // key
33 bool bExists ; // true - key in map, false - key not in map
36 typedef std::vector<value_type> ValueVector;
38 size_t m_nRealMapSize;
39 bool m_bSequenceInitialized;
41 void generateSequence()
43 size_t nPercent = c_nPercentExists;
47 else if ( nPercent < 1 )
52 m_Arr.resize( c_nMapSize );
53 for ( size_t i = 0; i < c_nMapSize; ++i ) {
54 m_Arr[i].nKey = i * 13;
55 m_Arr[i].bExists = CppUnitMini::Rand( 100 ) <= nPercent;
56 if ( m_Arr[i].bExists )
59 std::random_shuffle( m_Arr.begin(), m_Arr.end() );
63 class TestThread: public CppUnitMini::TestThread
67 virtual TestThread * clone()
69 return new TestThread( *this );
86 TestThread( CppUnitMini::ThreadPool& pool, Map& rMap )
87 : CppUnitMini::TestThread( pool )
90 TestThread( TestThread& src )
91 : CppUnitMini::TestThread( src )
95 Map_find_int& getTest()
97 return reinterpret_cast<Map_find_int&>( m_Pool.m_Test );
100 virtual void init() { cds::threading::Manager::attachThread() ; }
101 virtual void fini() { cds::threading::Manager::detachThread() ; }
105 ValueVector& arr = getTest().m_Arr;
106 //size_t nSize = arr.size();
109 for ( size_t nPass = 0; nPass < c_nPassCount; ++nPass ) {
110 if ( m_nThreadNo & 1 ) {
111 ValueVector::const_iterator itEnd = arr.end();
112 for ( ValueVector::const_iterator it = arr.begin(); it != itEnd; ++it ) {
113 bool bFound = rMap.find( it->nKey );
116 ++m_KeyExists.nSuccess;
118 //rMap.find( it->nKey );
119 ++m_KeyExists.nFailed;
124 //rMap.find( it->nKey );
125 ++m_KeyNotExists.nFailed;
128 ++m_KeyNotExists.nSuccess;
133 ValueVector::const_reverse_iterator itEnd = arr.rend();
134 for ( ValueVector::const_reverse_iterator it = arr.rbegin(); it != itEnd; ++it ) {
135 bool bFound = rMap.find( it->nKey );
138 ++m_KeyExists.nSuccess;
140 //rMap.find( it->nKey );
141 ++m_KeyExists.nFailed;
146 //rMap.find( it->nKey );
147 ++m_KeyNotExists.nFailed;
150 ++m_KeyNotExists.nSuccess;
161 void find_int_test( Map& testMap )
163 typedef TestThread<Map> Thread;
164 cds::OS::Timer timer;
167 CPPUNIT_MSG( " Fill map with " << m_Arr.size() << " items...");
169 for ( size_t i = 0; i < m_Arr.size(); ++i ) {
170 if ( m_Arr[i].bExists ) {
171 CPPUNIT_ASSERT( testMap.insert( m_Arr[i].nKey, m_Arr[i] ) );
174 CPPUNIT_MSG( " Duration=" << timer.duration() );
176 CPPUNIT_MSG( " Searching...");
177 CppUnitMini::ThreadPool pool( *this );
178 pool.add( new Thread( pool, testMap ), c_nThreadCount );
180 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
182 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
183 Thread * pThread = static_cast<Thread *>( *it );
184 CPPUNIT_ASSERT( pThread->m_KeyExists.nFailed == 0 );
185 CPPUNIT_ASSERT( pThread->m_KeyExists.nSuccess == m_nRealMapSize * c_nPassCount );
186 CPPUNIT_ASSERT( pThread->m_KeyNotExists.nFailed == 0 );
187 CPPUNIT_ASSERT( pThread->m_KeyNotExists.nSuccess == (m_Arr.size() - m_nRealMapSize) * c_nPassCount );
191 additional_check( testMap );
192 print_stat( testMap );
193 additional_cleanup( testMap );
196 void initTestSequence()
198 CPPUNIT_MSG( "Generating test data...");
199 cds::OS::Timer timer;
201 CPPUNIT_MSG( " Duration=" << timer.duration() );
202 CPPUNIT_MSG( "Map size=" << m_nRealMapSize << " find key loop=" << m_Arr.size() << " (" << c_nPercentExists << "% success)" );
203 CPPUNIT_MSG( "Thread count=" << c_nThreadCount << " Pass count=" << c_nPassCount );
205 m_bSequenceInitialized = true;
211 if ( !m_bSequenceInitialized )
214 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
215 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
216 Map testMap( c_nMapSize, nLoadFactor );
217 find_int_test( testMap );
218 if ( c_bPrintGCState )
226 if ( !m_bSequenceInitialized )
230 find_int_test( testMap );
231 if ( c_bPrintGCState )
235 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
236 c_nThreadCount = cfg.getULong("ThreadCount", 8 ) ; // thread count
237 c_nMapSize = cfg.getULong("MapSize", 20000000 ) ; // map size (count of searching item)
238 c_nPercentExists = cfg.getULong("PercentExists", 50 ) ; // percent of existing keys in searching sequence
239 c_nPassCount = cfg.getULong("PassCount", 2 );
240 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
241 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
247 : m_bSequenceInitialized( false )
250 # include "map2/map_defs.h"
251 CDSUNIT_DECLARE_MichaelMap
252 CDSUNIT_DECLARE_MichaelMap_nogc
253 CDSUNIT_DECLARE_SplitList
254 CDSUNIT_DECLARE_SplitList_nogc
255 CDSUNIT_DECLARE_SkipListMap
256 CDSUNIT_DECLARE_SkipListMap_nogc
257 CDSUNIT_DECLARE_EllenBinTreeMap
258 CDSUNIT_DECLARE_StripedMap
259 CDSUNIT_DECLARE_RefinableMap
260 CDSUNIT_DECLARE_CuckooMap
261 CDSUNIT_DECLARE_StdMap
263 CPPUNIT_TEST_SUITE( Map_find_int )
264 CDSUNIT_TEST_MichaelMap
265 CDSUNIT_TEST_MichaelMap_nogc
266 CDSUNIT_TEST_SplitList
267 CDSUNIT_TEST_SplitList_nogc
268 CDSUNIT_TEST_SkipListMap
269 CDSUNIT_TEST_SkipListMap_nogc
270 CDSUNIT_TEST_EllenBinTreeMap
271 CDSUNIT_TEST_StripedMap
272 CDSUNIT_TEST_RefinableMap
273 CDSUNIT_TEST_CuckooMap
275 CPPUNIT_TEST_SUITE_END()
278 CPPUNIT_TEST_SUITE_REGISTRATION( Map_find_int );