-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmem_testing.cpp
More file actions
121 lines (101 loc) · 3.35 KB
/
mem_testing.cpp
File metadata and controls
121 lines (101 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <random>
#include <chrono>
#include "s2st.hpp"
#include "superfix.hpp"
#include "fstack.hpp"
#include "parse_tree_ptr.hpp"
#include "uncompressed_parse_tree_ptr.hpp"
#include "parse_tree_levels.hpp"
#include "parse_tree_symbol_processing.hpp"
#include "context_insensitive_decomposition.hpp"
#include "index.hpp"
#include "recompression.hpp"
#include "memory_profiling.hpp"
using namespace recomp;
auto clock_now() {
return std::chrono::high_resolution_clock::now();
}
double time_between(const auto &a, const auto &b) {
return std::chrono::duration<double>(b - a).count();
}
template <class F>
double timeF(F f) {
auto start = clock_now();
f();
auto end = clock_now();
return time_between(start, end);
}
template <class F>
decltype(std::declval<F>()()) timeF(F f, double &time) {
auto start = clock_now();
auto ret = f();
auto end = clock_now();
time = time_between(start, end);
return ret;
}
std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()));
}
throw(errno);
}
std::vector<Fragment> generate_frag_queries(size_t len, size_t num_queries) {
std::random_device dev;
// std::mt19937 rng(dev());
std::mt19937 rng(0);
std::uniform_int_distribution<uint64_t> disti(0, 9999999999999ULL);
std::uniform_int_distribution<uint64_t> distl(4, 60);
std::vector<Fragment> frag_queries;
for (size_t i = 0; i < num_queries; i++) {
size_t index = disti(rng) % len;
size_t frag_len = std::min(distl(rng), len - index);
frag_queries.push_back(Fragment{index, frag_len});
}
return frag_queries;
}
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " <input_file>" << std::endl;
return 1;
}
PRINT_MEM();
auto text = get_file_contents(argv[1]);
std::cout << "Loaded text file" << std::endl;
PRINT_MEM("loaded text, before index");
double t1;
auto myindex = timeF([&]{return Index::from(text);}, t1);
std::cout << "Constructed text index in " << t1 << " seconds" << std::endl;
PRINT_MEM("alloc'd index");
std::vector<uint8_t> text_vec(text.size());
for (size_t i = 0; i < text_vec.size(); i++) {
text_vec[i] = (unsigned char)text[i];
}
auto frag_queries = generate_frag_queries(text.size(), 1000);
std::vector<std::vector<uint8_t>> queries(frag_queries.size());
std::transform(frag_queries.begin(), frag_queries.end(), queries.begin(), [&](Fragment f){
auto str = myindex.slp.extract(f.index, f.length);
std::vector<uint8_t> vec(str.size());
for (size_t i = 0; i < vec.size(); i++) {
vec[i] = (unsigned char)str[i];
}
return vec;
});
std::vector<std::vector<size_t>> query_results_1(frag_queries.size());
double t2 = timeF([&]{
for (size_t i = 0; i < frag_queries.size(); i++) {
auto frag = frag_queries[i];
myindex.report(frag, [&](int64_t j){
query_results_1[i].push_back(j);
});
}
});
std::cout << "Finished index reporting in " << t2 << " seconds" << std::endl;
return 0;
}