3 namespace junction_test {
5 class JunctionMapInsDelFindTest_Parallel : public cds_test::stress_fixture {
7 static unsigned s_nInsertPercentage;
8 static unsigned s_nDeletePercentage;
9 static size_t s_nGCFrequency; // Run GC after "s_nGCFrequency" operations.
10 static size_t s_nThreadCount;
11 static size_t s_nMapKeyRange;
12 static size_t s_nCrudeMapCapacity;
14 enum actions { do_find, do_insert, do_delete };
15 static const unsigned int kShuffleSize = 100;
16 static actions s_arrShuffle[kShuffleSize];
18 static size_t s_nCrudePassCount;
19 static size_t s_nGrampaPassCount;
20 static size_t s_nLinearPassCount;
21 static size_t s_nLeapfrogPassCount;
23 static void InitShuffleArray() {
24 // Build an array of shuffled actions.
25 EXPECT_LE(s_nInsertPercentage + s_nDeletePercentage, 100);
26 actions* pFirst = s_arrShuffle;
27 actions* pLast = s_arrShuffle + s_nInsertPercentage;
28 std::fill(pFirst, pLast, do_insert);
30 pLast += s_nDeletePercentage;
31 std::fill(pFirst, pLast, do_delete);
33 pLast = s_arrShuffle + sizeof(s_arrShuffle) / sizeof(s_arrShuffle[0]);
35 std::fill(pFirst, pLast, do_find);
37 std::random_device rd;
39 std::shuffle(s_arrShuffle, pLast, g);
42 static void SetUpTestCase() {
44 const cds_test::config& cfg = get_config("ParallelJunction");
45 GetConfigNonZeroExpected(InsertPercentage, 5);
46 GetConfigNonZeroExpected(DeletePercentage, 5);
47 GetConfigNonZeroExpected(ThreadCount, 4);
48 GetConfigNonZeroExpected(MapKeyRange, 20000);
49 GetConfigNonZeroExpected(CrudeMapCapacity, s_nMapKeyRange * 64);
50 GetConfigNonZeroExpected(GCFrequency, 1500);
51 GetConfigNonZeroExpected(CrudePassCount, 1500000000);
52 GetConfigNonZeroExpected(GrampaPassCount, 650000000);
53 GetConfigNonZeroExpected(LinearPassCount, 900000000);
54 GetConfigNonZeroExpected(LeapfrogPassCount, 850000000);
57 template <typename Map> static void run_test(Map* map, size_t pass_count) {
58 auto qsbrContext = junction::DefaultQSBR.createContext();
60 std::random_device rd;
61 std::mt19937 gen(rd());
62 std::uniform_int_distribution<size_t> dis(2, s_nMapKeyRange);
64 unsigned action_index = 0;
65 size_t nInsertedNum = 0;
66 size_t nFindSuccess = 0;
67 size_t nOperations = 0;
69 for (size_t count = 0; count < pass_count; count++) {
70 // The number to operate on the map.
71 size_t key = dis(gen);
72 switch (s_arrShuffle[action_index]) {
74 size_t val = dis(gen);
75 if (map_insert(map, key, val)) {
85 if (map_find(map, key)) {
92 if (++action_index >= kShuffleSize) {
95 if (++nOperations > s_nGCFrequency) {
96 junction::DefaultQSBR.update(qsbrContext);
100 junction::DefaultQSBR.update(qsbrContext);
101 junction::DefaultQSBR.destroyContext(qsbrContext);
105 size_t JunctionMapInsDelFindTest_Parallel::s_nThreadCount;
106 size_t JunctionMapInsDelFindTest_Parallel::s_nMapKeyRange;
107 size_t JunctionMapInsDelFindTest_Parallel::s_nCrudeMapCapacity;
108 size_t JunctionMapInsDelFindTest_Parallel::s_nGCFrequency;
109 unsigned JunctionMapInsDelFindTest_Parallel::s_nInsertPercentage;
110 unsigned JunctionMapInsDelFindTest_Parallel::s_nDeletePercentage;
111 const unsigned int JunctionMapInsDelFindTest_Parallel::kShuffleSize;
112 JunctionMapInsDelFindTest_Parallel::actions JunctionMapInsDelFindTest_Parallel::
113 s_arrShuffle[JunctionMapInsDelFindTest_Parallel::kShuffleSize];
114 size_t JunctionMapInsDelFindTest_Parallel::s_nCrudePassCount;
115 size_t JunctionMapInsDelFindTest_Parallel::s_nGrampaPassCount;
116 size_t JunctionMapInsDelFindTest_Parallel::s_nLinearPassCount;
117 size_t JunctionMapInsDelFindTest_Parallel::s_nLeapfrogPassCount;
119 #define JunctionThreading(map_type, pass_count) \
120 std::unique_ptr<std::thread[]> threads(new std::thread[s_nThreadCount]); \
121 for (size_t i = 0; i < s_nThreadCount; i++) { \
122 threads[i] = std::thread(run_test<map_type>, map.get(), pass_count); \
124 for (size_t i = 0; i < s_nThreadCount; i++) { \
128 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapCrude) {
129 std::unique_ptr<CrudeMap> map(new CrudeMap(s_nCrudeMapCapacity));
130 JunctionThreading(CrudeMap, s_nCrudePassCount);
133 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapLeapfrog) {
134 std::unique_ptr<LeapfrogMap> map(new LeapfrogMap());
135 JunctionThreading(LeapfrogMap, s_nLeapfrogPassCount);
138 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapLinear) {
139 std::unique_ptr<LinearMap> map(new LinearMap());
140 JunctionThreading(LinearMap, s_nLinearPassCount);
143 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapGrampa) {
144 std::unique_ptr<GrampaMap> map(new GrampaMap());
145 JunctionThreading(GrampaMap, s_nGrampaPassCount);
148 } // namespace junction_test