-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner.hpp
More file actions
455 lines (400 loc) · 12.3 KB
/
test-runner.hpp
File metadata and controls
455 lines (400 loc) · 12.3 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* @file test-runner.hpp
* @author agge3
* @data 2025-16-10
*
* Test runner, configuration, and configuration read-in (includes random
* distribution generators).
*/
#pragma once
#include "cache-manager.hpp"
#include "macros.hpp"
#include <nlohmann/json.hpp>
#include <algorithm>
#include <any>
#include <format>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <random>
#include <thread>
#include <vector>
enum class DistrType { INVALID, STRING, INT, DOUBLE };
enum class DistrGen { INVALID, NORMAL, UNIFORM };
struct DistrCfg {
DistrType type;
DistrGen gen;
size_t count;
size_t min;
size_t max;
};
template <typename T> class Distribution {
public:
Distribution(const DistrCfg &cfg)
: _cfg(cfg), _gen(std::random_device{}()) {
switch (_cfg.gen) {
case DistrGen::NORMAL:
_genFn = [this]() { return normal(); };
break;
case DistrGen::UNIFORM:
_genFn = [this]() { return uniform(); };
break;
default:
_genFn = [this]() { return normal(); };
}
if constexpr (std::is_same_v<T, int>) {
_type = "int";
_emitFn = [this](double val) {
_data.push_back(static_cast<int>(val));
};
} else if constexpr (std::is_same_v<T, double>) {
_type = "double";
_emitFn = [this](double val) { _data.push_back(val); };
} else if constexpr (std::is_same_v<T, std::string>) {
_type = "string";
_emitFn = [this](double val) {
// xxx accumulate multi-length strings
_data.push_back(
std::string(1, 'a' + static_cast<int>(val) % 26));
};
} else {
_type = "invalid";
_emitFn = [](double) {}; // noop for invalid type
}
}
void generate() {
_data.clear();
for (auto i = 0; i < _cfg.count; ++i) {
_emitFn(_genFn());
}
}
const std::vector<T> &data() const { return _data; }
std::string type() const { return _type; }
private:
double uniform() {
std::uniform_real_distribution<double> dist(_cfg.min, _cfg.max);
return dist(_gen);
}
double normal() {
std::normal_distribution<double> dist((_cfg.min + _cfg.max) / 2.0,
(_cfg.max - _cfg.min) / 6.0);
return std::clamp(dist(_gen), static_cast<double>(_cfg.min),
static_cast<double>(_cfg.max));
}
std::string _type;
DistrCfg _cfg;
std::mt19937 _gen;
std::vector<T> _data;
std::function<double()> _genFn;
std::function<void(double)> _emitFn;
};
// incomplete struct that is retrieved, with template specialization, from
// behind TestCfgHandle.
// @see makeTestCfg factory to make complete handle.
struct TestCfg {
using TestFns = std::vector<std::string>;
size_t threads;
size_t iter;
size_t capacity;
std::string name;
DistrCfg distr_cfg;
std::any distr_data;
TestFns fns;
TestCfg(size_t threads, size_t iter, size_t capacity,
const std::string &name, const DistrCfg &distr_cfg,
const TestFns &fns)
: threads(threads), iter(iter), capacity(capacity), name(name),
distr_cfg(distr_cfg), fns(fns) {}
};
// opaque pointer for storing different TestCfg template specializations
using TestCfgHandle = std::unique_ptr<TestCfg>;
using TestCfgs = std::vector<TestCfgHandle>;
TestCfgHandle makeTestCfg(size_t threads, size_t iter, size_t capacity,
const std::string &name, const DistrCfg &distr_cfg,
std::vector<std::string> &fns) {
switch (distr_cfg.type) {
case DistrType::INT: {
auto test = std::make_unique<TestCfg>(threads, iter, capacity, name,
distr_cfg, fns);
Distribution<int> dist(distr_cfg);
dist.generate();
test->distr_data = dist.data();
return test;
}
case DistrType::DOUBLE: {
auto test = std::make_unique<TestCfg>(threads, iter, capacity, name,
distr_cfg, fns);
Distribution<double> dist(distr_cfg);
dist.generate();
test->distr_data = dist.data();
return test;
}
case DistrType::STRING: {
auto test = std::make_unique<TestCfg>(threads, iter, capacity, name,
distr_cfg, fns);
Distribution<std::string> dist(distr_cfg);
dist.generate();
test->distr_data = dist.data();
return test;
}
default:
throw std::runtime_error("invalid distribution type");
}
}
DistrCfg parseDistrCfg(const nlohmann::json &j) {
DistrCfg cfg;
std::string type = j["distr"]["type"];
if (type == "string") {
cfg.type = DistrType::STRING;
} else if (type == "int") {
cfg.type = DistrType::INT;
} else if (type == "double") {
cfg.type = DistrType::DOUBLE;
} else {
cfg.type = DistrType::INVALID;
}
std::string gen = j["distr"]["gen"];
if (gen == "normal") {
cfg.gen = DistrGen::NORMAL;
} else if (gen == "uniform") {
cfg.gen = DistrGen::UNIFORM;
} else {
cfg.gen = DistrGen::INVALID;
}
cfg.count = static_cast<size_t>(j["distr"]["count"]);
cfg.min = static_cast<size_t>(j["distr"]["min"]);
cfg.max = static_cast<size_t>(j["distr"]["max"]);
return cfg;
}
/**
* Reads in JSON configuration from path and returns populated TestCfgs.
*/
TestCfgs readConfig(const std::string &path) {
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << "failed to open file: " << path << "\n";
return TestCfgs{};
}
nlohmann::json j;
try {
file >> j;
} catch (const std::exception &e) {
std::cerr << "failed to parse json: " << e.what() << "\n";
return TestCfgs{};
}
TestCfgs tests;
for (const auto &t : j["tests"]) {
std::string name = t["name"];
size_t threads = static_cast<size_t>(t["threads"]);
size_t iter = static_cast<size_t>(t["iter"]);
size_t capacity = static_cast<size_t>(t["capacity"]);
auto functions = t["functions"];
std::vector<std::string> fns;
fns.reserve(functions.size());
for (const auto &f : functions) {
fns.push_back(f.get<std::string>());
}
DistrCfg distr_cfg = parseDistrCfg(t);
tests.push_back(
makeTestCfg(threads, iter, capacity, name, distr_cfg, fns));
}
return tests;
}
class TestRunner {
public:
TestRunner(TestCfgs tests) : _tests(std::move(tests)) {}
void run() {
for (auto &t : _tests) {
std::cout << "INFO: running test: " << t->name << "\n";
switch (t->distr_cfg.type) {
case DistrType::INT:
runTest<int>(*t);
break;
case DistrType::DOUBLE:
runTest<double>(*t);
break;
case DistrType::STRING:
runTest<std::string>(*t);
break;
default:
throw std::runtime_error("invalid distribution type");
}
std::cout << "INFO: completed test: " << t->name << "\n";
}
}
void expect() {}
private:
template <typename T> void runTestFull(const TestCfg &test) {
cache::CacheManager<T, T, bench::TbbBench> cache(test.capacity);
const auto &data =
std::any_cast<const std::vector<T> &>(test.distr_data);
std::vector<T> keys = data;
size_t size = data.size();
std::reverse(keys.begin(), keys.end());
auto test_start = std::chrono::steady_clock::now();
for (auto i = 0; i < test.iter; ++i) {
std::vector<std::thread> pool;
for (auto j = 0; j < test.threads; ++j) {
pool.emplace_back([&]() {
for (const auto &f : test.fns) {
for (auto k = 0; k < size; ++k) {
const T &key = keys[k];
const T &val = data[k];
if (f == "add") {
auto res = cache.add(key, val);
} else if (f == "get") {
auto res = cache.getItem(key);
if (res == std::nullopt) {
DPRINT("Key not found");
} else {
DPRINT("Key found");
}
} else if (f == "contains") {
auto res = cache.contains(key);
if (res) {
DPRINT("Contains key");
} else {
DPRINT("Does not contain key");
}
} else if (f == "remove") {
auto res = cache.remove(key);
if (res) {
DPRINT("Removed key");
} else {
DPRINT("Did not remove key");
}
} else {
std::cerr << "ERROR: invalid function in "
"configuration: "
<< f << "\n";
}
}
}
});
}
for (auto &th : pool) {
th.join();
}
}
auto test_end = std::chrono::steady_clock::now(); // end timing
std::chrono::duration<double> elapsed = test_end - test_start;
std::cout << std::format("Test '{}' completed in {:.6f} seconds\n",
test.name, elapsed.count());
DPRINT("END BENCHMARK");
auto bm = cache.benchmark();
bench::printBenchmark(bm);
bench::writeBenchmark(bm);
}
std::vector<std::string> fn_map = {"add", "get", "contains", "remove"};
template <typename T> void runTest(const TestCfg &test) {
size_t num_shards = 31;
size_t shard_capacity = (test.capacity + num_shards - 1) / num_shards;
cache::CacheManager<T, T, bench::TbbBench> cache(shard_capacity);
const auto &data = std::any_cast<const std::vector<T> &>(test.distr_data);
size_t size = data.size();
// PRE-WARM: Load cache on main thread
std::cout << "INFO: Pre-warming cache with " << std::min(size, test.capacity) << " items...\n";
for (size_t i = 0; i < std::min(size, test.capacity); ++i) {
cache.add(data[i], data[i]);
}
std::cout << "INFO: Cache warmed up\n";
// Now test with worker threads
std::vector<std::thread> pool;
size_t total = size * test.iter;
size_t ops = (total + test.threads - 1) / test.threads;
std::cout << std::format("INFO: ops per thread: {}\n", ops);
auto test_start = std::chrono::steady_clock::now();
for (size_t j = 0; j < test.threads; ++j) {
pool.emplace_back([&, j]() { // Capture j by value!
// Sequential access pattern (no randomness)
for (size_t k = 0; k < ops; ++k) {
size_t idx = k % size; // Cycle through keys
const T &key = data[idx];
// Just do lookups
auto res = cache.getItem(key);
if (res == std::nullopt) {
DPRINT("Key not found");
} else {
DPRINT("Key found");
}
}
});
}
for (auto &th : pool) {
th.join();
}
auto test_end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = test_end - test_start;
std::cout << std::format("Test '{}' completed in {:.6f} seconds\n",
test.name, elapsed.count());
auto bm = cache.benchmark();
bench::printBenchmark(bm);
bench::writeBenchmark(bm);
}
template <typename T> void runTestChunks(const TestCfg &test) {
cache::CacheManager<T, T, bench::TbbBench> cache(test.capacity);
const auto &data =
std::any_cast<const std::vector<T> &>(test.distr_data);
std::vector<T> keys = data;
std::reverse(keys.begin(), keys.end());
size_t size = data.size();
auto test_start = std::chrono::steady_clock::now();
for (auto i = 0; i < test.iter; ++i) {
std::vector<std::thread> pool;
// calculate per-thread work
size_t chunk_size =
(size + test.threads - 1) / test.threads; // ceil division
for (auto j = 0; j < test.threads; ++j) {
size_t start = j * chunk_size;
size_t end = std::min(start + chunk_size, size);
pool.emplace_back([&, start, end]() {
std::mt19937 gen(std::random_device{}() + j); // thread-local RNG
std::uniform_int_distribution<size_t> dist_key(0, size - 1);
std::uniform_int_distribution<size_t> dist_fn(
0, fn_map.size() - 1);
for (auto k = 0; k < chunk_size; ++k) {
size_t idx = dist_key(gen);
std::string &f = fn_map[dist_fn(gen)];
const T &key = keys[idx];
const T &val = data[idx];
if (f == "add") {
cache.add(key, val);
} else if (f == "get") {
auto res = cache.getItem(key);
if (res == std::nullopt)
DPRINT("Key not found");
else
DPRINT("Key found");
} else if (f == "contains") {
auto res = cache.contains(key);
DPRINT(res ? "Contains key"
: "Does not contain key");
} else if (f == "remove") {
auto res = cache.remove(key);
DPRINT(res ? "Removed key" : "Did not remove key");
} else {
std::cerr
<< "ERROR: invalid function in configuration: "
<< f << "\n";
}
}
});
}
for (auto &th : pool) {
th.join();
}
}
auto test_end = std::chrono::steady_clock::now(); // end timing
std::chrono::duration<double> elapsed = test_end - test_start;
std::cout << std::format("Test '{}' completed in {:.6f} seconds\n",
test.name, elapsed.count());
DPRINT("END BENCHMARK");
auto bm = cache.benchmark();
bench::printBenchmark(bm);
bench::writeBenchmark(bm);
}
TestCfgs _tests;
}
;
// EOF