Make server_bench workload point writes of tuple-encoded keys

This commit is contained in:
2024-11-08 20:50:54 -08:00
parent 33f9c89328
commit 224d21648a

View File

@@ -1,3 +1,4 @@
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <cstdlib>
@@ -19,31 +20,69 @@
#include <vector>
#include "ConflictSet.h"
#include "Internal.h"
#include "third_party/nadeau.h"
std::atomic<int64_t> transactions;
constexpr int kWindowSize = 10000000;
int64_t safeUnaryMinus(int64_t x) {
return x == std::numeric_limits<int64_t>::min() ? x : -x;
}
constexpr int kNumPrefixes = 250000;
void tupleAppend(std::string &output, int64_t value) {
if (value == 0) {
output.push_back(0x14);
return;
}
uint32_t size = 8 - __builtin_clrsbll(value) / 8;
int typeCode = 0x14 + (value < 0 ? -1 : 1) * size;
output.push_back(typeCode);
if (value < 0) {
value = ~safeUnaryMinus(value);
}
uint64_t swap = __builtin_bswap64(value);
output.insert(output.end(), (uint8_t *)&swap + 8 - size,
(uint8_t *)&swap + 8);
}
std::string makeKey(int64_t num, int suffixLen) {
void tupleAppend(std::string &output, std::string_view value) {
output.push_back('\x02');
for (auto c : value) {
if (c == '\x00') {
output.push_back('\x00');
output.push_back('\xff');
} else {
output.push_back(c);
}
}
output.push_back('\x00');
}
template <class... Ts> std::string tupleKey(const Ts &...ts) {
std::string result;
result.resize(sizeof(int64_t) + suffixLen);
int64_t be = __builtin_bswap64(num);
memcpy(result.data(), &be, sizeof(int64_t));
memset(result.data() + sizeof(int64_t), 0, suffixLen);
(tupleAppend(result, ts), ...);
return result;
}
constexpr int kWindowSize = 300000;
void workload(weaselab::ConflictSet *cs) {
int64_t version = kWindowSize;
constexpr int kNumWrites = 16;
for (;; transactions.fetch_add(1, std::memory_order_relaxed)) {
std::vector<int64_t> keyIndices;
for (int i = 0; i < kNumWrites; ++i) {
keyIndices.push_back(rand() % 100'000'000);
}
std::sort(keyIndices.begin(), keyIndices.end());
std::vector<std::string> keys;
std::vector<weaselab::ConflictSet::WriteRange> writes;
constexpr std::string_view suffix = "this is a suffix";
for (int i = 0; i < kNumWrites; ++i) {
keys.push_back(makeKey(rand() % kNumPrefixes, rand() % 50));
keys.push_back(tupleKey(0x100, i, keyIndices[i],
suffix.substr(0, rand() % suffix.size()),
rand()));
// printf("%s\n", printable(keys.back()).c_str());
}
for (int i = 0; i < kNumWrites; ++i) {
writes.push_back({{(const uint8_t *)keys[i].data(), int(keys[i].size())},