Leapfrog uses more frequent GC
[junction.git] / test / junction_driver.cpp
1 #include <junction/ConcurrentMap_Grampa.h>
2 #include <junction/ConcurrentMap_Linear.h>
3 #include <junction/ConcurrentMap_Leapfrog.h>
4 #include <junction/ConcurrentMap_Crude.h>
5 #include <iostream>
6 #include <memory>
7 #include <chrono>
8 #include <cassert>
9
10 namespace {
11
12 const unsigned s_nInsertPercentage = 10;
13 const char* kTestName = "InsDelFind";
14 // Run GC after "kGCFrequency" operations.
15 const size_t kGCFrequency = 3000;
16 const size_t kLeapfrogGCFrequency = 1500;
17
18 const size_t kCrudeMapSize = 10000;
19 const size_t kCrudePassCount = 40000;
20 const char* kCrudeBenchmarkName = "JunctionMapCrude";
21
22 const size_t kGrampaMapSize = 20000;
23 const size_t kGrampaPassCount = 30000;
24 const char* kGrampaBenchmarkName = "JunctionMapGrampa";
25
26 const size_t kLinearMapSize = 20000;
27 const size_t kLinearPassCount = 70000;
28 const char* kLinearBenchmarkName = "JunctionMapLinear";
29
30 const size_t kLeapfrogMapSize = 20000;
31 const size_t kLeapfrogPassCount = 75000;
32 const char* kLeapfrogBenchmarkName = "JunctionMapLeapfrog";
33
34 } // namespace
35
36 typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
37 typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
38 typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
39 typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
40
41 template <typename Map>
42 void run_crude_map(size_t map_size, size_t pass_count, const char* bench_name,
43     size_t gc_frequency) {
44     std::cout << "[ RUN      ] " << kTestName << "." << bench_name << "\n";
45     auto start_time = std::chrono::system_clock::now();
46
47     size_t nInsertedNum = 0;
48     size_t nFindSuccess = 0;
49     size_t nOperations = 0;
50     // Seems like the crude map won't resize, so better have a large enough
51     // capacity.
52     std::unique_ptr<Map> map(new Map(map_size * 32));
53     auto qsbrContext = junction::DefaultQSBR.createContext();
54     for (size_t count = 0; count < pass_count; count++) {
55         for (size_t i = 0; i < map_size; ++i) {
56             // The number to operate on the map.
57             size_t n = map_size + i;
58             // Insert
59             if (i % s_nInsertPercentage == 1) {
60                 map->assign(i, n);
61                 nInsertedNum++;
62 //                std::cout << "Inserted" << i << "\n";
63             }
64             // Find
65             {
66                 if (map->get(i)) {
67                     ++nFindSuccess;
68 //                    std::cout << "Found" << i << "\n";
69                 }
70             }
71             // Delete
72             if (i % s_nInsertPercentage == 1) {
73                 if (map->get(i)) {
74                     map->assign(n, 0);
75 //                    std::cout << "Erased" << i << "\n";
76                 }
77             }
78             if (++nOperations > gc_frequency) {
79               junction::DefaultQSBR.update(qsbrContext);
80               nOperations = 0;
81             }
82         }
83     }
84     junction::DefaultQSBR.update(qsbrContext);
85     auto finish_time = std::chrono::system_clock::now();
86     auto dur = finish_time - start_time;
87     auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
88
89     if (nFindSuccess != nInsertedNum) {
90         std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
91                   << nInsertedNum << "\n";
92         std::cout << "[       FAILED ] " << kTestName << "." << bench_name
93                   << "(" << milisecs.count() << " ms)\n";
94         assert(false && "ConcurrentMap ERROR");
95     } else {
96         std::cout << "[       OK ] " << kTestName << "." << bench_name
97                   << "(" << milisecs.count() << " ms)\n";
98     }
99 }
100
101 template <typename Map>
102 void run_test(size_t map_size, size_t pass_count, const char* bench_name,
103     size_t gc_frequency) {
104     std::cout << "[ RUN      ] " << kTestName << "." << bench_name << "\n";
105     auto start_time = std::chrono::system_clock::now();
106
107     size_t nInsertedNum = 0;
108     size_t nFindSuccess = 0;
109     size_t nOperations = 0;
110     std::unique_ptr<Map> map(new Map());
111     auto qsbrContext = junction::DefaultQSBR.createContext();
112     for (size_t count = 0; count < pass_count; count++) {
113         for (size_t i = 0; i < map_size; ++i) {
114             // The number to operate on the map.
115             size_t n = map_size + i;
116             // Insert
117             if (i % s_nInsertPercentage == 1) {
118                 auto iter = map->insertOrFind(i);
119                 if (!iter.getValue()) {
120                   iter.assignValue(n);
121                   nInsertedNum++;
122 //                  std::cout << "Inserted" << i << "\n";
123                 }
124             }
125             // Find
126             {
127                 auto iter = map->find(i);
128                 if (iter.getValue()) {
129                     ++nFindSuccess;
130 //                    std::cout << "Found" << i << "\n";
131                 }
132             }
133             // Delete
134             if (i % s_nInsertPercentage == 1) {
135                 auto iter = map->find(i);
136                 if (iter.getValue()) {
137                     iter.eraseValue();
138 //                    std::cout << "Erased" << i << "\n";
139                 }
140             }
141             if (++nOperations > gc_frequency) {
142                 junction::DefaultQSBR.update(qsbrContext);
143                 nOperations = 0;
144             }
145         }
146     }
147     auto finish_time = std::chrono::system_clock::now();
148     auto dur = finish_time - start_time;
149     auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
150
151     if (nFindSuccess != nInsertedNum) {
152         std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
153                   << nInsertedNum << "\n";
154         std::cout << "[       FAILED ] " << kTestName << "." << bench_name
155                   << "(" << milisecs.count() << " ms)\n";
156         assert(false && "ConcurrentMap ERROR");
157     } else {
158         std::cout << "[       OK ] " << kTestName << "." << bench_name
159                   << "(" << milisecs.count() << " ms)\n";
160     }
161 }
162
163 int main() {
164     run_test<LeapfrogMap>(kLeapfrogMapSize, kLeapfrogPassCount,
165         kLeapfrogBenchmarkName, kLeapfrogGCFrequency );
166     run_test<LinearMap>(kLinearMapSize, kLinearPassCount, kLinearBenchmarkName,
167         kGCFrequency);
168     run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGrampaBenchmarkName,
169         kGCFrequency);
170     run_crude_map<CrudeMap>(kCrudeMapSize, kCrudePassCount, kCrudeBenchmarkName,
171         kGCFrequency);
172     return 0;
173 }