-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeed_test.cpp
299 lines (269 loc) · 9.59 KB
/
speed_test.cpp
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
297
298
299
/*
* Copyright (c) 2023 Amos Brocco.
* Adapted from cpp-anchorhash Copyright (c) 2020 anchorhash (MIT License)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "anchor/anchorengine.h"
#include "memento/mashtable.h"
#include "memento/mementoengine.h"
#include "jump/jumpengine.h"
#include "power/powerengine.h"
#ifdef USE_PCG32
#include "pcg_random.hpp"
#include <random>
#endif
#include <boost/unordered/unordered_flat_map.hpp>
#include <boost/unordered_map.hpp>
#include <cxxopts.hpp>
#include <fmt/core.h>
#include <fstream>
#include <unordered_map>
#include <gtl/phmap.hpp>
/*
* ******************************************
* Heap allocation measurement
* ******************************************
*/
#ifdef USE_HEAPSTATS
static unsigned long allocations{0};
static unsigned long deallocations{0};
static unsigned long allocated{0};
static unsigned long deallocated{0};
static unsigned long maximum{0};
void *operator new(size_t size) {
void *p = malloc(size);
allocations += 1;
allocated += size;
maximum = allocated > maximum ? allocated : maximum;
return p;
}
void *operator new[](size_t size) {
void *p = malloc(size);
allocations += 1;
allocated += size;
maximum = allocated > maximum ? allocated : maximum;
return p;
}
void operator delete(void *ptr, std::size_t size) noexcept {
deallocations += 1;
deallocated += size;
free(ptr);
}
void operator delete[](void *ptr, std::size_t size) noexcept {
deallocations += 1;
deallocated += size;
free(ptr);
}
void reset_memory_stats() noexcept {
allocations = 0;
allocated = 0;
deallocations = 0;
deallocated = 0;
maximum = 0;
}
void print_memory_stats(std::string_view label) noexcept {
auto alloc{allocations};
auto dealloc{deallocations};
auto asize{allocated};
auto dsize{deallocated};
auto max{maximum};
fmt::println(" @{}: Allocations: {}, Allocated: {}, Deallocations: {}, "
"Deallocated: {}, Maximum: {}",
label, alloc, asize, dealloc, dsize, max);
allocations = alloc;
deallocations = dealloc;
allocated = asize;
deallocated = dsize;
maximum = max;
}
#endif
/*
* ******************************************
* Benchmark routine
* ******************************************
*/
template <typename Algorithm>
int bench(const std::string_view name, const std::string &filename,
uint32_t anchor_set, uint32_t working_set, uint32_t num_removals,
uint32_t num_keys) {
#ifdef USE_PCG32
pcg_extras::seed_seq_from<std::random_device> seed;
pcg32 rng{seed};
#else
srand(time(NULL));
#endif
std::ofstream results_file;
results_file.open(filename, std::ofstream::out | std::ofstream::app);
double norm_keys_rate = (double)num_keys / 1000000.0;
uint32_t *bucket_status = new uint32_t[anchor_set]();
for (uint32_t i = 0; i < working_set; i++) {
bucket_status[i] = 1;
}
#ifdef USE_HEAPSTATS
reset_memory_stats();
print_memory_stats("StartBenchmark");
#endif
Algorithm engine(anchor_set, working_set);
#ifdef USE_HEAPSTATS
print_memory_stats("AfterAlgorithmInit");
#endif
uint32_t i = 0;
while (i < num_removals) {
#ifdef USE_PCG32
uint32_t removed = rng() % working_set;
#else
uint32_t removed = rand() % working_set;
#endif
if (bucket_status[removed] == 1) {
engine.removeBucket(removed);
bucket_status[removed] = 0;
i++;
}
}
#ifdef USE_HEAPSTATS
print_memory_stats("AfterRemovals");
#endif
volatile int64_t bucket{0};
auto start{clock()};
for (uint32_t i = 0; i < num_keys; ++i) {
#ifdef USE_PCG32
bucket = engine.getBucketCRC32c(rng(), rng());
#else
bucket = engine.getBucketCRC32c(rand(), rand());
#endif
}
auto end{clock()};
#ifdef USE_HEAPSTATS
print_memory_stats("EndBenchmark");
#endif
auto elapsed{static_cast<double>(end - start) / CLOCKS_PER_SEC};
#ifdef USE_HEAPSTATS
auto maxheap{maximum};
fmt::println("{} Elapsed time is {} seconds, maximum heap allocated memory is {} bytes, sizeof({}) is {}", name, elapsed, maxheap, name, sizeof(Algorithm));
results_file << name << ":\tAnchor\t" << anchor_set << "\tWorking\t"
<< working_set << "\tRemovals\t" << num_removals << "\tRate\t"
<< norm_keys_rate / elapsed << "\tMaxHeap\t" << maxheap << "\tAlgoSizeof\t" << sizeof(Algorithm)<< "\n";
#else
fmt::println("{} Elapsed time is {} seconds", name, elapsed);
results_file << name << ":\tAnchor\t" << anchor_set << "\tWorking\t"
<< working_set << "\tRemovals\t" << num_removals << "\tRate\t"
<< norm_keys_rate / elapsed << "\n";
#endif
results_file.close();
delete[] bucket_status;
return 0;
}
int main(int argc, char *argv[]) {
cxxopts::Options options("speed_test", "MementoHash vs AnchorHash benchmark");
options.add_options()(
"Algorithm",
"Algorithm (null|baseline|anchor|memento|mementoboost|mementomash|mementostd|mementogtl|jump|power)",
cxxopts::value<std::string>())(
"AnchorSet", "Size of the AnchorSet (ignored by Memento)",
cxxopts::value<int>())("WorkingSet", "Size of the WorkingSet",
cxxopts::value<int>())(
"NumRemovals", "Number of random removals", cxxopts::value<int>())(
"NumKeys", "Number of keys to lookup for",
cxxopts::value<int>())("ResFileName", "Number of keys to lookup for",
cxxopts::value<std::string>());
options.positional_help(
"Algorithm AnchorSet WorkingSet NumRemovals Numkeys ResFilename");
options.parse_positional({"Algorithm", "AnchorSet", "WorkingSet",
"NumRemovals", "NumKeys", "ResFileName"});
auto result = options.parse(argc, argv);
if (argc != 7) {
fmt::println("{}", options.help());
exit(1);
}
auto algorithm = result["Algorithm"].as<std::string>();
auto anchor_set = static_cast<uint32_t>(result["AnchorSet"].as<int>());
auto working_set = static_cast<uint32_t>(result["WorkingSet"].as<int>());
auto num_removals = static_cast<uint32_t>(result["NumRemovals"].as<int>());
auto num_keys = static_cast<uint32_t>(result["NumKeys"].as<int>());
auto filename = result["ResFileName"].as<std::string>();
#ifdef USE_PCG32
fmt::println("Algorithm: {}, AnchorSet: {}, WorkingSet: {}, NumRemovals: {}, "
"NumKeys: {}, ResFileName: {}, Random: PCG32",
algorithm, anchor_set, working_set, num_removals, num_keys,
filename);
#else
fmt::println("Algorithm: {}, AnchorSet: {}, WorkingSet: {}, NumRemovals: {}, "
"NumKeys: {}, ResFileName: {}, Random: rand()",
algorithm, anchor_set, working_set, num_removals, num_keys,
filename);
#endif
if (algorithm == "null") {
// do nothing
} else if (algorithm == "baseline") {
#ifdef USE_PCG32
pcg_extras::seed_seq_from<std::random_device> seed;
pcg32 rng(seed);
#else
srand(time(NULL));
#endif
fmt::println("Allocating {} buckets of size {} bytes...", anchor_set,
sizeof(uint32_t));
uint32_t *bucket_status = new uint32_t[anchor_set]();
for (uint32_t i = 0; i < working_set; i++) {
bucket_status[i] = 1;
}
uint32_t i = 0;
while (i < num_removals) {
#ifdef USE_PCG32
uint32_t removed = rng() % working_set;
#else
uint32_t removed = rand() % working_set;
#endif
if (bucket_status[removed] == 1) {
bucket_status[removed] = 0;
i++;
}
}
delete[] bucket_status;
} else if (algorithm == "anchor") {
return bench<AnchorEngine>("Anchor", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "memento") {
return bench<MementoEngine<boost::unordered_flat_map>>(
"Memento<boost::unordered_flat_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementoboost") {
return bench<MementoEngine<boost::unordered_map>>(
"Memento<boost::unordered_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementostd") {
return bench<MementoEngine<std::unordered_map>>(
"Memento<std::unordered_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementogtl") {
return bench<MementoEngine<gtl::flat_hash_map>>(
"Memento<std::gtl::flat_hash_map>", filename, anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "mementomash") {
return bench<MementoEngine<MashTable>>("Memento<MashTable>", filename,
anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "jump") {
return bench<JumpEngine>("JumpEngine", filename,
anchor_set, working_set,
num_removals, num_keys);
} else if (algorithm == "power") {
return bench<PowerEngine>("PowerEngine", filename,
anchor_set, working_set,
num_removals, num_keys);
} else {
fmt::println("Unknown algorithm {}", algorithm);
return 2;
}
}