// M10.6c probe-marker prediction traces (ADR-0094). Development measurement, not a test of // game behaviour: the engineering client's own session code (apps/evo-client // remote_session.hpp) against a loopback region that simulates at 50 ms in real time, through // the owned UDP impairment relay. Each profile scripts WASD-like movement, then stops and // requires the prediction to converge on the authority. // // evo-motion-trace [--seconds S] [--profile NAME] [--smoke] // // One line per profile: // motion PROFILE rtt=MS jitter=MS loss=% samples=N exact=% p50/p95/p99/max=MM corrections=N // snaps=N refused=N dropped=N resends=N lead=TICKS max_pending=N converged=0|1 // and for the restart profile: restart resync_ms=MS converged=0|1. // --smoke runs one short impaired profile plus the restart and exits 1 unless both converge, // the input list stayed bounded and most predictions were exact (the CTest). #include "evo/client/session_host.hpp" #include "evo/network/dev_credentials.hpp" #include "evo/network/session_grant.hpp" #include "evo/server/region_sessions.hpp" #include "remote_session.hpp" #include "udp_relay.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace evo; using namespace evo::network; using Clock = std::chrono::steady_clock; using std::chrono::milliseconds; constexpr std::string_view server_name = "dev-region.evolution.test"; constexpr world::WorldIdentity world_id{9, 2}; constexpr std::uint64_t region = 21; constexpr std::chrono::milliseconds tick_period{50}; struct Profile { std::string name; milliseconds one_way{}, jitter{}; double loss{}; }; struct Result { std::vector errors; client_app::RemoteStatus status; bool converged{}; double resync_ms{}; }; std::uint64_t now_seconds() { return static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count()); } double percentile_mm(std::vector v, double p) { if (v.empty()) return 0; std::ranges::sort(v); const auto rank = static_cast(p / 100 * static_cast(v.size() - 1)); return static_cast(v.at(rank)) / 1000; } struct Files { std::filesystem::path dir, ca, certificate, key; std::string ca_pem; }; std::optional credentials() { const auto made = generate_dev_credentials(DeploymentEnvironment::dev, server_name, 1, std::chrono::system_clock::now()); if (!made) return std::nullopt; Files f; f.dir = std::filesystem::temp_directory_path() / "evo-motion-trace"; std::filesystem::remove_all(f.dir); std::filesystem::create_directories(f.dir); f.ca_pem = made->authority_pem; f.ca = f.dir / "ca.pem"; f.certificate = f.dir / "server.pem"; f.key = f.dir / "server.key"; for (const auto& [path, text] : {std::pair{f.ca, made->authority_pem}, std::pair{f.certificate, made->server_certificate_pem}, std::pair{f.key, made->server_key_pem}}) { std::ofstream out{path, std::ios::binary | std::ios::trunc}; out << text; if (!out) return std::nullopt; } return f; } std::array scalar() { std::array s{}; s.back() = 0x2C; return s; } // One profile: a fresh region, relay and client session. class Trace { public: Trace(const Files& f, const Profile& p) : files_{f}, profile_{p} {} std::optional run(std::chrono::seconds moving, bool restart) { auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); if (!key) return std::nullopt; server::RegionSessionsConfig c; c.transport.certificate_pem_file = files_.certificate.string(); c.transport.key_pem_file = files_.key.string(); c.control.environment = DeploymentEnvironment::dev; c.control.world = world_id; c.control.region = region; c.control.epoch = epoch_; c.issuer_keys.push_back({1, *key->public_key()}); auto sessions = server::RegionSessions::create(std::move(c)); if (!sessions) return std::nullopt; sessions_ = std::move(*sessions); sessions_->set_default_interest({0, 0, 0, 7}); if (!sessions_->set_motion_area({-20'000'000, -20'000'000, 20'999'999, 20'999'999, 0}, 500'000, 500'000)) return std::nullopt; impair::Impairment way; way.delay = profile_.one_way; way.jitter = profile_.jitter; way.loss = profile_.loss; relay_ = impair::UdpRelay::start(sessions_->port(), way, way, 0xE6); if (!relay_) return std::nullopt; std::vector grants; for (std::uint64_t serial = 1; serial <= 4; ++serial) { GrantClaims g; g.grant_id = 700 + serial; g.account.bytes.fill(0x5A); g.serial = serial; g.world = world_id; g.region = region; g.issued_at = now_seconds(); g.expires_at = g.issued_at + 300; const auto signed_grant = key->sign(g); if (!signed_grant) return std::nullopt; grants.push_back(*signed_grant); } client::SessionHost host; if (!host.connect(client_app::RemoteSession::open({{"127.0.0.1", relay_->port()}, std::string{server_name}, files_.ca_pem, files_.ca.string(), std::move(grants)}))) return std::nullopt; client::UiState ui; client::Intention intention; auto* session = host.session(); const auto connected = [&] { return session && session->status().phase == "connected"; }; if (!pump(host, ui, intention, connected, std::chrono::seconds{10})) return std::nullopt; Result r; // Movement: a new direction every 0.4-1.1 s (seeded), held in between. std::uint64_t seed = 0x9E37'79B9'7F4A'7C15ULL; const auto next = [&] { seed = (seed ^ (seed >> 31)) * 0xBF58'476D'1CE4'E5B9ULL + 1; return seed; }; const std::array, 9> directions{{{1000, 0}, {-1000, 0}, {0, 1000}, {0, -1000}, {707, 707}, {-707, 707}, {707, -707}, {-707, -707}, {0, 0}}}; const auto end = Clock::now() + moving; auto change = Clock::now(); while (Clock::now() < end) { if (Clock::now() >= change) { const auto d = directions.at(next() % directions.size()); intention.move_x = d.first; intention.move_y = d.second; change = Clock::now() + milliseconds{400 + static_cast(next() % 700)}; } if (!pump( host, ui, intention, [] { return false; }, milliseconds{10}) && !host.session()) return std::nullopt; collect(r, *host.session()); } // Stop and require convergence: no input in flight, prediction equal to the authority. intention.move_x = intention.move_y = 0; r.converged = pump( host, ui, intention, [&] { return settled(*host.session()); }, std::chrono::seconds{5}); collect(r, *host.session()); if (restart) { // A restart: the epoch advances, the session reconnects with its next grant and the // marker is respawned; resync is the time until the prediction matches again. const auto before = host.session()->status().session; ++epoch_; const auto start = Clock::now(); const bool back = pump( host, ui, intention, [&] { const auto s = host.session()->status(); return s.phase == "connected" && s.session != before && s.marker && settled(*host.session()); }, std::chrono::seconds{15}); r.resync_ms = std::chrono::duration(Clock::now() - start).count(); r.converged = r.converged && back; collect(r, *host.session()); } r.status = host.session()->status(); return r; } private: // Region ticks every 50 ms of real time; the client steps every millisecond. bool pump(client::SessionHost& host, client::UiState& ui, const client::Intention& intention, const std::function& done, std::chrono::milliseconds limit) { const auto end = Clock::now() + limit; while (Clock::now() < end) { const auto now = Clock::now(); if (now >= next_tick_) { ++tick_; next_tick_ = std::max(next_tick_ + tick_period, now - tick_period); } sessions_->poll({true, epoch_, tick_}, now_seconds(), now); if (tick_ != replicated_) { replicated_ = tick_; sessions_->replicate(tick_, [](const SessionInterest&) { ReplicationSource s; s.identity = world_id; return s; }); } static_cast(sessions_->take_events()); host.step(Clock::now(), intention, ui); if (!host.session()) return false; if (done()) return true; std::this_thread::sleep_for(milliseconds{1}); } return done(); } // Nothing in flight and the presented (smoothed) marker on the authoritative position. bool settled(client_app::RemoteSession& s) { const auto status = s.status(); const auto* m = sessions_->markers().of(status.session); return status.marker && m != nullptr && status.inputs_pending == 0 && m->state.vector == motion::Vector{} && std::llround(status.marker_x_mm * 1000) == m->state.x && std::llround(status.marker_y_mm * 1000) == m->state.y; } static void collect(Result& r, client_app::RemoteSession& s) { for (const auto e : s.take_prediction_errors()) r.errors.push_back(e); } const Files& files_; Profile profile_; std::unique_ptr sessions_; std::unique_ptr relay_; std::uint64_t epoch_{1}, tick_{1}, replicated_{}; Clock::time_point next_tick_{Clock::now()}; }; void report(const Profile& p, const Result& r) { const auto exact = std::ranges::count(r.errors, std::uint64_t{0}); const auto share = r.errors.empty() ? 0.0 : 100.0 * static_cast(exact) / static_cast(r.errors.size()); const auto& s = r.status; std::printf( "motion %s rtt=%lld jitter=%lld loss=%.1f samples=%zu exact=%.1f " "p50/p95/p99/max=%.1f/%.1f/%.1f/%.1f corrections=%llu snaps=%llu refused=%llu " "dropped=%llu resends=%llu lead=%llu max_pending=%llu converged=%d\n", p.name.c_str(), 2 * static_cast(p.one_way.count()), static_cast(p.jitter.count()), p.loss * 100, r.errors.size(), share, percentile_mm(r.errors, 50), percentile_mm(r.errors, 95), percentile_mm(r.errors, 99), percentile_mm(r.errors, 100), static_cast(s.corrections), static_cast(s.snaps), static_cast(s.refused_inputs), static_cast(s.dropped_inputs), static_cast(s.resends), static_cast(s.lead), static_cast(s.max_pending), r.converged ? 1 : 0); std::fflush(stdout); } } // namespace int main(int argc, char** argv) { std::chrono::seconds seconds{20}; std::string only; bool smoke = false; for (int i = 1; i < argc; ++i) { const std::string_view a = argv[i]; if (a == "--smoke") smoke = true; else if (a == "--seconds" && i + 1 < argc) seconds = std::chrono::seconds{std::max(1, std::atoi(argv[++i]))}; else if (a == "--profile" && i + 1 < argc) only = argv[++i]; else { std::fputs("usage: evo-motion-trace [--seconds S] [--profile NAME] [--smoke]\n", stderr); return 2; } } const auto files = credentials(); if (!files) return 2; std::vector profiles{{"clean", milliseconds{0}, milliseconds{0}, 0.0}, {"lan", milliseconds{10}, milliseconds{2}, 0.0}, {"rtt60-loss2", milliseconds{30}, milliseconds{5}, 0.02}, {"rtt120-loss2", milliseconds{60}, milliseconds{10}, 0.02}, {"rtt200-loss5", milliseconds{100}, milliseconds{20}, 0.05}}; if (smoke) { profiles = {{"rtt60-loss2", milliseconds{30}, milliseconds{5}, 0.02}}; seconds = std::chrono::seconds{6}; } int code = 0; for (const auto& p : profiles) { if (!only.empty() && p.name != only) continue; Trace t{*files, p}; const auto r = t.run(seconds, smoke || p.name == "rtt120-loss2"); if (!r) { std::printf("motion %s setup-failed\n", p.name.c_str()); code = 1; continue; } report(p, *r); if (r->resync_ms > 0) std::printf("restart %s resync_ms=%.0f converged=%d\n", p.name.c_str(), r->resync_ms, r->converged ? 1 : 0); const auto exact = std::ranges::count(r->errors, std::uint64_t{0}); if (smoke && (!r->converged || r->status.max_pending > 32 || r->errors.empty() || exact * 2 < static_cast(r->errors.size()))) code = 1; } std::filesystem::remove_all(files->dir); return code; }