Spaces:
Running
Running
File size: 13,094 Bytes
4531c3e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | #include "../include/pulsefold.hpp"
#include "baselines.hpp"
#include "workloads.hpp"
#include "wide_radix.hpp"
#include <array>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <set>
#include <stdexcept>
#include <string>
#include <tuple>
namespace {
using namespace pulsefold::benchmarks;
using Policies = std::vector<std::vector<Time>>;
struct View {
const Id* members;
std::size_t length;
const Id* begin() const { return members; }
const Id* end() const { return members + length; }
std::size_t size() const { return length; }
};
// Independent compilation: recover a policy's stable subscriber ordering,
// one expiry TTL per distinct value and one shared list of member IDs.
struct Policy {
explicit Policy(const std::vector<Time>& times) : members(times.size()), rank(times.size()) {
std::iota(members.begin(), members.end(), Id{0});
std::stable_sort(members.begin(), members.end(), [&](Id a, Id b) { return times[a] < times[b]; });
for (std::size_t i = 0; i < members.size(); ++i) {
const Id id = members[i];
rank[id] = i;
if (i == 0 || times[id] != thresholds.back()) {
thresholds.push_back(times[id]);
starts.push_back(i);
}
}
starts.push_back(members.size());
}
View range(std::size_t first, std::size_t past) const {
return {members.data() + starts[first], starts[past] - starts[first]};
}
std::vector<Id> members;
std::vector<std::size_t> rank, starts;
std::vector<Time> thresholds;
};
struct Case {
Scenario schedule;
Id members, groups;
};
constexpr Case cases[] = {
{{"small-healthy", 64, 1, 6, false, Pattern::healthy}, 256, 4},
{{"large-mixed", 4096, 1, 6, false, Pattern::mixed}, 1024, 4},
{{"rotating-outages", 1024, 1, 3, false, Pattern::loss}, 1024, 8},
{{"synchronous-storm", 512, 1, 4, true, Pattern::storm}, 1024, 1},
{{"all-unique", 256, 1, 3, false, Pattern::loss}, 128, 128},
};
struct Work {
Trace trace;
Policies policies;
std::vector<Id> feed_policy;
std::vector<Policy> compiled;
Work(const Case& c, Time seed) : trace(generate(c.schedule, seed)),
feed_policy(c.schedule.feeds, 0) {
// Generate two explicitly shared policies only when the case has a
// distinct short-SLO cohort. IDs in each TTL group are interleaved.
const bool mixed = c.schedule.pattern == Pattern::mixed;
for (unsigned p = 0; p < (mixed ? 2u : 1u); ++p) {
std::vector<Time> times;
const Time period = c.schedule.feeds;
for (Id id = 0; id < c.members; ++id) {
const Time group = id % c.groups;
const Time ttl = p == 1
? period / 2 + group * period / c.groups
: c.schedule.min_periods * period + group * 2 * period / c.groups;
times.push_back(ttl);
}
policies.push_back(times);
compiled.emplace_back(times);
}
if (mixed) {
for (Id f = 0; f < c.schedule.feeds; f += 32) feed_policy[f] = 1;
}
trace.config.clear(); // The shared policies above are the measured contracts.
}
};
class GroupScan {
const Work& work_;
struct Feed { Time last = 0; std::size_t cursor = 0; bool armed = false; };
std::vector<Feed> feeds_;
using Expiry = std::tuple<Time, Id, std::size_t>;
std::vector<Expiry> expired_;
Time earliest_ = std::numeric_limits<Time>::max();
public:
explicit GroupScan(const Work& work) : work_(work), feeds_(work.feed_policy.size()) {}
template<class Emit>
void advance_groups(Time now, Emit&& emit) {
if (now < earliest_) return;
earliest_ = std::numeric_limits<Time>::max();
expired_.clear();
for (Id f = 0; f < feeds_.size(); ++f) {
auto& feed = feeds_[f];
if (!feed.armed) continue;
const auto& p = work_.compiled[work_.feed_policy[f]];
while (feed.cursor < p.thresholds.size()) {
const Time due = feed.last + p.thresholds[feed.cursor];
if (due > now) {
earliest_ = std::min(earliest_, due);
break;
}
expired_.emplace_back(due, f, feed.cursor++);
}
}
if (!std::is_sorted(expired_.begin(), expired_.end()))
std::sort(expired_.begin(), expired_.end());
for (const auto& [time, f, g] : expired_)
emit(time, f, work_.compiled[work_.feed_policy[f]].range(g, g + 1), false);
}
template<class Emit>
void observe_groups(Id f, Time now, Emit&& emit) {
advance_groups(now, emit);
auto& feed = feeds_[f];
const auto& p = work_.compiled[work_.feed_policy[f]];
if (feed.cursor) emit(now, f, p.range(0, feed.cursor), true);
feed = {now, 0, true};
earliest_ = std::min(earliest_, now + p.thresholds.front());
}
};
// One radix-queue virtual subscriber per distinct TTL. This competitor also
// shares its full member lists across feeds and can deliver whole views.
template<class Queue>
class GroupRadix {
const Work& work_;
Queue engine_;
static Policies group_ttls(const Work& work) {
Policies ttls;
ttls.reserve(work.feed_policy.size());
for (Id policy : work.feed_policy) ttls.push_back(work.compiled[policy].thresholds);
return ttls;
}
public:
explicit GroupRadix(const Work& work) : work_(work), engine_(group_ttls(work)) {}
template<class Emit>
void advance_groups(Time now, Emit&& emit) {
engine_.advance(now, [&](Time t, Id f, Id g, bool fresh) {
emit(t, f, work_.compiled[work_.feed_policy[f]].range(g, g + 1), fresh);
});
}
template<class Emit>
void observe_groups(Id f, Time now, Emit&& emit) {
std::size_t recovered = 0;
engine_.observe(f, now, [&](Time t, Id feed, Id g, bool fresh) {
if (fresh) { recovered = g + 1; return; }
emit(t, feed, work_.compiled[work_.feed_policy[feed]].range(g, g + 1), false);
});
if (recovered) emit(now, f, work_.compiled[work_.feed_policy[f]].range(0, recovered), true);
}
};
using BinaryGroupRadix = GroupRadix<pulsefold::bench::LazyRadix>;
using WideGroupRadix = GroupRadix<pulsefold::bench::WideRadix>;
struct Product : pulsefold::Monitor {
explicit Product(const Work& work) : Monitor(work.policies, work.feed_policy) {}
};
template<class Engine, class Emit>
void apply(Engine& engine, Operation op, Emit& emit) {
if (op.feed == poll) engine.advance_groups(op.time, emit);
else engine.observe_groups(op.feed, op.time, emit);
}
using Record = std::tuple<std::size_t, Time, Id, std::size_t, std::size_t, bool>;
// Compare the actual ordered transition streams before timing. Resolve each
// immutable view once by checking EVERY subscriber against the canonical
// policy member list. A record then names that exact interval in that policy.
template<class Engine>
void verify(const Work& work, std::vector<Record>& expected, bool capture) {
Engine engine(work);
std::set<std::tuple<Id, std::uintptr_t, std::size_t>> checked;
std::size_t index = 0;
for (std::size_t i = 0; i < work.trace.operations.size(); ++i) {
auto check = [&](Time t, Id feed, auto view, bool fresh) {
const Id policy = work.feed_policy.at(feed);
const auto& p = work.compiled.at(policy);
if (!view.size()) throw std::logic_error("empty callback");
const auto offset = p.rank.at(*view.begin());
const auto key = std::make_tuple(policy, reinterpret_cast<std::uintptr_t>(view.begin()), view.size());
if (checked.insert(key).second &&
(view.size() > p.members.size() - offset ||
!std::equal(view.begin(), view.end(), p.members.begin() + offset)))
throw std::logic_error("group membership mismatch");
const Record r{i, t, feed, offset, view.size(), fresh};
if (capture) expected.push_back(r);
else if (index >= expected.size() || r != expected[index])
throw std::logic_error("ordered group stream mismatch");
++index;
};
apply(engine, work.trace.operations[i], check);
}
if (index != expected.size()) throw std::logic_error("group count mismatch");
}
struct Totals {
Time groups = 0, members = 0, checksum = 0;
template<class V>
void operator()(Time t, Id f, V view, bool fresh) {
++groups;
members += view.size();
checksum += mix(t ^ (Time(f) << 32) ^ *view.begin() ^ mix(view.size()) ^ fresh);
}
};
struct Result {
Totals totals;
double mean, p99;
};
template<class Engine>
Result run(const Work& work) {
Engine engine(work);
const auto& trace = work.trace;
Totals warm, totals;
for (std::size_t i = 0; i < trace.warmup; ++i) apply(engine, trace.operations[i], warm);
const auto count = trace.operations.size() - trace.warmup;
std::vector<double> samples;
samples.reserve(count / batch_size);
double wall_ns = 0;
for (auto b = trace.warmup; b < trace.operations.size(); b += batch_size) {
const auto e = std::min(b + batch_size, trace.operations.size());
const auto start = Clock::now();
for (auto i = b; i < e; ++i) apply(engine, trace.operations[i], totals);
const double ns = std::chrono::duration<double, std::nano>(Clock::now() - start).count();
wall_ns += ns;
if (e - b == batch_size) samples.push_back(ns / batch_size);
}
std::sort(samples.begin(), samples.end());
return {totals, wall_ns / count, samples[(99 * samples.size() - 1) / 100]};
}
unsigned long long number(const std::string& s) {
if (s.empty() || s.find_first_not_of("0123456789") != std::string::npos)
throw std::invalid_argument("expected a nonnegative integer");
return std::stoull(s);
}
int benchmark(int argc, char** argv) {
unsigned reps = 5;
Time seed = 20261122;
std::string selected;
for (int i = 1; i < argc; ++i) {
const std::string flag = argv[i];
if (flag == "--help") {
std::cout << "grouped [--repeats N] [--seed N] [--scenario NAME]\n";
return 0;
}
if (i + 1 == argc) throw std::invalid_argument("missing value for " + flag);
const std::string val = argv[++i];
if (flag == "--repeats") {
auto n = number(val);
if (!n || n > 1000) throw std::invalid_argument("repeats must be in [1,1000]");
reps = static_cast<unsigned>(n);
} else if (flag == "--seed") seed = number(val);
else if (flag == "--scenario") selected = val;
else throw std::invalid_argument("unknown option " + flag);
}
constexpr std::array<const char*, 4> names = {"pulsefold", "group-scan", "group-radix", "group-wide-radix"};
using Fn = Result(*)(const Work&);
const std::array<Fn, names.size()> runners = {run<Product>, run<GroupScan>, run<BinaryGroupRadix>, run<WideGroupRadix>};
std::cout << "scenario,seed,repeat,run_order,engine,feeds,subscribers_per_feed,distinct_ttls,"
"warmup_ops,measured_ops,group_callbacks,represented_transitions,checksum,batch_size,"
"mean_ns_per_op,p99_batch_mean_ns_per_op\n";
bool found = false;
for (const auto& c : cases) {
if (!selected.empty() && selected != c.schedule.name) continue;
found = true;
const Work work(c, seed);
std::vector<Record> expected;
verify<Product>(work, expected, true);
verify<GroupScan>(work, expected, false);
verify<BinaryGroupRadix>(work, expected, false);
verify<WideGroupRadix>(work, expected, false);
for (unsigned rep = 0; rep < reps; ++rep) {
for (unsigned order = 0; order < names.size(); ++order) {
const auto id = (rep + order) % names.size();
const auto r = runners[id](work);
std::cout << c.schedule.name << ',' << seed << ',' << rep + 1 << ',' << order + 1 << ','
<< names[id] << ',' << c.schedule.feeds << ',' << c.members << ',' << c.groups
<< ',' << work.trace.warmup << ',' << work.trace.operations.size() - work.trace.warmup
<< ',' << r.totals.groups << ',' << r.totals.members << ',' << r.totals.checksum
<< ',' << batch_size << ',' << std::fixed << std::setprecision(3) << r.mean << ','
<< r.p99 << '\n';
}
}
}
if (!found) throw std::invalid_argument("unknown scenario " + selected);
return 0;
}
} // namespace
int main(int argc, char** argv) {
try { return benchmark(argc, argv); }
catch (const std::exception& e) { std::cerr << e.what() << '\n'; return EXIT_FAILURE; }
}
|