summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 0325a5719ece976005cde407d6755cc02e3e8a20 (plain)
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
#include <chrono>
#include <cinttypes>
#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));
  }
}

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::uint64_t seed;
  if (argc > 3) {
    try {
      parse_int(argv[3], seed);
    } catch (std::exception &e) {
      std::println(stderr, "{}", e.what());
      return 1;
    }
  } else {
    seed = std::chrono::system_clock::now().time_since_epoch().count();
  }

  std::default_random_engine rng{seed};

  std::println(stderr, "starting benchmark");

  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::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;
}