Repository navigation
Expand file tree
/
Copy pathqueries.cpp
More file actions
209 lines (173 loc) · 7.2 KB
/
Copy pathqueries.cpp
File metadata and controls
209 lines (173 loc) · 7.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <succinct/mapper.hpp>
#include "index_types.hpp"
#include "wand_data_compressed.hpp"
#include "wand_data_raw.hpp"
#include "queries.hpp"
#include "util.hpp"
namespace {
void printUsage(const std::string &programName) {
std::cerr << "Usage: " << programName
<< " index_type query_algorithm index_filename [--wand wand_data_filename]"
<< " [--compressed-wand] [--query query_filename]" << std::endl;
}
} // namespace
using namespace ds2i;
template<typename Functor>
void op_perftest(Functor query_func, // XXX!!!
std::vector<ds2i::term_id_vec> const &queries,
std::string const &index_type,
std::string const &query_type,
size_t runs) {
using namespace ds2i;
std::vector<double> query_times;
for (size_t run = 0; run <= runs; ++run) {
for (auto const &query: queries) {
auto tick = get_time_usecs();
uint64_t result = query_func(query);
do_not_optimize_away(result);
double elapsed = double(get_time_usecs() - tick);
if (run != 0) { // first run is not timed
query_times.push_back(elapsed);
}
}
}
if (false) {
for (auto t: query_times) {
std::cout << (t / 1000) << std::endl;
}
} else {
std::sort(query_times.begin(), query_times.end());
double avg = std::accumulate(query_times.begin(), query_times.end(), double()) / query_times.size();
double q50 = query_times[query_times.size() / 2];
double q90 = query_times[90 * query_times.size() / 100];
double q95 = query_times[95 * query_times.size() / 100];
logger() << "---- " << index_type << " " << query_type << std::endl;
logger() << "Mean: " << avg << std::endl;
logger() << "50% quantile: " << q50 << std::endl;
logger() << "90% quantile: " << q90 << std::endl;
logger() << "95% quantile: " << q95 << std::endl;
stats_line()
("type", index_type)
("query", query_type)
("avg", avg)
("q50", q50)
("q90", q90)
("q95", q95);
}
}
template<typename IndexType, typename WandType>
void perftest(const char *index_filename,
const char *wand_data_filename,
std::vector<ds2i::term_id_vec> const &queries,
std::string const &type,
std::string const &query_type) {
using namespace ds2i;
IndexType index;
logger() << "Loading index from " << index_filename << std::endl;
boost::iostreams::mapped_file_source m(index_filename);
succinct::mapper::map(index, m);
logger() << "Warming up posting lists" << std::endl;
std::unordered_set<term_id_type> warmed_up;
for (auto const &q: queries) {
for (auto t: q) {
if (!warmed_up.count(t)) {
index.warmup(t);
warmed_up.insert(t);
}
}
}
WandType wdata;
std::vector<std::string> query_types;
boost::algorithm::split(query_types, query_type, boost::is_any_of(":"));
boost::iostreams::mapped_file_source md;
if (wand_data_filename) {
md.open(wand_data_filename);
succinct::mapper::map(wdata, md, succinct::mapper::map_flags::warmup);
}
uint64_t k = configuration::get().k;
logger() << "Performing " << type << " queries" << std::endl;
for (auto const &t: query_types) {
logger() << "Query type: " << t << std::endl;
std::function<uint64_t(ds2i::term_id_vec)> query_fun;
if (t == "and") {
query_fun = [&](ds2i::term_id_vec query) { return and_query<false>()(index, query); };
} else if (t == "and_freq") {
query_fun = [&](ds2i::term_id_vec query) { return and_query<true>()(index, query); };
} else if (t == "or") {
query_fun = [&](ds2i::term_id_vec query) { return or_query<false>()(index, query); };
} else if (t == "or_freq") {
query_fun = [&](ds2i::term_id_vec query) { return or_query<true>()(index, query); };
} else if (t == "wand" && wand_data_filename) {
query_fun = [&](ds2i::term_id_vec query) { return wand_query<WandType>(wdata, k)(index, query); };
} else if (t == "block_max_wand" && wand_data_filename) {
query_fun = [&](ds2i::term_id_vec query) { return block_max_wand_query<WandType>(wdata, k)(index, query); };
} else if (t == "ranked_or" && wand_data_filename) {
query_fun = [&](ds2i::term_id_vec query) { return ranked_or_query<WandType>(wdata, k)(index, query); };
} else if (t == "maxscore" && wand_data_filename) {
query_fun = [&](ds2i::term_id_vec query) { return maxscore_query<WandType>(wdata, k)(index, query); };
} else {
logger() << "Unsupported query type: " << t << std::endl;
break;
}
op_perftest(query_fun, queries, type, t, 2);
}
}
typedef wand_data<bm25, wand_data_raw<bm25>> wand_raw_index;
typedef wand_data<bm25, wand_data_compressed<bm25, uniform_score_compressor>> wand_uniform_index;
int main(int argc, const char **argv) {
using namespace ds2i;
std::string programName = argv[0];
if (argc < 3) {
printUsage(programName);
return 1;
}
std::string type = argv[1];
std::string query_type = argv[2];
const char *index_filename = argv[3];
const char *wand_data_filename = nullptr;
const char *query_filename = nullptr;
bool compressed = false;
std::vector<term_id_vec> queries;
for (int i = 4; i < argc; ++i) {
std::string arg = argv[i];
if(arg == "--wand"){
wand_data_filename = argv[++i];;
}
if(arg == "--compressed-wand"){
compressed = true;
}
if (arg == "--query") {
query_filename = argv[++i];
}
}
term_id_vec q;
if(query_filename){
std::filebuf fb;
if (fb.open(query_filename, std::ios::in)) {
std::istream is(&fb);
while (read_query(q, is)) queries.push_back(q);
}
} else {
while (read_query(q)) queries.push_back(q);
}
/**/
if (false) {
#define LOOP_BODY(R, DATA, T) \
} else if (type == BOOST_PP_STRINGIZE(T)) { \
if (compressed) { \
perftest<BOOST_PP_CAT(T, _index), wand_uniform_index> \
(index_filename, wand_data_filename, queries, type, query_type); \
} else { \
perftest<BOOST_PP_CAT(T, _index), wand_raw_index> \
(index_filename, wand_data_filename, queries, type, query_type); \
} \
/**/
BOOST_PP_SEQ_FOR_EACH(LOOP_BODY, _, DS2I_INDEX_TYPES);
#undef LOOP_BODY
} else {
logger() << "ERROR: Unknown type " << type << std::endl;
}
}