3 #include "map2/map_types.h"
4 #include "cppunit/thread.h"
6 #include <cds/os/topology.h>
8 #include <algorithm> // random_shuffle
12 # define TEST_MAP(X) void X() { test<MapTypes<key_type, value_type>::X >() ; }
13 # define TEST_MAP_NOLF(X) void X() { test_nolf<MapTypes<key_type, value_type>::X >() ; }
14 # define TEST_MAP_EXTRACT(X) TEST_MAP(X)
15 # define TEST_MAP_NOLF_EXTRACT(X) TEST_MAP_NOLF(X)
18 static size_t c_nMapSize = 1000000 ; // map size
19 static size_t c_nThreadCount = 4 ; // count of insertion thread
20 static size_t c_nMaxLoadFactor = 8 ; // maximum load factor
21 static bool c_bPrintGCState = true;
24 class Map_InsFind_int: public CppUnitMini::TestCase
26 typedef size_t key_type;
27 typedef size_t value_type;
29 template <typename Iterator, typename Map>
30 static bool check_result( Iterator const& it, Map const& map )
32 return it != map.end();
34 template <typename Map>
35 static bool check_result( bool b, Map const& )
41 class Inserter: public CppUnitMini::TestThread
44 std::vector<size_t> m_arrVal;
46 virtual Inserter * clone()
48 return new Inserter( *this );
53 size_t const nSize = c_nMapSize / c_nThreadCount + 1;
54 m_arrVal.resize( nSize );
55 size_t nItem = m_nThreadNo;
56 for ( size_t i = 0; i < nSize; nItem += c_nThreadCount, ++i )
58 std::random_shuffle( m_arrVal.begin(), m_arrVal.end() );
61 size_t m_nInsertSuccess;
62 size_t m_nInsertFailed;
63 size_t m_nFindSuccess;
67 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
68 : CppUnitMini::TestThread( pool )
71 Inserter( Inserter& src )
72 : CppUnitMini::TestThread( src )
76 Map_InsFind_int& getTest()
78 return reinterpret_cast<Map_InsFind_int&>( m_Pool.m_Test );
83 cds::threading::Manager::attachThread();
86 virtual void fini() { cds::threading::Manager::detachThread() ; }
97 size_t const nArrSize = m_arrVal.size();
98 for ( size_t i = 0; i < nArrSize; ++i ) {
99 size_t const nItem = m_arrVal[i];
100 if ( check_result( rMap.insert( nItem, nItem * 8 ), rMap ))
105 for ( size_t k = 0; k <= i; ++k ) {
106 if ( check_result( rMap.find( m_arrVal[k] ), rMap ))
118 void do_test( Map& testMap )
120 typedef Inserter<Map> InserterThread;
121 cds::OS::Timer timer;
123 CppUnitMini::ThreadPool pool( *this );
124 pool.add( new InserterThread( pool, testMap ), c_nThreadCount );
126 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
128 size_t nInsertSuccess = 0;
129 size_t nInsertFailed = 0;
130 size_t nFindSuccess = 0;
131 size_t nFindFailed = 0;
132 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
133 InserterThread * pThread = static_cast<InserterThread *>( *it );
135 nInsertSuccess += pThread->m_nInsertSuccess;
136 nInsertFailed += pThread->m_nInsertFailed;
137 nFindSuccess += pThread->m_nFindSuccess;
138 nFindFailed += pThread->m_nFindFail;
141 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess << " fail=" << nInsertFailed << "\n"
142 << " Find succ=" << nFindSuccess << " fail=" << nFindFailed
145 CPPUNIT_CHECK( nInsertFailed == 0 );
146 CPPUNIT_CHECK( nFindFailed == 0 );
148 check_before_cleanup( testMap );
151 additional_check( testMap );
152 print_stat( testMap );
153 additional_cleanup( testMap );
159 static_assert( (!std::is_same< typename Map::item_counter, cds::atomicity::empty_item_counter >::value),
160 "Empty item counter is not suitable for this test");
162 CPPUNIT_MSG( "Thread count: " << c_nThreadCount
163 << " map size=" << c_nMapSize
166 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
167 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
168 Map testMap( c_nMapSize, nLoadFactor );
170 if ( c_bPrintGCState )
178 static_assert( (!std::is_same< typename Map::item_counter, cds::atomicity::empty_item_counter >::value),
179 "Empty item counter is not suitable for this test");
181 CPPUNIT_MSG( "Thread count: " << c_nThreadCount
182 << " map size=" << c_nMapSize
187 if ( c_bPrintGCState )
191 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
192 c_nThreadCount = cfg.getULong("ThreadCount", 0 );
193 c_nMapSize = cfg.getULong("MapSize", 10000 );
194 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
195 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
196 if ( c_nThreadCount == 0 )
197 c_nThreadCount = cds::OS::topology::processor_count();
200 # include "map2/map_defs.h"
201 CDSUNIT_DECLARE_MichaelMap
202 CDSUNIT_DECLARE_MichaelMap_nogc
203 CDSUNIT_DECLARE_SplitList
204 CDSUNIT_DECLARE_SplitList_nogc
205 CDSUNIT_DECLARE_SkipListMap
206 CDSUNIT_DECLARE_SkipListMap_nogc
207 CDSUNIT_DECLARE_EllenBinTreeMap
208 CDSUNIT_DECLARE_BronsonAVLTreeMap
209 CDSUNIT_DECLARE_StripedMap
210 CDSUNIT_DECLARE_RefinableMap
211 CDSUNIT_DECLARE_CuckooMap
212 CDSUNIT_DECLARE_StdMap
214 CPPUNIT_TEST_SUITE( Map_InsFind_int )
215 CDSUNIT_TEST_MichaelMap
216 CDSUNIT_TEST_MichaelMap_nogc
217 CDSUNIT_TEST_SplitList
218 CDSUNIT_TEST_SplitList_nogc
219 CDSUNIT_TEST_SkipListMap
220 CDSUNIT_TEST_SkipListMap_nogc
221 CDSUNIT_TEST_EllenBinTreeMap
222 CDSUNIT_TEST_BronsonAVLTreeMap
223 CDSUNIT_TEST_StripedMap
224 CDSUNIT_TEST_RefinableMap
225 CDSUNIT_TEST_CuckooMap
227 CPPUNIT_TEST_SUITE_END()
231 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsFind_int );