|
| 1 | +#include <algorithm> |
| 2 | +#include <chrono> |
| 3 | +#include <fstream> |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "ART.h" |
| 8 | +#include "ArtNode.h" |
| 9 | +#include "ArtNodeNewMethods.cpp" |
| 10 | +#include "Chain.h" |
| 11 | +#include "Helper.h" |
| 12 | +#include "trees/QuART_tail.h" |
| 13 | + |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +template <typename key_type> |
| 17 | +std::vector<key_type> read_bin(const char* filename) { |
| 18 | + std::ifstream inputFile(filename, std::ios::binary); |
| 19 | + inputFile.seekg(0, std::ios::end); |
| 20 | + const std::streampos fileSize = inputFile.tellg(); |
| 21 | + inputFile.seekg(0, std::ios::beg); |
| 22 | + std::vector<key_type> data(fileSize / sizeof(key_type)); |
| 23 | + inputFile.read(reinterpret_cast<char*>(data.data()), fileSize); |
| 24 | + return data; |
| 25 | +} |
| 26 | + |
| 27 | +int main(int argc, char** argv) { |
| 28 | + bool verbose = false; // optional argument |
| 29 | + int N = 5000000; // optional argument |
| 30 | + string input_file; // required argument |
| 31 | + string tree_type = "ART"; // default tree type |
| 32 | + |
| 33 | + // Parse arguments; make sure to increment i by 2 if you consume an argument |
| 34 | + for (int i = 1; i < argc;) { |
| 35 | + if (string(argv[i]) == "-v") { |
| 36 | + verbose = true; |
| 37 | + i++; |
| 38 | + } else if (string(argv[i]) == "-N") { |
| 39 | + N = atoi(argv[i + 1]); |
| 40 | + i += 2; |
| 41 | + } else if (string(argv[i]) == "-f") { |
| 42 | + input_file = argv[i + 1]; |
| 43 | + i += 2; |
| 44 | + } else if (string(argv[i]) == "-t") { |
| 45 | + tree_type = argv[i + 1]; |
| 46 | + i += 2; |
| 47 | + } else { |
| 48 | + i++; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // read data |
| 53 | + auto keys = read_bin<uint32_t>(input_file.c_str()); |
| 54 | + |
| 55 | + // Build tree |
| 56 | + ART::ART* tree = nullptr; |
| 57 | + if (tree_type == "ART") { |
| 58 | + tree = new ART::ART(); |
| 59 | + } else if (tree_type == "QuART_tail") { |
| 60 | + tree = new ART::QuART_tail(); |
| 61 | + } else { |
| 62 | + cerr << "Unknown tree type: " << tree_type << endl; |
| 63 | + return 1; |
| 64 | + } |
| 65 | + |
| 66 | + if (verbose) { |
| 67 | + cout << "Tree type: " << tree_type << endl; |
| 68 | + } |
| 69 | + |
| 70 | + long long insertion_time = 0; |
| 71 | + for (uint64_t i = 0; i < N; i++) { |
| 72 | + uint8_t key[4]; |
| 73 | + ART::loadKey(keys[i], key); |
| 74 | + auto start = chrono::high_resolution_clock::now(); |
| 75 | + tree->insert(key, keys[i]); |
| 76 | + auto stop = chrono::high_resolution_clock::now(); |
| 77 | + auto duration = |
| 78 | + chrono::duration_cast<chrono::nanoseconds>(stop - start); |
| 79 | + insertion_time += duration.count(); |
| 80 | + } |
| 81 | + |
| 82 | + if (verbose) { |
| 83 | + cout << "Insertion time: " << insertion_time << " ns" << endl; |
| 84 | + } |
| 85 | + |
| 86 | + // Query tree |
| 87 | + long long query_time = 0; |
| 88 | + for (uint64_t i = 0; i < N; i++) { |
| 89 | + uint8_t key[4]; |
| 90 | + ART::loadKey(keys[i], key); |
| 91 | + auto start = chrono::high_resolution_clock::now(); |
| 92 | + ART::ArtNode* leaf = tree->lookup(key); |
| 93 | + auto stop = chrono::high_resolution_clock::now(); |
| 94 | + auto duration = |
| 95 | + chrono::duration_cast<chrono::nanoseconds>(stop - start); |
| 96 | + query_time += duration.count(); |
| 97 | + assert(ART::isLeaf(leaf) && ART::getLeafValue(leaf) == keys[i]); |
| 98 | + } |
| 99 | + |
| 100 | + if (verbose) { |
| 101 | + cout << "Query time: " << query_time << " ns" << endl; |
| 102 | + } |
| 103 | + |
| 104 | + // Output the times in csv format, including tree type |
| 105 | + cout << insertion_time << "," << query_time << endl; |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
0 commit comments