#include "leanstore/KVInterface.hpp" #include "leanstore/Slice.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include "instant.h" using leanstore::IsolationLevel; using leanstore::LeanStore; using leanstore::Slice; using leanstore::TxMode; using leanstore::storage::btree::TransactionKV; #define begin() leanstore::cr::WorkerContext::My().StartTx(); #define readonly() \ leanstore::cr::WorkerContext::My().StartTx(TxMode::kShortRunning, \ IsolationLevel::kSnapshotIsolation, true) #define commit() leanstore::cr::WorkerContext::My().CommitTx(); #define rollback() leanstore::cr::WorkerContext::My().AbortTx(); static auto OpenDB(bool isNew) -> std::pair, TransactionKV*> { const char* dbroot = "/home/abby/leanstore_bench"; StoreOption* option = CreateStoreOption(dbroot); option->mCreateFromScratch = isNew; option->mWorkerThreads = std::thread::hardware_concurrency(); option->mEnableWalFsync = false; auto res = LeanStore::Open(option); if (!res) { DestroyStoreOption(option); std::cerr << "open store failed: " << res.error().ToString() << std::endl; exit(1); } auto store = std::move(res.value()); leanstore::storage::btree::TransactionKV* tx; store->ExecSync(0, [&store, &tx, isNew] { if (isNew) { auto r = store->CreateTransactionKV("tx_tree"); if (!r) { printf("can't create transaction tree: %s\n", r.error().ToString().c_str()); abort(); } tx = r.value(); } else { store->GetTransactionKV("tx_tree", &tx); } }); return std::pair{std::move(store), tx}; } int main() { auto [store, tx] = OpenDB(true); size_t cnt = 100000; std::vector keys{}; for (size_t i = 0; i < cnt; ++i) keys.push_back(std::format("{:08}", i)); auto b = nm::Instant::now(); for (auto& k : keys) { store->ExecSync(1, [tx, k] { begin(); auto x = Slice{k.data(), k.length()}; tx->Insert(x, x); commit(); }); } auto e1 = b.elapse_ms(); b.reset(); for (auto& k : keys) { store->ExecSync(1, [tx, k] { readonly(); tx->Lookup({k.data(), k.length()}, [](auto x) { (void)x; }); commit(); }); } auto e2 = b.elapse_ms(); store.reset(); auto [store1, tx1] = OpenDB(false); b.reset(); for (auto& k : keys) { store1->ExecSync(1, [tx1, k] { readonly(); tx1->Lookup({k.data(), k.length()}, [](auto x) { (void)x; }); commit(); }); } auto e3 = b.elapse_ms(); std::cout << std::format("{:<10}{}ms\n{:<10}{}ms\n{:<10}{}ms\n", "put", (size_t)e1, "hot get", (size_t)e2, "cold get", (size_t)e3); }