// M10.6c/M10.7b probe-marker traces (ADR-0094, ADR-0095). 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 (one relay per client). // // evo-motion-trace [--seconds S] [--profile NAME] [--remote] [--smoke] // // Prediction (default): one client scripts WASD-like movement, then stops and requires its // prediction to converge on the authority. 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. // Remote (--remote): client A moves, client B shows A's marker interpolated; every 10 ms // B's presented position is compared with A's authoritative path at B's render tick. Midway, // B's link is cut for 1.5 s (outage). One line per profile: // remote PROFILE rtt=MS jitter=MS loss=% samples=N p50/p95/p99/max=MM // current_p99/max=MM (markers not shown stale) extrapolated=% stale=% delay=TICKS // outage_stale=% recovered_ms=MS // --smoke runs one short impaired profile (prediction: plus a restart) and exits 1 unless // its bounds hold (the CTests). #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 #include namespace { using namespace evo; using namespace evo::network; using Clock = std::chrono::steady_clock; using std::chrono::milliseconds; using Host = client::SessionHost; 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{}; }; std::uint64_t now_seconds() { return static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count()); } // Nearest-rank percentile of micrometre samples, in millimetres. 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; } double share(std::uint64_t part, std::uint64_t whole) { return whole == 0 ? 0.0 : 100.0 * static_cast(part) / static_cast(whole); } 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; } impair::Impairment impairment(const Profile& p) { impair::Impairment way; way.delay = p.one_way; way.jitter = p.jitter; way.loss = p.loss; return way; } // WASD-like movement: a new direction every 0.4-1.1 s (seeded), held in between. class Script { public: void step(client::Intention& intention) { if (Clock::now() < change_) return; static constexpr std::array, 9> directions{ {{1000, 0}, {-1000, 0}, {0, 1000}, {0, -1000}, {707, 707}, {-707, 707}, {707, -707}, {-707, -707}, {0, 0}}}; 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)}; } private: std::uint64_t next() { seed_ = ((seed_ ^ (seed_ >> 31)) * 0xBF58'476D'1CE4'E5B9ULL) + 1; return seed_; } std::uint64_t seed_{0x9E37'79B9'7F4A'7C15ULL}; Clock::time_point change_{}; }; // A loopback region on real 50 ms ticks with probe markers, and clients behind relays. class Bench { public: explicit Bench(const Files& f) : files_{f} {} bool start() { auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); if (!key) return false; key_.emplace(std::move(*key)); 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 false; sessions_ = std::move(*sessions); sessions_->set_default_interest({0, 0, 0, 7}); return sessions_->set_motion_area({-20'000'000, -20'000'000, 20'999'999, 20'999'999, 0}, 500'000, 500'000); } // A client session behind its own relay, with `grants` single-use grants. std::optional, std::unique_ptr>> client(std::uint8_t account, const Profile& p, std::uint64_t grants) { auto relay = impair::UdpRelay::start(sessions_->port(), impairment(p), impairment(p), 0xE6 + account); if (!relay) return std::nullopt; std::vector bundle; for (std::uint64_t serial = 1; serial <= grants; ++serial) { GrantClaims g; g.grant_id = (std::uint64_t{account} << 16) + serial; g.account.bytes.fill(account); g.serial = serial; g.world = world_id; g.region = region; g.issued_at = now_seconds(); g.expires_at = g.issued_at + 300; if (!key_) return std::nullopt; const auto signed_grant = key_->sign(g); if (!signed_grant) return std::nullopt; bundle.push_back(*signed_grant); } auto host = std::make_unique(); 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(bundle)}))) return std::nullopt; return std::pair{std::move(relay), std::move(host)}; } // One server iteration: a new tick every 50 ms of real time, then poll and replicate. void serve() { 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()); } // Steps the region and the clients (each with its intention) until `done` or `limit`. bool pump(const std::vector>& clients, const std::function& done, std::chrono::milliseconds limit) { client::UiState ui; const auto end = Clock::now() + limit; while (Clock::now() < end) { serve(); for (const auto& [host, intention] : clients) { host->step(Clock::now(), *intention, ui); if (!host->session()) return false; } if (done()) return true; std::this_thread::sleep_for(milliseconds{1}); } return done(); } [[nodiscard]] const motion::Marker* marker(std::uint64_t session) const { return sessions_->markers().of(session); } std::uint64_t epoch{1}, tick{1}; private: const Files& files_; std::optional key_; std::unique_ptr sessions_; std::uint64_t replicated_{}; Clock::time_point next_tick_{Clock::now()}; }; bool connected(Host& h) { return h.session() && h.session()->status().phase == "connected"; } // Nothing in flight and the presented (smoothed) own marker on the authoritative position. bool settled(const Bench& bench, client_app::RemoteSession& s) { const auto status = s.status(); const auto* m = bench.marker(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; } struct PredictionResult { std::vector errors; client_app::RemoteStatus status; bool converged{}; double resync_ms{}; }; std::optional prediction(const Files& files, const Profile& p, std::chrono::seconds moving, bool restart) { Bench bench{files}; if (!bench.start()) return std::nullopt; auto made = bench.client(0x5A, p, 4); if (!made) return std::nullopt; auto& host = *made->second; client::Intention intention; const std::vector> one{{&host, &intention}}; if (!bench.pump(one, [&] { return connected(host); }, std::chrono::seconds{10})) return std::nullopt; PredictionResult r; const auto collect = [&] { for (const auto e : host.session()->take_prediction_errors()) r.errors.push_back(e); }; Script script; const auto end = Clock::now() + moving; while (Clock::now() < end) { script.step(intention); if (!bench.pump(one, [] { return false; }, milliseconds{10}) && !host.session()) return std::nullopt; collect(); } // Stop and require convergence on the authority. intention = {}; r.converged = bench.pump(one, [&] { return settled(bench, *host.session()); }, std::chrono::seconds{5}); collect(); if (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; ++bench.epoch; const auto start = Clock::now(); const bool back = bench.pump( one, [&] { const auto s = host.session()->status(); return s.phase == "connected" && s.session != before && s.marker && settled(bench, *host.session()); }, std::chrono::seconds{15}); r.resync_ms = std::chrono::duration(Clock::now() - start).count(); r.converged = r.converged && back; collect(); } r.status = host.session()->status(); return r; } struct RemoteResult { std::vector errors; // Outside the outage. std::vector current; // The same, for markers not shown as stale. std::uint64_t samples{}, extrapolated{}, stale{}, outage_samples{}, outage_stale{}; double delay_sum{}, recovered_ms{-1}; }; std::optional remote(const Files& files, const Profile& p, std::chrono::seconds moving) { Bench bench{files}; if (!bench.start()) return std::nullopt; auto a = bench.client(0x61, p, 2); auto b = bench.client(0x62, p, 2); if (!a || !b) return std::nullopt; auto& ha = *a->second; auto& hb = *b->second; client::Intention ia, ib; const std::vector> both{{&ha, &ia}, {&hb, &ib}}; if (!bench.pump( both, [&] { return connected(ha) && connected(hb) && hb.session()->status().other_markers == 1; }, std::chrono::seconds{10})) return std::nullopt; const auto session_a = ha.session()->status().session; // A's authoritative path by tick (state after the tick). std::map> path; const auto record = [&] { if (const auto* m = bench.marker(session_a)) path[bench.tick] = {static_cast(m->state.x), static_cast(m->state.y)}; }; const auto truth = [&](double t) -> std::optional> { const auto lo = static_cast(std::floor(t)); const auto from = path.find(lo), to = path.find(lo + 1); if (from == path.end() || to == path.end()) return std::nullopt; const auto f = t - static_cast(lo); return std::pair{from->second.first + ((to->second.first - from->second.first) * f), from->second.second + ((to->second.second - from->second.second) * f)}; }; RemoteResult r; Script script; const auto start = Clock::now(); const auto outage_from = start + (moving / 2), outage_to = outage_from + milliseconds{1500}; bool cut = false; const auto end = start + moving; while (Clock::now() < end) { const auto now = Clock::now(); if (!cut && now >= outage_from && now < outage_to) { impair::Impairment dead = impairment(p); dead.loss = 1.0; b->first->set(dead, dead); cut = true; } else if (cut && now >= outage_to) { b->first->set(impairment(p), impairment(p)); cut = false; } script.step(ia); if (!bench.pump( both, [&] { record(); return false; }, milliseconds{10}) && (!ha.session() || !hb.session())) return std::nullopt; const auto s = hb.session()->status(); const auto& markers = hb.session()->markers(); const auto it = std::ranges::find_if(markers, [](const auto& m) { return !m.own; }); if (it == markers.end()) continue; const bool in_outage = Clock::now() >= outage_from && Clock::now() < outage_to; if (in_outage) { ++r.outage_samples; r.outage_stale += it->stale ? 1 : 0; continue; } if (Clock::now() >= outage_to && r.recovered_ms < 0 && !it->stale) r.recovered_ms = std::chrono::duration(Clock::now() - outage_to).count(); const auto t = truth(s.remote_render_tick); if (!t) continue; ++r.samples; r.extrapolated += it->extrapolated ? 1 : 0; r.stale += it->stale ? 1 : 0; r.delay_sum += s.interpolation_delay; const auto dx = (it->x_mm * 1000) - t->first, dy = (it->y_mm * 1000) - t->second; const auto error = static_cast(std::llround(std::hypot(dx, dy))); r.errors.push_back(error); if (!it->stale) r.current.push_back(error); } return r; } void report(const Profile& p, const PredictionResult& r) { const auto exact = static_cast(std::ranges::count(r.errors, std::uint64_t{0})); 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(exact, r.errors.size()), 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); 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); std::fflush(stdout); } void report(const Profile& p, const RemoteResult& r) { std::printf("remote %s rtt=%lld jitter=%lld loss=%.1f samples=%llu " "p50/p95/p99/max=%.1f/%.1f/%.1f/%.1f current_p99/max=%.1f/%.1f extrapolated=%.1f " "stale=%.1f delay=%.2f outage_stale=%.1f recovered_ms=%.0f\n", p.name.c_str(), 2 * static_cast(p.one_way.count()), static_cast(p.jitter.count()), p.loss * 100, static_cast(r.samples), percentile_mm(r.errors, 50), percentile_mm(r.errors, 95), percentile_mm(r.errors, 99), percentile_mm(r.errors, 100), percentile_mm(r.current, 99), percentile_mm(r.current, 100), share(r.extrapolated, r.samples), share(r.stale, r.samples), r.samples == 0 ? 0.0 : r.delay_sum / static_cast(r.samples), share(r.outage_stale, r.outage_samples), r.recovered_ms); std::fflush(stdout); } } // namespace int main(int argc, char** argv) { std::chrono::seconds seconds{20}; std::string only; bool smoke = false, remote_mode = false; for (int i = 1; i < argc; ++i) { const std::string_view a = argv[i]; if (a == "--smoke") smoke = true; else if (a == "--remote") remote_mode = 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] [--remote] " "[--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{remote_mode ? 8 : 6}; } int code = 0; for (const auto& p : profiles) { if (!only.empty() && p.name != only) continue; if (remote_mode) { const auto r = remote(*files, p, seconds); if (!r) { std::printf("remote %s setup-failed\n", p.name.c_str()); code = 1; continue; } report(p, *r); // Bounds: errors within a metre at the 95th percentile, the outage shown stale, // recovery after it. if (smoke && (r->samples == 0 || percentile_mm(r->current, 95) > 1000 || share(r->outage_stale, r->outage_samples) < 50 || r->recovered_ms < 0)) code = 1; continue; } const auto r = prediction(*files, p, 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); 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; }