8dadf63bd30e3b337997a6c4f5c05f17d3924fc2
[junction.git] / test / junction_parallel_driver.cpp
1 #include "test.h"
2
3 namespace junction_test {
4
5 class JunctionMapInsDelFindTest_Parallel : public cds_test::stress_fixture {
6 protected:
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;
13
14   enum actions { do_find, do_insert, do_delete };
15   static const unsigned int kShuffleSize = 100;
16   static actions s_arrShuffle[kShuffleSize];
17
18   static size_t s_nCrudePassCount;
19   static size_t s_nGrampaPassCount;
20   static size_t s_nLinearPassCount;
21   static size_t s_nLeapfrogPassCount;
22
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);
29     pFirst = pLast;
30     pLast += s_nDeletePercentage;
31     std::fill(pFirst, pLast, do_delete);
32     pFirst = pLast;
33     pLast = s_arrShuffle + sizeof(s_arrShuffle) / sizeof(s_arrShuffle[0]);
34     if (pFirst < pLast) {
35       std::fill(pFirst, pLast, do_find);
36     }
37     std::random_device rd;
38     std::mt19937 g(rd());
39     std::shuffle(s_arrShuffle, pLast, g);
40   }
41
42   static void SetUpTestCase() {
43     InitShuffleArray();
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     // CrudeMap may need a big enough capacity in case may insertion operations
50     // got jammed up.
51     GetConfigNonZeroExpected(CrudeMapCapacity, s_nMapKeyRange * 1024);
52     GetConfigNonZeroExpected(GCFrequency, 1500);
53     GetConfigNonZeroExpected(CrudePassCount, 1500000000);
54     GetConfigNonZeroExpected(GrampaPassCount, 650000000);
55     GetConfigNonZeroExpected(LinearPassCount, 900000000);
56     GetConfigNonZeroExpected(LeapfrogPassCount, 850000000);
57   }
58
59   template <typename Map>
60   static void run_test(Map* map, size_t pass_count, size_t* inserted_num,
61                        size_t* deleted_num) {
62     auto qsbrContext = junction::DefaultQSBR.createContext();
63
64     std::random_device rd;
65     std::mt19937 gen(rd());
66     std::uniform_int_distribution<size_t> dis(2, s_nMapKeyRange);
67
68     unsigned action_index = 0;
69     size_t nInsertedNum = 0;
70     size_t nDeletedNum = 0;
71     size_t nFindSuccess = 0;
72     size_t nOperations = 0;
73
74     for (size_t count = 0; count < pass_count; count++) {
75       // The number to operate on the map.
76       size_t key = dis(gen);
77       switch (s_arrShuffle[action_index]) {
78         case do_insert: {
79           size_t val = dis(gen);
80           if (map_insert(map, key, val)) {
81             nInsertedNum++;
82           }
83           break;
84         }
85         case do_delete: {
86           if (map_delete(map, key)) {
87             nDeletedNum++;
88           }
89           break;
90         }
91         case do_find: {
92           if (map_find(map, key)) {
93             ++nFindSuccess;
94           }
95           break;
96         }
97         default: { break; }
98       }
99       if (++action_index >= kShuffleSize) {
100         action_index = 0;
101       }
102       if (++nOperations > s_nGCFrequency) {
103         junction::DefaultQSBR.update(qsbrContext);
104         nOperations = 0;
105       }
106     }
107     junction::DefaultQSBR.update(qsbrContext);
108     junction::DefaultQSBR.destroyContext(qsbrContext);
109     *inserted_num = nInsertedNum;
110     *deleted_num = nDeletedNum;
111   }
112
113   template <typename Map>
114   void JunctionThreading(Map* map, size_t pass_count) {
115     std::unique_ptr<std::thread[]> threads(new std::thread[s_nThreadCount]);
116     std::unique_ptr<size_t[]> inserted_nums(new size_t[s_nThreadCount]);
117     std::unique_ptr<size_t[]> deleted_nums(new size_t[s_nThreadCount]);
118     for (size_t i = 0; i < s_nThreadCount; i++) {
119       threads[i] = std::thread(run_test<Map>, map, pass_count,
120                                &inserted_nums[i], &deleted_nums[i]);
121     }
122     size_t inserted_sum = 0;
123     size_t deleted_sum = 0;
124     for (size_t i = 0; i < s_nThreadCount; i++) {
125       threads[i].join();
126       inserted_sum += inserted_nums[i];
127       deleted_sum += deleted_nums[i];
128     }
129     EXPECT_LE(deleted_sum, inserted_sum);
130   }
131 };
132
133 size_t JunctionMapInsDelFindTest_Parallel::s_nThreadCount;
134 size_t JunctionMapInsDelFindTest_Parallel::s_nMapKeyRange;
135 size_t JunctionMapInsDelFindTest_Parallel::s_nCrudeMapCapacity;
136 size_t JunctionMapInsDelFindTest_Parallel::s_nGCFrequency;
137 unsigned JunctionMapInsDelFindTest_Parallel::s_nInsertPercentage;
138 unsigned JunctionMapInsDelFindTest_Parallel::s_nDeletePercentage;
139 const unsigned int JunctionMapInsDelFindTest_Parallel::kShuffleSize;
140 JunctionMapInsDelFindTest_Parallel::actions JunctionMapInsDelFindTest_Parallel::
141     s_arrShuffle[JunctionMapInsDelFindTest_Parallel::kShuffleSize];
142 size_t JunctionMapInsDelFindTest_Parallel::s_nCrudePassCount;
143 size_t JunctionMapInsDelFindTest_Parallel::s_nGrampaPassCount;
144 size_t JunctionMapInsDelFindTest_Parallel::s_nLinearPassCount;
145 size_t JunctionMapInsDelFindTest_Parallel::s_nLeapfrogPassCount;
146
147 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapCrude) {
148   std::unique_ptr<CrudeMap> map(new CrudeMap(s_nCrudeMapCapacity));
149   JunctionThreading(map.get(), s_nCrudePassCount);
150 }
151
152 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapLeapfrog) {
153   std::unique_ptr<LeapfrogMap> map(new LeapfrogMap());
154   JunctionThreading(map.get(), s_nLeapfrogPassCount);
155 }
156
157 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapLinear) {
158   std::unique_ptr<LinearMap> map(new LinearMap());
159   JunctionThreading(map.get(), s_nLinearPassCount);
160 }
161
162 TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapGrampa) {
163   std::unique_ptr<GrampaMap> map(new GrampaMap());
164   JunctionThreading(map.get(), s_nGrampaPassCount);
165 }
166
167 } // namespace junction_test