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;
30 class Inserter: public CppUnitMini::TestThread
33 std::vector<size_t> m_arrVal;
35 virtual Inserter * clone()
37 return new Inserter( *this );
42 size_t const nSize = c_nMapSize / c_nThreadCount + 1;
43 m_arrVal.resize( nSize );
44 size_t nItem = m_nThreadNo;
45 for ( size_t i = 0; i < nSize; nItem += c_nThreadCount, ++i )
47 std::random_shuffle( m_arrVal.begin(), m_arrVal.end() );
50 size_t m_nInsertSuccess;
51 size_t m_nInsertFailed;
52 size_t m_nFindSuccess;
56 Inserter( CppUnitMini::ThreadPool& pool, Map& rMap )
57 : CppUnitMini::TestThread( pool )
60 Inserter( Inserter& src )
61 : CppUnitMini::TestThread( src )
65 Map_InsFind_int& getTest()
67 return reinterpret_cast<Map_InsFind_int&>( m_Pool.m_Test );
72 cds::threading::Manager::attachThread();
75 virtual void fini() { cds::threading::Manager::detachThread() ; }
86 size_t const nArrSize = m_arrVal.size();
87 for ( size_t i = 0; i < nArrSize; ++i ) {
88 size_t const nItem = m_arrVal[i];
89 if ( rMap.insert( nItem, nItem * 8 ) )
94 for ( size_t k = 0; k <= i; ++k ) {
95 if ( rMap.find( m_arrVal[k] ) )
107 void do_test( Map& testMap )
109 typedef Inserter<Map> InserterThread;
110 cds::OS::Timer timer;
112 CppUnitMini::ThreadPool pool( *this );
113 pool.add( new InserterThread( pool, testMap ), c_nThreadCount );
115 CPPUNIT_MSG( " Duration=" << pool.avgDuration() );
117 size_t nInsertSuccess = 0;
118 size_t nInsertFailed = 0;
119 size_t nFindSuccess = 0;
120 size_t nFindFailed = 0;
121 for ( CppUnitMini::ThreadPool::iterator it = pool.begin(); it != pool.end(); ++it ) {
122 InserterThread * pThread = static_cast<InserterThread *>( *it );
124 nInsertSuccess += pThread->m_nInsertSuccess;
125 nInsertFailed += pThread->m_nInsertFailed;
126 nFindSuccess += pThread->m_nFindSuccess;
127 nFindFailed += pThread->m_nFindFail;
130 CPPUNIT_MSG( " Totals: Ins succ=" << nInsertSuccess << " fail=" << nInsertFailed << "\n"
131 << " Find succ=" << nFindSuccess << " fail=" << nFindFailed
134 CPPUNIT_ASSERT( nInsertFailed == 0 );
135 CPPUNIT_ASSERT( nFindFailed == 0 );
138 additional_check( testMap );
139 print_stat( testMap );
140 additional_cleanup( testMap );
146 static_assert( (!std::is_same< typename Map::item_counter, cds::atomicity::empty_item_counter >::value),
147 "Empty item counter is not suitable for this test");
149 CPPUNIT_MSG( "Thread count: " << c_nThreadCount
150 << " map size=" << c_nMapSize
153 for ( size_t nLoadFactor = 1; nLoadFactor <= c_nMaxLoadFactor; nLoadFactor *= 2 ) {
154 CPPUNIT_MSG( "Load factor=" << nLoadFactor );
155 Map testMap( c_nMapSize, nLoadFactor );
157 if ( c_bPrintGCState )
165 static_assert( (!std::is_same< typename Map::item_counter, cds::atomicity::empty_item_counter >::value),
166 "Empty item counter is not suitable for this test");
168 CPPUNIT_MSG( "Thread count: " << c_nThreadCount
169 << " map size=" << c_nMapSize
174 if ( c_bPrintGCState )
178 void setUpParams( const CppUnitMini::TestCfg& cfg ) {
179 c_nThreadCount = cfg.getULong("ThreadCount", 0 );
180 c_nMapSize = cfg.getULong("MapSize", 10000 );
181 c_nMaxLoadFactor = cfg.getULong("MaxLoadFactor", 8 );
182 c_bPrintGCState = cfg.getBool("PrintGCStateFlag", true );
183 if ( c_nThreadCount == 0 )
184 c_nThreadCount = cds::OS::topology::processor_count();
187 # include "map2/map_defs.h"
188 CDSUNIT_DECLARE_MichaelMap
189 CDSUNIT_DECLARE_MichaelMap_nogc
190 CDSUNIT_DECLARE_SplitList
191 CDSUNIT_DECLARE_SplitList_nogc
192 CDSUNIT_DECLARE_SkipListMap
193 CDSUNIT_DECLARE_SkipListMap_nogc
194 CDSUNIT_DECLARE_EllenBinTreeMap
195 CDSUNIT_DECLARE_StripedMap
196 CDSUNIT_DECLARE_RefinableMap
197 CDSUNIT_DECLARE_CuckooMap
198 CDSUNIT_DECLARE_StdMap
200 CPPUNIT_TEST_SUITE( Map_InsFind_int )
201 CDSUNIT_TEST_MichaelMap
202 CDSUNIT_TEST_MichaelMap_nogc
203 CDSUNIT_TEST_SplitList
204 CDSUNIT_TEST_SplitList_nogc
205 CDSUNIT_TEST_SkipListMap
206 CDSUNIT_TEST_SkipListMap_nogc
207 CDSUNIT_TEST_EllenBinTreeMap
208 CDSUNIT_TEST_StripedMap
209 CDSUNIT_TEST_RefinableMap
210 CDSUNIT_TEST_CuckooMap
212 CPPUNIT_TEST_SUITE_END()
216 CPPUNIT_TEST_SUITE_REGISTRATION( Map_InsFind_int );