Add benchmark
All checks were successful
Tests / Release [gcc] total: 363, passed: 363
GNU C Compiler (gcc) |Total|New|Outstanding|Fixed|Trend
|:-:|:-:|:-:|:-:|:-:
|0|0|0|0|:clap:
Reference build: <a href="https://jenkins.weaselab.dev/job/weaselab/job/conflict-set/job/main/12//gcc">weaselab » conflict-set » main #12</a>
Tests / Coverage total: 361, passed: 361
weaselab/conflict-set/pipeline/head This commit looks good
All checks were successful
Tests / Release [gcc] total: 363, passed: 363
GNU C Compiler (gcc) |Total|New|Outstanding|Fixed|Trend
|:-:|:-:|:-:|:-:|:-:
|0|0|0|0|:clap:
Reference build: <a href="https://jenkins.weaselab.dev/job/weaselab/job/conflict-set/job/main/12//gcc">weaselab » conflict-set » main #12</a>
Tests / Coverage total: 361, passed: 361
weaselab/conflict-set/pipeline/head This commit looks good
This commit is contained in:
207
Bench.cpp
207
Bench.cpp
@@ -1,5 +1,7 @@
|
||||
#include "ConflictSet.h"
|
||||
#include "Internal.h"
|
||||
#include <byteswap.h>
|
||||
#include <cstdint>
|
||||
|
||||
#define ANKERL_NANOBENCH_IMPLEMENT
|
||||
#include "third_party/nanobench.h"
|
||||
@@ -15,94 +17,165 @@ std::string toKey(int n) {
|
||||
}
|
||||
|
||||
constexpr int kNumKeys = 100000;
|
||||
// A range read, a point read, and a point write. Range writes can erase
|
||||
// keys, and we don't want to change the number of keys stored in the
|
||||
// conflict set.
|
||||
constexpr int kOpsPerTx = 100;
|
||||
|
||||
void benchPointRead() {
|
||||
ankerl::nanobench::Bench bench;
|
||||
constexpr int kPrefixLen = 0;
|
||||
|
||||
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); });
|
||||
std::span<const uint8_t> makeKey(Arena &arena, int index) {
|
||||
auto result = std::span<uint8_t>{new (arena) uint8_t[4], 4};
|
||||
index = __builtin_bswap32(index);
|
||||
memcpy(result.data(), &index, 4);
|
||||
return result;
|
||||
}
|
||||
|
||||
void benchPointerSet() {
|
||||
template <class ConflictSet_> void benchConflictSet() {
|
||||
ankerl::nanobench::Bench bench;
|
||||
ConflictSet_ cs{0};
|
||||
|
||||
constexpr int kNumPointers = 32;
|
||||
bench.batch(kNumPointers);
|
||||
bench.batch(kOpsPerTx);
|
||||
|
||||
std::vector<void *> pointers;
|
||||
for (int i = 0; i < kNumPointers; ++i) {
|
||||
pointers.push_back(malloc(1));
|
||||
int64_t version = 0;
|
||||
|
||||
// Populate conflict set
|
||||
Arena arena;
|
||||
{
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
writes.reserve(kNumKeys);
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
auto key = makeKey(arena, i);
|
||||
ConflictSet::WriteRange conflict;
|
||||
conflict.begin.p = key.data();
|
||||
conflict.begin.len = key.size();
|
||||
conflict.end.len = 0;
|
||||
conflict.writeVersion = version + 1;
|
||||
writes.push_back(conflict);
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
++version;
|
||||
}
|
||||
|
||||
bench.run("Create and destroy Arena hashset", [&]() {
|
||||
Arena arena;
|
||||
auto h = hashSet<void *>(arena);
|
||||
for (auto p : pointers) {
|
||||
h.insert(p);
|
||||
// I don't know why std::less didn't work /shrug
|
||||
struct Less {
|
||||
bool operator()(const std::span<const uint8_t> &lhs,
|
||||
const std::span<const uint8_t> &rhs) const {
|
||||
return lhs < rhs;
|
||||
}
|
||||
});
|
||||
};
|
||||
auto points = set<std::span<const uint8_t>, Less>(arena);
|
||||
|
||||
bench.run("Create and destroy malloc hashset", [&]() {
|
||||
std::unordered_set<void *, MyHash<void *>> h;
|
||||
for (auto p : pointers) {
|
||||
h.insert(p);
|
||||
}
|
||||
});
|
||||
// Two points for each range read, one for each point read, and one for each
|
||||
// point write
|
||||
while (points.size() < kOpsPerTx * 2 + 1) {
|
||||
// TODO don't use rand?
|
||||
points.insert(makeKey(arena, rand() % kNumKeys));
|
||||
}
|
||||
|
||||
bench.run("Create and destroy Arena vector", [&]() {
|
||||
Arena arena;
|
||||
auto h = vector<void *>(arena);
|
||||
for (auto p : pointers) {
|
||||
h.push_back(p);
|
||||
// Make short-circuiting non-trivial
|
||||
{
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
writes.reserve(kNumKeys);
|
||||
for (int i = 0; i < kNumKeys; ++i) {
|
||||
auto key = makeKey(arena, i);
|
||||
if (points.find(key) != points.end()) {
|
||||
continue;
|
||||
}
|
||||
ConflictSet::WriteRange conflict;
|
||||
conflict.begin.p = key.data();
|
||||
conflict.begin.len = key.size();
|
||||
conflict.end.len = 0;
|
||||
conflict.writeVersion = version + 1;
|
||||
writes.push_back(conflict);
|
||||
}
|
||||
});
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
++version;
|
||||
}
|
||||
|
||||
{
|
||||
Arena arena;
|
||||
auto h = hashSet<void *>(arena);
|
||||
bench.run("Find hashset", [&]() {
|
||||
for (auto p : pointers) {
|
||||
bench.doNotOptimizeAway(h.find(p));
|
||||
std::vector<ConflictSet::ReadRange> reads;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx; ++i) {
|
||||
ConflictSet::ReadRange r;
|
||||
r.begin.p = iter->data();
|
||||
r.begin.len = iter->size();
|
||||
r.end.len = 0;
|
||||
r.readVersion = version - 1;
|
||||
reads.push_back(r);
|
||||
++iter;
|
||||
}
|
||||
|
||||
auto *results = new (arena) ConflictSet::Result[kOpsPerTx];
|
||||
|
||||
bench.run("radix tree (point reads)",
|
||||
[&]() { cs.check(reads.data(), results, kOpsPerTx); });
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<ConflictSet::ReadRange> reads;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx; ++i) {
|
||||
auto begin = *iter++;
|
||||
auto end = *iter++;
|
||||
ConflictSet::ReadRange r;
|
||||
r.begin.p = begin.data();
|
||||
r.begin.len = begin.size();
|
||||
r.end.p = end.data();
|
||||
r.end.len = end.size();
|
||||
r.readVersion = version - 1;
|
||||
reads.push_back(r);
|
||||
}
|
||||
|
||||
auto *results = new (arena) ConflictSet::Result[kOpsPerTx];
|
||||
|
||||
bench.run("radix tree (range reads)",
|
||||
[&]() { cs.check(reads.data(), results, kOpsPerTx); });
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx; ++i) {
|
||||
ConflictSet::WriteRange w;
|
||||
w.begin.p = iter->data();
|
||||
w.begin.len = iter->size();
|
||||
w.end.len = 0;
|
||||
writes.push_back(w);
|
||||
++iter;
|
||||
}
|
||||
|
||||
bench.run("radix tree (point writes)", [&]() {
|
||||
auto v = ++version;
|
||||
for (auto &w : writes) {
|
||||
w.writeVersion = v;
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
bench.run("Find vector", [&]() {
|
||||
for (auto p : pointers) {
|
||||
bench.doNotOptimizeAway(std::find(pointers.begin(), pointers.end(), p));
|
||||
}
|
||||
});
|
||||
}
|
||||
std::vector<ConflictSet::WriteRange> writes;
|
||||
auto iter = points.begin();
|
||||
for (int i = 0; i < kOpsPerTx - 1; ++i) {
|
||||
auto begin = *iter++;
|
||||
auto end = *iter++;
|
||||
ConflictSet::WriteRange w;
|
||||
w.begin.p = begin.data();
|
||||
w.begin.len = begin.size();
|
||||
w.end.p = end.data();
|
||||
w.end.len = end.size();
|
||||
writes.push_back(w);
|
||||
}
|
||||
|
||||
for (auto p : pointers) {
|
||||
free(p);
|
||||
bench.run("radix tree (range writes)", [&]() {
|
||||
auto v = ++version;
|
||||
for (auto &w : writes) {
|
||||
w.writeVersion = v;
|
||||
}
|
||||
cs.addWrites(writes.data(), writes.size());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
// benchPointRead();
|
||||
benchPointerSet();
|
||||
}
|
||||
int main(void) { benchConflictSet<ConflictSet>(); }
|
Reference in New Issue
Block a user