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 // Run GC after "kGCFrequency" operations.
15 const size_t kGCFrequency = 3000;
16 const size_t kLeapfrogGCFrequency = 1500;
18 const size_t kCrudeMapSize = 10000;
19 const size_t kCrudePassCount = 40000;
20 const char* kCrudeBenchmarkName = "JunctionMapCrude";
22 const size_t kGrampaMapSize = 20000;
23 const size_t kGrampaPassCount = 30000;
24 const char* kGrampaBenchmarkName = "JunctionMapGrampa";
26 const size_t kLinearMapSize = 20000;
27 const size_t kLinearPassCount = 70000;
28 const char* kLinearBenchmarkName = "JunctionMapLinear";
30 const size_t kLeapfrogMapSize = 20000;
31 const size_t kLeapfrogPassCount = 75000;
32 const char* kLeapfrogBenchmarkName = "JunctionMapLeapfrog";
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;
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();
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
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;
59 if (i % s_nInsertPercentage == 1) {
62 // std::cout << "Inserted" << i << "\n";
68 // std::cout << "Found" << i << "\n";
72 if (i % s_nInsertPercentage == 1) {
75 // std::cout << "Erased" << i << "\n";
78 if (++nOperations > gc_frequency) {
79 junction::DefaultQSBR.update(qsbrContext);
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);
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");
96 std::cout << "[ OK ] " << kTestName << "." << bench_name
97 << "(" << milisecs.count() << " ms)\n";
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();
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;
117 if (i % s_nInsertPercentage == 1) {
118 auto iter = map->insertOrFind(i);
119 if (!iter.getValue()) {
122 // std::cout << "Inserted" << i << "\n";
127 auto iter = map->find(i);
128 if (iter.getValue()) {
130 // std::cout << "Found" << i << "\n";
134 if (i % s_nInsertPercentage == 1) {
135 auto iter = map->find(i);
136 if (iter.getValue()) {
138 // std::cout << "Erased" << i << "\n";
141 if (++nOperations > gc_frequency) {
142 junction::DefaultQSBR.update(qsbrContext);
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);
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");
158 std::cout << "[ OK ] " << kTestName << "." << bench_name
159 << "(" << milisecs.count() << " ms)\n";
164 run_test<LeapfrogMap>(kLeapfrogMapSize, kLeapfrogPassCount,
165 kLeapfrogBenchmarkName, kLeapfrogGCFrequency );
166 run_test<LinearMap>(kLinearMapSize, kLinearPassCount, kLinearBenchmarkName,
168 run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGrampaBenchmarkName,
170 run_crude_map<CrudeMap>(kCrudeMapSize, kCrudePassCount, kCrudeBenchmarkName,