0b2fcf9a3a2c812b4e6daf7abfe488ea16ed8e20
[junction.git] / test / junction_driver.cpp
1 #include <junction/ConcurrentMap_Grampa.h>
2 #include <iostream>
3 #include <memory>
4 #include <chrono>
5 #include <cassert>
6
7 namespace {
8
9 const unsigned s_nInsertPercentage = 10;
10 const char* kTestName = "InsDelFind";
11 const size_t kGrampaMapSize = 20000;
12 const size_t kGrampaPassCount = 30000;
13 const char* kGrampaBenchmarkName = "JunctionMapLinear";
14
15 } // namespace
16
17 typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
18
19 template <typename Map>
20 void run_test(size_t map_size, size_t pass_count, const char* bench_name) {
21     auto start_time = std::chrono::system_clock::now();
22
23     size_t nInsertedNum = 0;
24     size_t nFindSuccess = 0;
25     std::unique_ptr<Map> map(new Map());
26     auto qsbrContext = junction::DefaultQSBR.createContext();
27     for (size_t count = 0; count < pass_count; count++) {
28         for (size_t i = 0; i < map_size; ++i) {
29             // The number to operate on the map.
30             size_t n = map_size + i;
31             // Insert
32             if (i % s_nInsertPercentage == 1) {
33                 auto iter = map->insertOrFind(n);
34                 if (!iter.getValue()) {
35                   iter.assignValue(n);
36                   nInsertedNum++;
37 //                  std::cout << "Inserted" << n << "\n";
38                 }
39             }
40             // Find
41             {
42                 auto iter = map->find(n);
43                 if (iter.getValue()) {
44                     ++nFindSuccess;
45 //                    std::cout << "Found" << n << "\n";
46                 }
47             }
48             // Delete
49             if (i % s_nInsertPercentage == 1) {
50                 auto iter = map->find(n);
51                 if (iter.getValue()) {
52                     iter.eraseValue();
53 //                    std::cout << "Erased" << n << "\n";
54                 }
55             }
56             junction::DefaultQSBR.update(qsbrContext);
57         }
58     }
59     auto finish_time = std::chrono::system_clock::now();
60     auto dur = finish_time - start_time;
61     auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
62
63     if (nFindSuccess != nInsertedNum) {
64         std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
65                   << nInsertedNum << "\n";
66         std::cout << "[       FAILED ] " << kTestName << "." << bench_name
67                   << "(" << milisecs.count() << " ms)\n";
68         assert(false && "ConcurrentMap ERROR");
69     } else {
70         std::cout << "[       OK ] " << kTestName << "." << bench_name
71                   << "(" << milisecs.count() << " ms)\n";
72     }
73 }
74
75 int main() {
76     run_test<GrampaMap>(kGrampaMapSize, kGrampaPassCount, kGrampaBenchmarkName);
77     return 0;
78 }