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
|
#include <array>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <errno.h>
#include <fstream>
#include <print>
#include <random>
#include <string>
#include <string_view>
#include <vector>
#include <boost/sqlite.hpp>
void parse_int(std::string_view arg, std::uint64_t &i) {
auto [_, ec] = std::from_chars(arg.data(), arg.data() + arg.size(), i);
if (ec == std::errc{}) {
return;
} else {
throw std::runtime_error(std::format("failed to parse {} as u64", arg));
}
}
std::uint64_t random_u64() {
std::ifstream ifstream{"/dev/random", std::ios::binary};
if (!ifstream.is_open()) {
throw std::runtime_error(
std::format("failed to open /dev/random: {}", std::strerror(errno)));
}
std::array<char, 8> buff;
ifstream.read(buff.data(), 8);
std::uint64_t i;
std::memcpy(&i, buff.data(), 8);
return i;
}
int main(int argc, char **argv) {
if (argc < 3) {
std::println(stderr, "usage: bench <database> <limit> [seed]");
return 1;
}
auto *path = argv[1];
auto *limit = argv[2];
std::println(stderr, "starting benchmark");
std::uint64_t seed;
if (argc > 3) {
try {
parse_int(argv[3], seed);
} catch (std::exception &e) {
std::println(stderr, "{}", e.what());
return 1;
}
} else {
try {
seed = random_u64();
} catch (std::exception &e) {
std::println(stderr, "{}", e.what());
return 1;
}
}
std::println(stderr, "using random seed: {}", seed);
boost::sqlite::connection connection{path};
std::vector<std::string> keys;
auto st = connection.prepare("select kv.key from kv limit ?");
for (const auto row : st.execute({limit})) {
auto key = row.at(0).get_text();
keys.push_back(key);
}
std::default_random_engine rng{seed};
std::shuffle(keys.begin(), keys.end(), rng);
std::println(stderr, "slurped {} keys into memory", keys.size());
auto start = std::chrono::system_clock::now();
st = connection.prepare("select kv.value from kv where kv.key=?");
for (const auto &key : keys) {
auto row = st.execute({key});
if (row.done()) {
std::println(stderr, "found no value for key: {}", key);
} else {
continue;
}
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> d = end - start;
std::println(stderr, "selected {} keys in {} ({})", keys.size(), d,
keys.size() / d.count());
return 0;
}
|