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