300 lines
8.7 KiB
C++
300 lines
8.7 KiB
C++
// Applications-stack workload baseline.
|
|
//
|
|
// Measures the replicated KV propose/quorum-read paths, the replicated
|
|
// queue publish/claim/ack path and the three-listener HTTP fixture roundtrip.
|
|
// Each case runs warmup, several repetitions, and reports p50/p95/p99 plus a
|
|
// 95% confidence interval for the mean. No latency threshold is asserted:
|
|
// numbers are workload baselines, per PERFORMANCE.md.
|
|
//
|
|
// host_graph_bench [--quick] [--json <path>]
|
|
|
|
#include "distributed_host/cluster_fixture.hpp"
|
|
#include "distributed_host/replicated_kv.hpp"
|
|
#include "distributed_host/replicated_queue.hpp"
|
|
|
|
#include <Void/Net/Http.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <format>
|
|
#include <fstream>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
void check(bool condition, const char* what)
|
|
{
|
|
if (!condition) {
|
|
std::fprintf(stderr, "benchmark invariant failed: %s\n", what);
|
|
std::exit(1);
|
|
}
|
|
}
|
|
|
|
struct Stats {
|
|
std::string name;
|
|
std::size_t samples{0};
|
|
double p50_ns{0};
|
|
double p95_ns{0};
|
|
double p99_ns{0};
|
|
double mean_ns{0};
|
|
double ci95_low_ns{0};
|
|
double ci95_high_ns{0};
|
|
double ops_per_sec{0};
|
|
};
|
|
|
|
[[nodiscard]] double percentile(std::vector<double> values, double p)
|
|
{
|
|
if (values.empty()) {
|
|
return 0;
|
|
}
|
|
std::ranges::sort(values);
|
|
const double rank = (p / 100.0) * static_cast<double>(values.size());
|
|
auto index = static_cast<std::size_t>(std::ceil(rank));
|
|
index = index == 0 ? 0 : index - 1;
|
|
return values[std::min(index, values.size() - 1)];
|
|
}
|
|
|
|
class Bench {
|
|
public:
|
|
|
|
Bench(std::string name, std::size_t reps, std::size_t ops,
|
|
std::size_t warmup = 3)
|
|
: m_name(std::move(name)), m_reps(reps), m_ops(ops),
|
|
m_warmup(warmup)
|
|
{
|
|
}
|
|
|
|
/// `fn` runs outside the timer for setup/epoch resets; `op` is the
|
|
/// measured operation.
|
|
void run(const std::function<void()>& setup,
|
|
const std::function<void()>& op)
|
|
{
|
|
for (std::size_t i = 0; i < m_warmup; ++i) {
|
|
setup();
|
|
op();
|
|
}
|
|
m_samples.reserve(m_reps * m_ops);
|
|
for (std::size_t rep = 0; rep < m_reps; ++rep) {
|
|
std::size_t epoch = 0;
|
|
for (std::size_t i = 0; i < m_ops; ++i) {
|
|
if (epoch == 0) {
|
|
setup();
|
|
epoch = kEpochOps;
|
|
}
|
|
--epoch;
|
|
const auto begin = Clock::now();
|
|
op();
|
|
const auto elapsed = Clock::now() - begin;
|
|
m_samples.push_back(
|
|
static_cast<double>(
|
|
std::chrono::duration_cast<
|
|
std::chrono::nanoseconds>(elapsed)
|
|
.count()));
|
|
}
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] Stats stats() const
|
|
{
|
|
Stats out;
|
|
out.name = m_name;
|
|
out.samples = m_samples.size();
|
|
double total = 0;
|
|
for (const double sample : m_samples) {
|
|
total += sample;
|
|
}
|
|
out.mean_ns = m_samples.empty() ? 0 : total / m_samples.size();
|
|
double variance = 0;
|
|
for (const double sample : m_samples) {
|
|
const double delta = sample - out.mean_ns;
|
|
variance += delta * delta;
|
|
}
|
|
variance = m_samples.size() > 1
|
|
? variance / (m_samples.size() - 1)
|
|
: 0;
|
|
const double standard_error =
|
|
std::sqrt(variance / static_cast<double>(m_samples.size()));
|
|
out.ci95_low_ns = std::max(0.0, out.mean_ns - 1.96 * standard_error);
|
|
out.ci95_high_ns = out.mean_ns + 1.96 * standard_error;
|
|
out.p50_ns = percentile(m_samples, 50);
|
|
out.p95_ns = percentile(m_samples, 95);
|
|
out.p99_ns = percentile(m_samples, 99);
|
|
out.ops_per_sec =
|
|
out.mean_ns > 0 ? 1e9 / out.mean_ns : 0;
|
|
return out;
|
|
}
|
|
|
|
private:
|
|
|
|
static constexpr std::size_t kEpochOps = 32;
|
|
|
|
std::string m_name;
|
|
std::size_t m_reps;
|
|
std::size_t m_ops;
|
|
std::size_t m_warmup;
|
|
std::vector<double> m_samples;
|
|
};
|
|
|
|
struct Options {
|
|
bool quick{false};
|
|
std::string json_path{};
|
|
};
|
|
|
|
[[nodiscard]] Options parse_options(int argc, char** argv)
|
|
{
|
|
Options options;
|
|
for (int i = 1; i < argc; ++i) {
|
|
const std::string_view arg{argv[i]};
|
|
if (arg == "--quick") {
|
|
options.quick = true;
|
|
} else if (arg == "--json" && i + 1 < argc) {
|
|
options.json_path = argv[++i];
|
|
}
|
|
}
|
|
return options;
|
|
}
|
|
|
|
[[nodiscard]] DistributedHost::ReplicatedKvCluster
|
|
fresh_cluster(std::string& leader)
|
|
{
|
|
DistributedHost::ReplicatedKvCluster cluster{{"a", "b", "c"}};
|
|
check(cluster.elect("a").has_value(), "elect leader");
|
|
leader = "a";
|
|
return cluster;
|
|
}
|
|
|
|
void append_json(std::string& out, const Stats& stats)
|
|
{
|
|
out += std::format(
|
|
" \"{}\": {{\"samples\": {}, \"p50_ns\": {:.2f}, "
|
|
"\"p95_ns\": {:.2f}, \"p99_ns\": {:.2f}, \"mean_ns\": {:.2f}, "
|
|
"\"ci95_ns\": [{:.2f}, {:.2f}], \"ops_per_sec\": {:.2f}}}",
|
|
stats.name, stats.samples, stats.p50_ns, stats.p95_ns, stats.p99_ns,
|
|
stats.mean_ns, stats.ci95_low_ns, stats.ci95_high_ns,
|
|
stats.ops_per_sec);
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
using namespace DistributedHost;
|
|
const Options options = parse_options(argc, argv);
|
|
const std::size_t reps = options.quick ? 1 : 5;
|
|
const std::size_t ops = options.quick ? 16 : 200;
|
|
|
|
std::vector<Stats> results;
|
|
|
|
// ── replicated KV propose ─────────────────────────────────────────────
|
|
{
|
|
std::optional<ReplicatedKvCluster> cluster;
|
|
std::string leader;
|
|
std::uint64_t sequence = 0;
|
|
Bench bench{"kv_propose_3node", reps, ops};
|
|
bench.run(
|
|
[&] {
|
|
cluster = fresh_cluster(leader);
|
|
sequence = 0;
|
|
},
|
|
[&] {
|
|
const auto reply = cluster->propose(
|
|
leader, "key-" + std::to_string(sequence), std::string{"value"});
|
|
check(reply.outcome == KvWriteOutcome::Applied,
|
|
"propose is majority-committed");
|
|
++sequence;
|
|
});
|
|
results.push_back(bench.stats());
|
|
}
|
|
|
|
// ── replicated KV quorum read ────────────────────────────────────────
|
|
{
|
|
std::string leader;
|
|
auto cluster = fresh_cluster(leader);
|
|
check(cluster.propose(leader, "hot", std::string{"value"}).outcome ==
|
|
KvWriteOutcome::Applied,
|
|
"seed value");
|
|
Bench bench{"kv_quorum_read_3node", reps, ops * 5};
|
|
bench.run([] {}, [&] {
|
|
auto read = cluster.quorum_read(leader, "hot");
|
|
check(read.has_value() && read->has_value(), "quorum read");
|
|
});
|
|
results.push_back(bench.stats());
|
|
}
|
|
|
|
// ── replicated queue publish/claim/ack ───────────────────────────────
|
|
{
|
|
std::optional<ReplicatedKvCluster> cluster;
|
|
std::optional<ReplicatedQueue> queue;
|
|
std::uint64_t sequence = 0;
|
|
QueueConfig config;
|
|
config.max_jobs = 1000000;
|
|
Bench bench{"queue_publish_claim_ack_3node", reps, ops};
|
|
bench.run(
|
|
[&] {
|
|
std::string leader;
|
|
cluster = fresh_cluster(leader);
|
|
queue.emplace(*cluster, leader, config);
|
|
sequence = 0;
|
|
},
|
|
[&] {
|
|
const std::string key = "job-" + std::to_string(sequence++);
|
|
auto id = queue->publish("payload", key);
|
|
check(id.has_value(), "publish");
|
|
auto claimed = queue->claim("worker");
|
|
check(claimed.has_value() && claimed->has_value(), "claim");
|
|
check(queue->ack("worker", *id).has_value(), "ack");
|
|
});
|
|
results.push_back(bench.stats());
|
|
}
|
|
|
|
// ── HTTP fixture roundtrip ────────────────────────────────────────────
|
|
{
|
|
auto fixture = ClusterFixture::create();
|
|
check(fixture.has_value(), "fixture create");
|
|
check((*fixture)->start().has_value(), "fixture start");
|
|
const std::string url = (*fixture)->endpoint("a") + "/peer/health";
|
|
Void::Net::HttpClient client;
|
|
const std::vector<Void::Net::Header> headers{
|
|
Void::Net::Header{"authorization", "Bearer peer-secret"}};
|
|
Bench bench{"http_peer_health_roundtrip", reps, ops};
|
|
bench.run([] {}, [&] {
|
|
auto response = client.get(url, headers);
|
|
check(response.has_value() && response->status == 200,
|
|
"http health roundtrip");
|
|
});
|
|
results.push_back(bench.stats());
|
|
(*fixture)->stop();
|
|
}
|
|
|
|
for (const auto& stats : results) {
|
|
std::printf(
|
|
"%-28s samples=%-6zu p50=%10.1fns p95=%10.1fns p99=%10.1fns "
|
|
"mean=%10.1fns ci95=[%.1f, %.1f] %.0f ops/s\n",
|
|
stats.name.c_str(), stats.samples, stats.p50_ns, stats.p95_ns,
|
|
stats.p99_ns, stats.mean_ns, stats.ci95_low_ns, stats.ci95_high_ns,
|
|
stats.ops_per_sec);
|
|
}
|
|
|
|
if (!options.json_path.empty()) {
|
|
std::ofstream out{options.json_path};
|
|
check(out.is_open(), "open json output");
|
|
out << "{\n \"reps\": " << reps << ", \"ops_per_rep\": " << ops
|
|
<< ",\n \"cases\": {\n";
|
|
for (std::size_t i = 0; i < results.size(); ++i) {
|
|
std::string line;
|
|
append_json(line, results[i]);
|
|
out << line << (i + 1 == results.size() ? "\n" : ",\n");
|
|
}
|
|
out << " }\n}\n";
|
|
}
|
|
return 0;
|
|
}
|