2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 class Set_InsDelFind: public cds_test::stress_fixture
38 static size_t s_nSetSize; // initial set size
39 static size_t s_nThreadCount; // thread count
40 static size_t s_nMaxLoadFactor; // maximum load factor
41 static unsigned int s_nInsertPercentage;
42 static unsigned int s_nDeletePercentage;
43 static unsigned int s_nDuration; // test duration, seconds
45 static size_t s_nCuckooInitialSize; // initial size for CuckooSet
46 static size_t s_nCuckooProbesetSize; // CuckooSet probeset size (only for list-based probeset)
47 static size_t s_nCuckooProbesetThreshold; // CUckooSet probeset threshold (0 - use default)
49 static size_t s_nFeldmanSet_HeadBits;
50 static size_t s_nFeldmanSet_ArrayBits;
52 static size_t s_nLoadFactor;
54 static void SetUpTestCase();
55 //static void TearDownTestCase();
64 static const unsigned int c_nShuffleSize = 100;
65 actions m_arrShuffle[c_nShuffleSize];
68 typedef size_t key_type;
69 typedef size_t value_type;
72 class Worker: public cds_test::thread
74 typedef cds_test::thread base_class;
78 size_t m_nInsertSuccess = 0;
79 size_t m_nInsertFailed = 0;
80 size_t m_nDeleteSuccess = 0;
81 size_t m_nDeleteFailed = 0;
82 size_t m_nFindSuccess = 0;
83 size_t m_nFindFailed = 0;
86 Worker( cds_test::thread_pool& pool, Set& set )
96 virtual thread * clone()
98 return new Worker( *this );
104 Set_InsDelFind& fixture = pool().template fixture<Set_InsDelFind>();
106 actions * pAct = fixture.m_arrShuffle;
108 size_t const nNormalize = size_t(-1) / ( fixture.s_nSetSize * 2);
110 typedef typename Set::value_type value_type;
113 while ( !time_elapsed()) {
114 nRand = cds::bitop::RandXorShift(nRand);
115 size_t n = nRand / nNormalize;
118 if ( rSet.contains( n ))
124 if ( rSet.insert( n ))
130 if ( rSet.erase( n ))
137 if ( ++i >= c_nShuffleSize )
145 void do_test( Set& testSet )
147 typedef Worker<Set> work_thread;
149 // fill map - only odd number
151 size_t * pInitArr = new size_t[ s_nSetSize ];
152 size_t * pEnd = pInitArr + s_nSetSize;
153 for ( size_t i = 0; i < s_nSetSize; ++i )
154 pInitArr[i] = i * 2 + 1;
155 shuffle( pInitArr, pEnd );
156 for ( size_t * p = pInitArr; p < pEnd; ++p )
157 testSet.insert( typename Set::value_type( *p, *p ));
161 cds_test::thread_pool& pool = get_pool();
162 pool.add( new work_thread( pool, testSet ), s_nThreadCount );
164 propout() << std::make_pair( "thread_count", s_nThreadCount )
165 << std::make_pair( "set_size", s_nSetSize )
166 << std::make_pair( "insert_percentage", s_nInsertPercentage )
167 << std::make_pair( "delete_percentage", s_nDeletePercentage )
168 << std::make_pair( "total_duration", s_nDuration );
170 std::chrono::milliseconds duration = pool.run( std::chrono::seconds( s_nDuration ));
172 propout() << std::make_pair( "duration", duration );
174 size_t nInsertSuccess = 0;
175 size_t nInsertFailed = 0;
176 size_t nDeleteSuccess = 0;
177 size_t nDeleteFailed = 0;
178 size_t nFindSuccess = 0;
179 size_t nFindFailed = 0;
180 for ( size_t i = 0; i < pool.size(); ++i ) {
181 work_thread& thr = static_cast<work_thread&>( pool.get( i ));
182 nInsertSuccess += thr.m_nInsertSuccess;
183 nInsertFailed += thr.m_nInsertFailed;
184 nDeleteSuccess += thr.m_nDeleteSuccess;
185 nDeleteFailed += thr.m_nDeleteFailed;
186 nFindSuccess += thr.m_nFindSuccess;
187 nFindFailed += thr.m_nFindFailed;
191 << std::make_pair( "insert_success", nInsertSuccess )
192 << std::make_pair( "insert_failed", nInsertFailed )
193 << std::make_pair( "delete_success", nDeleteSuccess )
194 << std::make_pair( "delete_failed", nDeleteFailed )
195 << std::make_pair( "find_success", nFindSuccess )
196 << std::make_pair( "find_failed", nFindFailed );
199 ASSERT_TRUE( std::chrono::duration_cast<std::chrono::seconds>(duration).count() > 0 );
200 size_t nTotalOps = nInsertSuccess + nInsertFailed + nDeleteSuccess + nDeleteFailed + nFindSuccess + nFindFailed;
201 propout() << std::make_pair( "avg_speed", nTotalOps / std::chrono::duration_cast<std::chrono::seconds>(duration).count());
206 EXPECT_TRUE( testSet.empty()) << "set size=" << testSet.size();
208 additional_check( testSet );
209 print_stat( propout(), testSet );
210 additional_cleanup( testSet );
221 class Set_InsDelFind_LF: public Set_InsDelFind
222 , public ::testing::WithParamInterface<size_t>
228 s_nLoadFactor = GetParam();
229 propout() << std::make_pair( "load_factor", s_nLoadFactor );
230 Set_InsDelFind::run_test<Set>();
233 static std::vector<size_t> get_load_factors();