|
| 1 | +// Observe the real adapter, including its callers, before the static archive |
| 2 | +// is searched. Only the existing work macro is overridden. |
| 3 | +#include <pineforge/source/pine_strategy_host.hpp> |
| 4 | +#include "../src/source/pine_cohort_exposure.hpp" |
| 5 | + |
| 6 | +#include <array> |
| 7 | +#include <cmath> |
| 8 | +#include <cstddef> |
| 9 | +#include <cstdint> |
| 10 | +#include <cstdio> |
| 11 | +#include <cstring> |
| 12 | +#include <fstream> |
| 13 | +#include <limits> |
| 14 | +#include <sstream> |
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace exposure_work_fixture { |
| 19 | +struct Site { |
| 20 | + const char* name; |
| 21 | + std::uint64_t calls = 0; |
| 22 | +}; |
| 23 | +struct Work { |
| 24 | + const void* owner = nullptr; |
| 25 | + std::uint64_t run = 0; |
| 26 | + bool have_run = false, identity_ok = true, compact = true, singleton = true; |
| 27 | + std::uint64_t calls = 0, steps = 0, windows = 0, rows = 0; |
| 28 | + std::uint64_t step_events = 0, window_events = 0, row_events = 0; |
| 29 | + std::uint64_t window = 0, max_window = 0, max_rows = 0; |
| 30 | + std::uint64_t path_b = 0, abandoned = 0; |
| 31 | + std::array<Site, 10> sites{{ |
| 32 | + {"cohort_exposure_site_short_seed_context_is_live"}, |
| 33 | + {"cohort_exposure_site_reconcile_exit_reservations_after_margin"}, |
| 34 | + {"cohort_exposure_site_place_entry"}, |
| 35 | + {"cohort_exposure_site_withdraw_unplaced_cap_adds"}, |
| 36 | + {"cohort_exposure_site_schedule_tv_money_long_margin_before_trail"}, |
| 37 | + {"cohort_exposure_site_fill_pooc_close_exits"}, |
| 38 | + {"cohort_exposure_site_on_applied_opening"}, |
| 39 | + {"cohort_exposure_site_on_applied_held"}, |
| 40 | + {"cohort_exposure_site_on_applied_margin_revival"}, |
| 41 | + {"cohort_exposure_site_PendingIntentView_copy_v1"} |
| 42 | + }}; |
| 43 | +}; |
| 44 | +inline Work* active = nullptr; |
| 45 | + |
| 46 | +void observe(const char* site, const void* owner, std::uint64_t run, |
| 47 | + std::uint64_t count) noexcept { |
| 48 | + if (!active || std::strncmp(site, "cohort_exposure_", 16) != 0) return; |
| 49 | + auto& work = *active; |
| 50 | + work.identity_ok = work.identity_ok && owner == work.owner; |
| 51 | + if (!work.have_run) { |
| 52 | + work.run = run; |
| 53 | + work.have_run = true; |
| 54 | + } |
| 55 | + work.identity_ok = work.identity_ok && run == work.run; |
| 56 | + if (std::strcmp(site, "cohort_exposure_call") == 0) work.calls += count; |
| 57 | + else if (std::strcmp(site, "cohort_exposure_step") == 0) { |
| 58 | + work.steps += count; |
| 59 | + ++work.step_events; |
| 60 | + } else if (std::strcmp(site, "cohort_exposure_window") == 0) { |
| 61 | + work.windows += count; |
| 62 | + work.window = count; |
| 63 | + if (count > work.max_window) work.max_window = count; |
| 64 | + ++work.window_events; |
| 65 | + } else if (std::strcmp(site, "cohort_exposure_rows") == 0) { |
| 66 | + work.rows += count; |
| 67 | + if (count > work.max_rows) work.max_rows = count; |
| 68 | + work.compact = work.compact && work.window <= 4 * count + 16; |
| 69 | + work.singleton = work.singleton && work.window <= 1 && count <= 1; |
| 70 | + ++work.row_events; |
| 71 | + } else if (std::strcmp(site, "cohort_exposure_path_b") == 0) work.path_b += count; |
| 72 | + else if (std::strcmp(site, "cohort_exposure_b_abandoned") == 0) work.abandoned += count; |
| 73 | + else { |
| 74 | + for (auto& group : work.sites) |
| 75 | + if (std::strcmp(site, group.name) == 0) group.calls += count; |
| 76 | + } |
| 77 | +} |
| 78 | +} // namespace exposure_work_fixture |
| 79 | + |
| 80 | +#define PINEFORGE_LOOKUP_INDEX_WORK(site, owner, run, count) \ |
| 81 | + ::exposure_work_fixture::observe(site, owner, run, count) |
| 82 | +#include "../src/source/pine_adapter.cpp" |
| 83 | +#undef PINEFORGE_LOOKUP_INDEX_WORK |
| 84 | + |
| 85 | +namespace exposure_work_fixture { |
| 86 | +using namespace pineforge; |
| 87 | +int failures = 0; |
| 88 | +#define CHECK(condition) do { \ |
| 89 | + if (!(condition)) { \ |
| 90 | + std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, #condition); \ |
| 91 | + ++failures; \ |
| 92 | + } \ |
| 93 | +} while (false) |
| 94 | + |
| 95 | +constexpr std::array<int, 3> sizes{{128, 256, 512}}; |
| 96 | +constexpr double na = std::numeric_limits<double>::quiet_NaN(); |
| 97 | + |
| 98 | +class OpeningHost final : public source::PineStrategyHost { |
| 99 | +public: |
| 100 | + OpeningHost(int entries, bool reused) : entries_(entries), reused_(reused) { |
| 101 | + source::PineStrategyConfig config; |
| 102 | + config.initial_capital = 10000000.0; |
| 103 | + config.default_qty_type = static_cast<int>(QtyType::FIXED); |
| 104 | + config.default_qty_value = 1.0; |
| 105 | + config.pyramiding = entries + 1; |
| 106 | + config.calc_on_order_fills = true; |
| 107 | + config.margin_long = config.margin_short = 0.0; |
| 108 | + configure_pine_strategy(config); |
| 109 | + set_syminfo_timezone("UTC"); |
| 110 | + set_syminfo_session("24x7"); |
| 111 | + set_syminfo_mintick(0.25); |
| 112 | + } |
| 113 | + |
| 114 | + void on_source_bar(const Bar&) override { |
| 115 | + const int bar = pine_bar_index(); |
| 116 | + // A fill callback may revisit this bar. Issue exactly once per bar. |
| 117 | + if (bar == last_bar_ || bar >= entries_) return; |
| 118 | + last_bar_ = bar; |
| 119 | + strategy_entry(reused_ ? "entry" : "entry-" + std::to_string(bar), |
| 120 | + true, na, na, 1.0); |
| 121 | + ++issued_; |
| 122 | + } |
| 123 | + |
| 124 | + const void* adapter_identity() const noexcept { return &adapter_; } |
| 125 | + int issued() const noexcept { return issued_; } |
| 126 | + |
| 127 | + void hash_host_extension(BrokerStateHashSink& sink) const override { |
| 128 | + source::PineStrategyHost::hash_host_extension(sink); |
| 129 | + sink.s("exposure-work/openings"); |
| 130 | + sink.i(entries_); sink.b(reused_); |
| 131 | + sink.i(last_bar_); sink.i(issued_); |
| 132 | + } |
| 133 | + |
| 134 | +private: |
| 135 | + int entries_, last_bar_ = -1, issued_ = 0; |
| 136 | + bool reused_; |
| 137 | +}; |
| 138 | + |
| 139 | +Work measure(int entries, bool reused) { |
| 140 | + OpeningHost host(entries, reused); |
| 141 | + std::vector<Bar> bars; |
| 142 | + for (int i = 0; i < entries + 2; ++i) |
| 143 | + bars.push_back({100.0, 101.0, 99.0, 100.0, 1.0, |
| 144 | + 1736121600000LL + i * 60000LL}); |
| 145 | + Work work; |
| 146 | + work.owner = host.adapter_identity(); |
| 147 | + active = &work; |
| 148 | + host.run(bars.data(), static_cast<int>(bars.size()), "1", "1"); |
| 149 | + active = nullptr; |
| 150 | + CHECK(host.last_error().empty()); |
| 151 | + CHECK(host.native_state().kind == NativeLifecycleKind::Completed); |
| 152 | + CHECK(host.issued() == entries); |
| 153 | + CHECK(host.live_position_size() == static_cast<double>(entries)); |
| 154 | + CHECK(work.identity_ok && work.have_run); |
| 155 | + CHECK(work.calls > 0 && work.steps > 0); |
| 156 | + CHECK(work.step_events == work.calls); |
| 157 | + CHECK(work.window_events == work.calls && work.row_events == work.calls); |
| 158 | + const char* shape = reused ? "reused" : "distinct"; |
| 159 | + std::printf("exposure-work %s %d %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 160 | + shape, entries, static_cast<unsigned long long>(work.calls), |
| 161 | + static_cast<unsigned long long>(work.steps), |
| 162 | + static_cast<unsigned long long>(work.windows), |
| 163 | + static_cast<unsigned long long>(work.rows), |
| 164 | + static_cast<unsigned long long>(work.max_window), |
| 165 | + static_cast<unsigned long long>(work.max_rows), |
| 166 | + static_cast<unsigned long long>(work.path_b), |
| 167 | + static_cast<unsigned long long>(work.abandoned)); |
| 168 | + std::fflush(stdout); |
| 169 | + return work; |
| 170 | +} |
| 171 | + |
| 172 | +struct Baseline { |
| 173 | + std::string shape; |
| 174 | + int entries = 0; |
| 175 | + std::uint64_t calls = 0, steps = 0; |
| 176 | +}; |
| 177 | + |
| 178 | +std::vector<Baseline> read_baseline(const char* path) { |
| 179 | + std::ifstream input(path); |
| 180 | + CHECK(input.is_open()); |
| 181 | + std::vector<Baseline> rows; |
| 182 | + std::string line; |
| 183 | + while (std::getline(input, line)) { |
| 184 | + std::istringstream fields(line); |
| 185 | + std::string marker; |
| 186 | + Baseline row; |
| 187 | + if (fields >> marker && marker == "exposure-work") { |
| 188 | + CHECK(static_cast<bool>(fields >> row.shape >> row.entries |
| 189 | + >> row.calls >> row.steps)); |
| 190 | + rows.push_back(row); |
| 191 | + } |
| 192 | + } |
| 193 | + CHECK(rows.size() == 6); |
| 194 | + return rows; |
| 195 | +} |
| 196 | + |
| 197 | +std::uint64_t legacy_reused_steps(std::uint64_t n) { |
| 198 | + return 2 * n * (n + 1) * (n + 2) / 3; |
| 199 | +} |
| 200 | + |
| 201 | +void calibrate(const std::array<Work, 3>& work) { |
| 202 | + for (std::size_t i = 0; i < sizes.size(); ++i) { |
| 203 | + const auto expected = legacy_reused_steps(sizes[i]); |
| 204 | + const auto actual = work[i].steps; |
| 205 | + const auto delta = actual > expected ? actual - expected : expected - actual; |
| 206 | + CHECK(actual >= static_cast<std::uint64_t>(sizes[i]) * (sizes[i] + 1) / 2); |
| 207 | + CHECK(delta * 100 <= expected); |
| 208 | + CHECK(work[i].compact); |
| 209 | + std::printf("exposure-calibration %d actual=%llu expected=%llu compact=%d\n", |
| 210 | + sizes[i], static_cast<unsigned long long>(actual), |
| 211 | + static_cast<unsigned long long>(expected), work[i].compact ? 1 : 0); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void compare_baseline(const std::vector<Baseline>& baseline, |
| 216 | + const char* shape, const std::array<Work, 3>& work) { |
| 217 | + for (std::size_t i = 0; i < sizes.size(); ++i) { |
| 218 | + int matches = 0; |
| 219 | + for (const auto& row : baseline) { |
| 220 | + if (row.shape != shape || row.entries != sizes[i]) continue; |
| 221 | + ++matches; |
| 222 | + CHECK(work[i].calls == row.calls); |
| 223 | + CHECK(work[i].steps <= row.steps); |
| 224 | + if (row.shape == "reused") { |
| 225 | + const auto expected = legacy_reused_steps(row.entries); |
| 226 | + const auto delta = row.steps > expected |
| 227 | + ? row.steps - expected : expected - row.steps; |
| 228 | + CHECK(delta * 100 <= expected); |
| 229 | + } |
| 230 | + } |
| 231 | + CHECK(matches == 1); |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +void reused_growth(const std::array<Work, 3>& work) { |
| 236 | + for (std::size_t i = 0; i < sizes.size(); ++i) { |
| 237 | + const std::uint64_t n = sizes[i]; |
| 238 | + CHECK(work[i].compact); |
| 239 | + CHECK(work[i].steps >= n * (n + 1) / 2); |
| 240 | + CHECK(work[i].steps <= 8 * n * n); |
| 241 | + if (i) CHECK(work[i].steps <= 5 * work[i - 1].steps); |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +void distinct_guard(const std::array<Work, 3>& work) { |
| 246 | + for (std::size_t i = 0; i < sizes.size(); ++i) { |
| 247 | + // An exposed distinct id has one position and one row; pending |
| 248 | + // or emptied cohorts may have zero. The frozen selector visits |
| 249 | + // a contributing row twice, including its final unsuccessful scan. |
| 250 | + CHECK(work[i].singleton); |
| 251 | + CHECK(work[i].max_window == 1 && work[i].max_rows == 1); |
| 252 | + CHECK(work[i].steps <= 2 * work[i].calls); |
| 253 | + if (i) CHECK(work[i].steps <= 5 * work[i - 1].steps); |
| 254 | + } |
| 255 | +} |
| 256 | + |
| 257 | +void history_guard() { |
| 258 | + const auto fold = [](std::size_t reissues) { |
| 259 | + source::OriginRoster origins; |
| 260 | + std::unordered_map<std::uint64_t, double> units{{1, 1.0}}; |
| 261 | + origins.push_back({{"history", 1}, 1}); |
| 262 | + for (std::size_t i = 0; i < reissues; ++i) |
| 263 | + origins.push_back({{"history", 1}, i + 2}); |
| 264 | + CHECK(units.size() == 1); |
| 265 | + CHECK(origins.positions_of(1) && origins.positions_of(1)->size() == 1); |
| 266 | + CHECK(origins.members().size() > 4 * units.size() + 16); |
| 267 | + const auto result = source::detail::fold_cohort_exposure(origins, units); |
| 268 | + CHECK(result.total == 1.0); |
| 269 | + return result; |
| 270 | + }; |
| 271 | + const auto small = fold(64); |
| 272 | + const auto large = fold(128); |
| 273 | + const auto delta = large.steps > small.steps |
| 274 | + ? large.steps - small.steps : small.steps - large.steps; |
| 275 | + CHECK(delta <= 16); |
| 276 | + std::printf("exposure-history 64=%llu 128=%llu paths=%u,%u\n", |
| 277 | + static_cast<unsigned long long>(small.steps), |
| 278 | + static_cast<unsigned long long>(large.steps), |
| 279 | + static_cast<unsigned>(small.path), static_cast<unsigned>(large.path)); |
| 280 | +} |
| 281 | + |
| 282 | +int caller_groups() { |
| 283 | + for (const int entries : {256, 512}) { |
| 284 | + const auto work = measure(entries, true); |
| 285 | + if (failures) return 1; |
| 286 | + std::uint64_t attributed = 0; |
| 287 | + for (const auto& group : work.sites) attributed += group.calls; |
| 288 | + if (!attributed) { |
| 289 | + std::puts("SKIP cohort exposure caller groups: overlay events are absent"); |
| 290 | + return 0; |
| 291 | + } |
| 292 | + CHECK(work.sites[7].calls > 0); // Held-cohort loop on opening Applied. |
| 293 | + for (const auto& group : work.sites) |
| 294 | + std::printf("exposure-site reused %d %s %llu\n", entries, group.name, |
| 295 | + static_cast<unsigned long long>(group.calls)); |
| 296 | + } |
| 297 | + return failures == 0 ? 0 : 1; |
| 298 | +} |
| 299 | +} // namespace exposure_work_fixture |
| 300 | + |
| 301 | +int main(int argc, char** argv) { |
| 302 | + using namespace exposure_work_fixture; |
| 303 | + bool calibration = false; |
| 304 | + bool callers = false; |
| 305 | + const char* baseline_path = nullptr; |
| 306 | + for (int i = 1; i < argc; ++i) { |
| 307 | + if (std::strcmp(argv[i], "--calibrate") == 0) calibration = true; |
| 308 | + else if (std::strcmp(argv[i], "--caller-groups") == 0) callers = true; |
| 309 | + else if (std::strcmp(argv[i], "--baseline") == 0 && i + 1 < argc) |
| 310 | + baseline_path = argv[++i]; |
| 311 | + else return 2; |
| 312 | + } |
| 313 | + if (callers) { |
| 314 | + if (calibration || baseline_path) return 2; |
| 315 | + return caller_groups(); |
| 316 | + } |
| 317 | + if (calibration && baseline_path) return 2; |
| 318 | + std::array<Work, 3> reused, distinct; |
| 319 | + for (std::size_t i = 0; i < sizes.size(); ++i) { |
| 320 | + reused[i] = measure(sizes[i], true); |
| 321 | + distinct[i] = measure(sizes[i], false); |
| 322 | + } |
| 323 | + if (calibration) calibrate(reused); |
| 324 | + else reused_growth(reused); |
| 325 | + distinct_guard(distinct); |
| 326 | + history_guard(); |
| 327 | + if (baseline_path) { |
| 328 | + const auto baseline = read_baseline(baseline_path); |
| 329 | + compare_baseline(baseline, "reused", reused); |
| 330 | + compare_baseline(baseline, "distinct", distinct); |
| 331 | + } |
| 332 | + return failures == 0 ? 0 : 1; |
| 333 | +} |
0 commit comments