From 76d0785b331aedbeacb8565832c2b87c89c2a7e4 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Tue, 25 Jun 2024 20:46:48 -0700 Subject: [PATCH] Add worst-case benchmark for radix tree Closes #27 --- Bench.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/Bench.cpp b/Bench.cpp index 0f8e81a..3ab5b5f 100644 --- a/Bench.cpp +++ b/Bench.cpp @@ -258,4 +258,75 @@ void benchConflictSet() { } } -int main(void) { benchConflictSet(); } +void benchWorstCaseForRadixRangeRead() { + ankerl::nanobench::Bench bench; + ConflictSet cs{0}; + + int64_t version = 0; + + constexpr int kKeyLength = 50; + + for (int i = 0; i < kKeyLength; ++i) { + for (int j = 0; j < 256; ++j) { + auto b = std::vector(i, 0); + b.push_back(j); + auto e = std::vector(i, 255); + e.push_back(j); + weaselab::ConflictSet::WriteRange w[] = {{ + {b.data(), int(b.size())}, + {nullptr, 0}, + }, + { + {e.data(), int(e.size())}, + {nullptr, 0}, + }}; + cs.addWrites(w, sizeof(w) / sizeof(w[0]), version); + } + } + + // Defeat short-circuiting on the left + { + auto k = std::vector(kKeyLength, 0); + weaselab::ConflictSet::WriteRange w[] = { + { + {k.data(), int(k.size())}, + {nullptr, 0}, + }, + }; + cs.addWrites(w, sizeof(w) / sizeof(w[0]), 2); + } + + // Defeat short-circuiting on the right + { + auto k = std::vector(kKeyLength, 255); + weaselab::ConflictSet::WriteRange w[] = { + { + {k.data(), int(k.size())}, + {nullptr, 0}, + }, + }; + cs.addWrites(w, sizeof(w) / sizeof(w[0]), 2); + } + + auto begin = std::vector(kKeyLength - 1, 0); + begin.push_back(1); + auto end = std::vector(kKeyLength - 1, 255); + end.push_back(254); + + weaselab::ConflictSet::Result result; + weaselab::ConflictSet::ReadRange r{ + {begin.data(), int(begin.size())}, {end.data(), int(end.size())}, 1}; + + bench.run("worst case for radix tree", [&]() { + result = weaselab::ConflictSet::TooOld; + cs.check(&r, &result, 1); + if (result != weaselab::ConflictSet::Commit) { + abort(); + } + }); +} + +int main(void) { + benchConflictSet(); + benchWorstCaseForRadixRangeRead(); +}