-
Notifications
You must be signed in to change notification settings - Fork 16
/
spatial_index_benchmark.hpp
296 lines (258 loc) · 8.59 KB
/
spatial_index_benchmark.hpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// Copyright (C) 2013 Mateusz Loskot <mateusz@loskot.net>
// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy
// at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MLOSKOT_SPATIAL_INDEX_BENCHMARK_HPP_INCLUDED
#define MLOSKOT_SPATIAL_INDEX_BENCHMARK_HPP_INCLUDED
#ifdef _MSC_VER
#if (_MSC_VER == 1700)
#define _VARIADIC_MAX 6
#endif
#define NOMINMAX
#endif // _MSC_VER
#include "high_resolution_timer.hpp"
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace sibench {
//
// Default benchmark settings
//
std::size_t const max_iterations = 1000000;
std::size_t const max_capacities = 100;
std::size_t const capacity_step = 2;
std::size_t const max_insertions = max_iterations;
std::size_t const max_random_insertions = 20000;
double const insert_step = 0.10;
std::size_t const random_runs = 200;
std::size_t const max_queries = std::size_t(max_insertions * 0.1);
std::size_t const constant_max_capacity = 4;
std::size_t const constant_min_capacity = 2;
std::size_t const query_size = 100;
typedef uint32_t id_type;
#ifndef NDEBUG
#define SIBENCH_DEBUG_PRINT_INFO 1
#endif
enum class rtree_variant { rstar, linear, quadratic, rtree, quadtree };
inline std::pair<rtree_variant, std::string> get_rtree_split_variant() {
#ifdef SIBENCH_RTREE_SPLIT_LINEAR
return std::make_pair(rtree_variant::linear, "L");
#elif SIBENCH_RTREE_SPLIT_QUADRATIC
return std::make_pair(rtree_variant::quadratic, "Q");
#elif SIBENCH_RTREE_SPLIT_QUADRATIC_SPHERE
return std::make_pair(rtree_variant::quadratic, "QS");
#elif SIBENCH_RTREE_SPLIT_RSTAR
return std::make_pair(rtree_variant::rstar, "R");
#elif SIBENCH_RTREE_SPLIT_QUADTREE
return std::make_pair(rtree_variant::quadtree, "QT");
#else
#error Unknown rtree split algorithm
#endif
}
inline std::string get_rtree_load_variant() {
#ifdef SIBENCH_RTREE_LOAD_ITR
return "ITR";
#elif SIBENCH_RTREE_LOAD_BLK
return "BLK";
#elif SIBENCH_RTREE_LOAD_CUSTOM
return "CST";
#else
#error Unknown rtree loading method
#endif
}
inline std::string get_banner(std::string const &lib) {
return lib + "(" + get_rtree_split_variant().second + ',' +
get_rtree_load_variant() + ")";
}
//
// Generators of random objects
//
typedef float coord_t;
typedef std::vector<coord_t> coords_t;
typedef std::tuple<coord_t, coord_t> point2d_t;
typedef std::vector<point2d_t> points2d_t;
typedef struct box2d {
coord_t min[2];
coord_t max[2];
} box2d_t;
typedef std::vector<box2d_t> boxes2d_t;
template <typename T> struct random_generator {
typedef typename std::uniform_real_distribution<T>::result_type result_type;
T const max;
std::mt19937 gen;
std::uniform_real_distribution<T> dis;
random_generator(std::size_t n)
: max(static_cast<T>(n / 2)),
gen(1) // generate the same succession of results for everyone
// (unsigned
// int)std::chrono::system_clock::now().time_since_epoch().count())
,
dis(-max, max) {}
result_type operator()() { return dis(gen); }
private:
random_generator(random_generator const &) /*= delete*/;
random_generator &operator=(random_generator const &) /*= delete*/;
};
inline coords_t generate_coordinates(std::size_t n) {
random_generator<float> rg(n);
coords_t coords;
coords.reserve(n);
for (decltype(n) i = 0; i < n; ++i) {
coords.emplace_back(rg());
}
return coords;
}
inline points2d_t generate_points(std::size_t n) {
auto coords = generate_coordinates(n * 2);
points2d_t points;
points.reserve(n);
auto s = coords.size();
for (decltype(s) i = 0; i < s; i += 2) {
points.emplace_back(coords[i], coords[i + 1]);
}
return points;
}
inline boxes2d_t generate_boxes(std::size_t n) {
random_generator<float> rg(n);
boxes2d_t boxes;
boxes.reserve(n);
for (decltype(n) i = 0; i < n; ++i) {
auto const x = rg();
auto const y = rg();
boxes.emplace_back();
auto &box = boxes.back();
float const size = 0.5f;
box.min[0] = x - size;
box.min[1] = y - size;
box.max[0] = x + size;
box.max[1] = y + size;
}
return boxes;
}
//
// Benchmark running routines
//
struct result_info {
std::string step;
double min;
double max;
double sum;
double count;
std::size_t min_capacity;
std::size_t max_capacity;
std::size_t iterations;
result_info(char const *step = "")
: step(step), min(-1), max(-1), sum(0), count(0), min_capacity(0),
max_capacity(0), iterations(0) {}
void accumulate(result_info const &r) {
min = min < 0 ? r.min : (std::min)(min, r.min);
max = max < 0 ? r.max : (std::max)(max, r.max);
sum += r.sum;
count += r.count;
assert(min <= max);
}
template <typename Timer> void set_mark(Timer const &t) {
auto const m = t.elapsed();
sum += m;
count++;
min = min < 0 ? m : (std::min)(m, min);
max = max < 0 ? m : (std::max)(m, max);
assert(min <= max);
}
double avg() const { return sum / count; }
};
inline std::ostream &operator<<(std::ostream &os, result_info const &r) {
os << r.step << " " << r.iterations << " in " << r.min << " to " << r.max
<< " sec" << std::endl;
return os;
}
inline std::ostream &print_result(std::ostream &os, std::string const &lib,
result_info const &r) {
os << std::setw(15) << std::setfill(' ') << std::left
<< sibench::get_banner(lib) << ": " << r;
return os;
}
inline std::ostream &print_result(std::ostream &os, std::string const & /*lib*/,
result_info const &load,
result_info const &query) {
assert(load.min_capacity == query.min_capacity);
assert(load.max_capacity == query.max_capacity);
std::streamsize wn(5), wf(10);
os << std::left << std::setfill(' ') << std::fixed << std::setprecision(6)
<< std::setw(wn) << load.max_capacity << std::setw(wn) << load.min_capacity
<< std::setw(wf) << load.min << std::setw(wf) << query.min << std::endl;
return os;
}
inline std::ostream &print_insert_result(std::ostream &os,
std::string const & /*lib*/,
result_info const &res) {
std::streamsize wn(5), wf(10);
os << std::left << std::setfill(' ') << std::fixed << std::setprecision(6)
<< std::setw(wn) << res.max_capacity << std::setw(wn) << res.min_capacity
<< std::setw(wn * 2 + 1) << res.iterations << std::setw(wf)
<< std::setprecision(3) << res.avg() * 1000.0 << std::setw(wf)
<< std::setprecision(5) << res.sum << std::endl;
return os;
}
inline std::ostream &print_result_header(std::ostream &os,
std::string const &lib) {
std::streamsize const wn(5), wf(10), vn(2);
os << sibench::get_banner(lib) << ' ' << std::setw(wn * vn + wf * vn)
<< std::setfill('-') << ' ' << std::endl;
os << std::left << std::setfill(' ') << std::setw(wn * vn) << "capacity"
<< std::setw(wf) << "load" << std::setw(wf) << "query" << std::endl;
return os;
}
inline std::ostream &print_insert_result_header(std::ostream &os,
std::string const &lib) {
std::streamsize const wn(5), wf(10), vn(2);
os << sibench::get_banner(lib) << ' ' << std::setw(wn * vn + wf * vn)
<< std::setfill('-') << ' ' << std::endl;
os << std::left << std::setfill(' ') << std::setw(wn * vn) << "capacity"
<< std::setw(wn * vn + 1) << "insertions" << std::setw(wf) << "avg"
<< std::setw(wf) << "total" << std::endl;
return os;
}
inline std::ostream &print_query_count(std::ostream &os, std::string const &lib,
size_t i) {
os << sibench::get_banner(lib) << " stats: found=" << i << std::endl;
return os;
}
inline std::ostream &print_insert_count(std::ostream &os,
std::string const &lib, size_t i,
size_t t) {
os << sibench::get_banner(lib) << " stats: insertions=" << i << " total=" << t
<< std::endl;
return os;
}
template <typename Container, typename Operation>
inline result_info benchmark(char const *step, std::size_t iterations,
Container const &objects, Operation op) {
result_info r(step);
r.iterations = iterations;
{
util::high_resolution_timer t;
op(objects, iterations);
r.set_mark(t);
}
return r;
}
} // namespace sibench
#endif