Adds parallel junction map test cases
[junction.git] / test / junction_parallel_driver.cpp
1 #include <junction/ConcurrentMap_Crude.h>
2 #include <junction/ConcurrentMap_Grampa.h>
3 #include <junction/ConcurrentMap_Leapfrog.h>
4 #include <junction/ConcurrentMap_Linear.h>
5
6 #include <gtest/gtest.h>
7
8 #include <algorithm>
9 #include <cassert>
10 #include <chrono>
11 #include <iostream>
12 #include <memory>
13 #include <random>
14
15 namespace junction_test {
16
17 class JunctionMapInsDelFindTest : public ::testing::Test {
18 protected:
19   typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
20   typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
21   typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
22   typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
23
24   static const unsigned s_nInsertPercentage = 5;
25   static const unsigned s_nDeletePercentage = 5;
26
27   // Run GC after "kGCFrequency" operations.
28   const size_t kGCFrequency = 1500;
29   const size_t kMapSize = 20000;
30
31   enum actions { do_find, do_insert, do_delete };
32   static const unsigned int kShuffleSize = 100;
33   static actions s_arrShuffle[kShuffleSize];
34
35   const size_t kCrudePassCount = 400000;
36   const size_t kGrampaPassCount = 60000;
37   const size_t kLinearPassCount = 70000;
38   const size_t kLeapfrogPassCount = 75000;
39
40   static void SetUpTestCase() {
41     // Build an array of shuffled actions.
42     EXPECT_LE(s_nInsertPercentage + s_nDeletePercentage, 100);
43     actions* pFirst = s_arrShuffle;
44     actions* pLast = s_arrShuffle + s_nInsertPercentage;
45     std::fill(pFirst, pLast, do_insert);
46     pFirst = pLast;
47     pLast += s_nDeletePercentage;
48     std::fill(pFirst, pLast, do_delete);
49     pFirst = pLast;
50     pLast = s_arrShuffle + sizeof(s_arrShuffle) / sizeof(s_arrShuffle[0]);
51     if (pFirst < pLast) {
52       std::fill(pFirst, pLast, do_find);
53     }
54     std::random_device rd;
55     std::mt19937 g(rd());
56     std::shuffle(s_arrShuffle, pLast, g);
57   }
58
59   template <typename Map, typename Key, typename Value>
60   static bool map_insert(Map* map, Key key, Value value) {
61     auto iter = map->insertOrFind(key);
62     if (!iter.getValue()) {
63       iter.assignValue(value);
64       return true;
65     } else {
66       return false;
67     }
68   }
69
70   template <typename Map, typename Key>
71   static bool map_delete(Map* map, Key key) {
72     auto iter = map->find(key);
73     if (iter.getValue()) {
74       iter.eraseValue();
75       return true;
76     } else {
77       return false;
78     }
79   }
80
81   template <typename Map, typename Key>
82   static bool map_find(Map* map, Key key) {
83     auto iter = map->find(key);
84     if (iter.getValue()) {
85       return true;
86     } else {
87       return false;
88     }
89   }
90
91   // Specialization for CrudeMap
92   template <typename Key, typename Value>
93   static bool map_insert(CrudeMap* map, Key key, Value value) {
94     if (!map->get(key)) {
95       map->assign(key, value);
96       return true;
97     } else {
98       return false;
99     }
100   }
101
102   template <typename Key> static bool map_delete(CrudeMap* map, Key key) {
103     if (!map->get(key)) {
104       map->assign(key, ((Key)0));
105       return true;
106     } else {
107       return false;
108     }
109   }
110
111   template <typename Key> static bool map_find(CrudeMap* map, Key key) {
112     return map->get(key) != ((Key)0);
113   }
114
115   template <typename Map>
116   void run_test(Map* map, size_t pass_count) {
117     auto qsbrContext = junction::DefaultQSBR.createContext();
118
119     std::random_device rd;
120     std::mt19937 gen(rd());
121     std::uniform_int_distribution<size_t> dis(kMapSize, 2 * kMapSize);
122
123     unsigned action_index = 0;
124     size_t nInsertedNum = 0;
125     size_t nFindSuccess = 0;
126     size_t nOperations = 0;
127
128     for (size_t count = 0; count < pass_count; count++) {
129       for (size_t i = 0; i < kMapSize; ++i) {
130         // The number to operate on the map.
131         size_t n = dis(gen);
132         switch (s_arrShuffle[action_index]) {
133           case do_insert: {
134             if (map_insert(map, n, n)) {
135               nInsertedNum++;
136             }
137             break;
138           }
139           case do_delete: {
140             map_delete(map, n);
141             break;
142           }
143           case do_find: {
144             if (map_find(map, n)) {
145               ++nFindSuccess;
146             }
147             break;
148           }
149           default: { break; }
150         }
151         if (++action_index >= kShuffleSize) {
152           action_index = 0;
153         }
154         if (++nOperations > kGCFrequency) {
155           junction::DefaultQSBR.update(qsbrContext);
156           nOperations = 0;
157         }
158       }
159     }
160     junction::DefaultQSBR.update(qsbrContext);
161     junction::DefaultQSBR.destroyContext(qsbrContext);
162   }
163 };
164
165 const unsigned JunctionMapInsDelFindTest::s_nInsertPercentage;
166 const unsigned JunctionMapInsDelFindTest::s_nDeletePercentage;
167 const unsigned int JunctionMapInsDelFindTest::kShuffleSize;
168 JunctionMapInsDelFindTest::actions JunctionMapInsDelFindTest::s_arrShuffle
169     [JunctionMapInsDelFindTest::kShuffleSize];
170
171 TEST_F(JunctionMapInsDelFindTest, JunctionMapCrude) {
172   std::unique_ptr<CrudeMap> map(new CrudeMap(kMapSize * 32));
173   run_test(map.get(), kCrudePassCount);
174 }
175
176 TEST_F(JunctionMapInsDelFindTest, JunctionMapLeapfrog) {
177   std::unique_ptr<LeapfrogMap> map(new LeapfrogMap());
178   run_test(map.get(), kLeapfrogPassCount);
179 }
180
181 TEST_F(JunctionMapInsDelFindTest, JunctionMapLinear) {
182   std::unique_ptr<LinearMap> map(new LinearMap());
183   run_test(map.get(), kLinearPassCount);
184 }
185
186 TEST_F(JunctionMapInsDelFindTest, JunctionMapGrampa) {
187   std::unique_ptr<GrampaMap> map(new GrampaMap());
188   run_test(map.get(), kGrampaPassCount);
189 }
190
191 } // namespace junction_test
192
193 int main(int argc, char** argv) {
194   // Init Google test
195   ::testing::InitGoogleTest(&argc, argv);
196   int result = RUN_ALL_TESTS();
197   return 0;
198 }