diff --git a/Bench.cpp b/Bench.cpp new file mode 100644 index 0000000..35866b5 --- /dev/null +++ b/Bench.cpp @@ -0,0 +1,45 @@ +#include "ConflictSet.h" + +#define ANKERL_NANOBENCH_IMPLEMENT +#include "third_party/nanobench.h" + +std::string toKey(int n) { + std::string result; + result.resize(32); + + for (int i = 0; i < 32; ++i) { + result[i] = n & (1 << (31 - i)) ? '1' : '0'; + } + return result; +} + +constexpr int kNumKeys = 100000; + +int main(void) { + ankerl::nanobench::Bench bench; + + int readKey = kNumKeys / 2; + + ConflictSet cs(0); + for (int i = 0; i < kNumKeys; ++i) { + if (i != readKey) { + continue; + } + auto key = toKey(i); + ConflictSet::WriteRange w; + w.begin.p = (const uint8_t *)key.data(); + w.begin.len = key.size(); + w.end.len = 0; + w.writeVersion = 1; + cs.addWrites(&w, 1); + } + + auto key = toKey(readKey); + ConflictSet::ReadRange r; + r.begin.p = (const uint8_t *)key.data(); + r.begin.len = key.size(); + r.end.len = 0; + r.readVersion = 0; + ConflictSet::Result result; + bench.run("point read", [&]() { cs.check(&r, &result, 1); }); +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c4d221..496b4c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,11 @@ if(BUILD_TESTING) ${CMAKE_SOURCE_DIR}/test_symbols.sh $ ${CMAKE_SOURCE_DIR}/symbols.txt) endif() + + # bench + + add_executable(conflict_set_bench Bench.cpp) + target_link_libraries(conflict_set_bench PRIVATE ${PROJECT_NAME}) endif() # packaging diff --git a/ConflictSet.cpp b/ConflictSet.cpp index 02b5563..26a727b 100644 --- a/ConflictSet.cpp +++ b/ConflictSet.cpp @@ -1178,30 +1178,6 @@ void __throw_length_error(const char *) { __builtin_unreachable(); } } // namespace std #ifdef ENABLE_MAIN -#define ANKERL_NANOBENCH_IMPLEMENT -#include "third_party/nanobench.h" - -void bench() { - ankerl::nanobench::Bench bench; - { - auto *n = newNode(); - for (int i = 0; i < 64; ++i) { - getOrCreateChild(n, i) = newNode(); - bench.run("getChildLeq" + std::to_string(i), - [&]() { bench.doNotOptimizeAway(getChildLeq(n, 255)); }); - } - destroyTree(n); - } - { - auto *n = newNode(); - for (int i = 255; i >= 3 * 64; --i) { - getOrCreateChild(n, i) = newNode(); - bench.run("getChildGeq" + std::to_string(i), - [&]() { bench.doNotOptimizeAway(getChildGeq(n, 0)); }); - } - destroyTree(n); - } -} void printTree() { int64_t writeVersion = 0; @@ -1224,8 +1200,7 @@ void printTree() { } int main(void) { - bench(); - // printTree(); + printTree(); return 0; } #endif