Skip to content

Instantly share code, notes, and snippets.

@jbosboom
Created August 30, 2020 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbosboom/0aaab7b9f0ff77e60a15db0aae1de40e to your computer and use it in GitHub Desktop.
Save jbosboom/0aaab7b9f0ff77e60a15db0aae1de40e to your computer and use it in GitHub Desktop.
LMDB benchmark testing sorted vs unsorted insert order for a hashtable
#include "precompiled.hpp"
#include "stopwatch.hpp"
#include "lmdb++.h"
int main() { //genbuild {'entrypoint': True, 'ldflags': '-llmdb'}
lmdb::env env = lmdb::env::create();
env.set_mapsize(1UL * 1024 * 1024 * 1024 * 1024);
env.set_max_dbs(64);
env.open(std::string("/home/jbosboom/scratch/benchmark.mdb").c_str(), MDB_NORDAHEAD); //TODO: flags?
lmdb::dbi hashtable;
{
lmdb::txn txn = lmdb::txn::begin(env);
hashtable = lmdb::dbi::open(txn, "gadget_hashtable", MDB_CREATE | MDB_INTEGERKEY);
txn.commit();
}
std::mt19937 rng(1235);
std::uniform_int_distribution<unsigned int> length(15, 300);
std::uniform_int_distribution<std::size_t> hash(0);
const bool sort = true;
const std::size_t batch_size = 5000;
std::vector<std::byte> data; //we're not bothering to mutate the data here (shouldn't matter)
data.resize(1024);
std::vector<std::size_t> hashes;
hashes.reserve(batch_size);
Stopwatch stopwatch = Stopwatch::process();
for (unsigned int batch = 0; batch < 100; ++batch) {
for (std::size_t i = 0; i < batch_size; ++i)
hashes.push_back(hash(rng));
if (sort)
std::sort(hashes.begin(), hashes.end());
auto txn = lmdb::txn::begin(env);
{
lmdb::cursor cur = lmdb::cursor::open(txn, hashtable);
for (std::size_t hash : hashes) {
std::string_view value(reinterpret_cast<const char*>(data.data()), length(rng));
cur.put(lmdb::to_sv(hash), value, MDB_NOOVERWRITE);
}
}
txn.commit();
}
Stopwatch::Result elapsed = stopwatch.elapsed();
fmt::print("{} {} {}\n", sort, elapsed.hms(), elapsed.userMillis(), elapsed.systemMillis());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment