1 #include <junction/ConcurrentMap_Grampa.h>
2 #include <junction/ConcurrentMap_Linear.h>
3 #include <junction/ConcurrentMap_Leapfrog.h>
4 #include <junction/ConcurrentMap_Crude.h>
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";
18 const size_t kLinearMapSize = 25000;
19 const size_t kLinearPassCount = 30000;
20 const char* kLinearBenchmarkName = "JunctionMapLinear";
22 const size_t kLeapfrogMapSize = 25000;
23 const size_t kLeapfrogPassCount = 30000;
24 const char* kLeapfrogBenchmarkName = "JunctionMapLeapfrog";
26 const size_t kCrudeMapSize = 10000;
27 const size_t kCrudePassCount = 120000;
28 const char* kCrudeBenchmarkName = "JunctionMapCrude";
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;
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();
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
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;
53 if (i % s_nInsertPercentage == 1) {
56 // std::cout << "Inserted" << i << "\n";
62 // std::cout << "Found" << i << "\n";
66 if (i % s_nInsertPercentage == 1) {
69 // std::cout << "Erased" << i << "\n";
72 junction::DefaultQSBR.update(qsbrContext);
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);
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");
86 std::cout << "[ OK ] " << kTestName << "." << bench_name
87 << "(" << milisecs.count() << " ms)\n";
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();
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;
105 if (i % s_nInsertPercentage == 1) {
106 auto iter = map->insertOrFind(i);
107 if (!iter.getValue()) {
110 // std::cout << "Inserted" << i << "\n";
115 auto iter = map->find(i);
116 if (iter.getValue()) {
118 // std::cout << "Found" << i << "\n";
122 if (i % s_nInsertPercentage == 1) {
123 auto iter = map->find(i);
124 if (iter.getValue()) {
126 // std::cout << "Erased" << i << "\n";
129 junction::DefaultQSBR.update(qsbrContext);
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);
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");
143 std::cout << "[ OK ] " << kTestName << "." << bench_name
144 << "(" << milisecs.count() << " ms)\n";
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);