Files
tidewall/test/net/test_snapshot_budget.gd
T
claudeandClaude Opus 5.5 e535c1f39c N2: Snapshot replication and interest management
NetSnapshot defines the game traffic after the handshake: delta snapshots of
quantized entity records, the client's input packet and a reliable match
notice. SnapshotSender builds each client's snapshot against the newest one
it acknowledged, tells it only about entities near the player it is watching,
and keeps every snapshot under 1,200 bytes, nearest first, with a staleness
bonus so nothing starves. SnapshotReceiver rebuilds the picture, ignores old
and repeated snapshots and asks for a fresh start if it is ever sent a delta
on something it doesn't hold.

NetGame joins the transport to the match: people who connect become
participants, their inputs go through ServerLoop.submit() once per tick, and
snapshots go out 20 times a second. `--server --listen` makes the headless
server take network players and wait in the lobby for the first of them.

Measured by test on a 24-player match start with every player a client, a
tenth of packets lost and late acknowledgements: 2.4 KB/s mean and 2.5 KB/s
for the worst client, packet headers included, against a 40 KB/s limit.

Closes #33

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 08:26:02 -05:00

130 lines
6.3 KiB
GDScript

extends TestCase
## Issue N2's numbers: a whole 24-player match start, every player a client, over a
## link that loses a tenth of its packets and acknowledges late. Measures what each
## client is sent, and checks that nothing outside its interest area is in it.
## Bytes around every snapshot on the wire: IPv4 and UDP headers, and ENet's protocol
## and command headers for an unreliable, unsequenced send.
const PACKET_OVERHEAD_BYTES: int = 28 + 4 + 8
const TICKS: int = 2700
## Snapshots between a snapshot leaving and its acknowledgement arriving.
const ACK_DELAY: int = 2
func test_24_players_stay_under_40_kb_a_second_and_inside_their_interest() -> void:
var loop: ServerLoop = ServerLoop.new(4, 0, Tuning.SOLO_PLAYERS)
var game: NetGame = NetGame.new(loop, NetServer.new())
var flow: Match = loop.match_flow
var sender: SnapshotSender = game.sender
var receivers: Dictionary = {}
var pending: Dictionary = {}
var handles: Array[int] = flow.participants.duplicate()
assert_eq(handles.size(), Tuning.SOLO_PLAYERS)
for handle: int in handles:
sender.add_client(handle)
receivers[handle] = SnapshotReceiver.new()
pending[handle] = []
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = 99
var limit_players: float = Tuning.NET_INTEREST_PLAYERS_METRES + 0.05
var limit_pickups: float = Tuning.NET_INTEREST_PICKUPS_METRES + 0.05
var records_seen: int = 0
var most_entities: int = 0
var biggest: int = 0
var send_usec: int = 0
var rounds: int = 0
# Per client, the bytes of the busiest second (20 snapshots in a row).
var window: Dictionary = {}
var worst_second: int = 0
while loop.ticks < TICKS and flow.phase != Match.RESULTS:
loop.run_tick()
if loop.ticks % Tuning.NET_SNAPSHOT_INTERVAL_TICKS != 0:
continue
rounds += 1
var started: int = Time.get_ticks_usec()
var built: Dictionary = {}
for handle: int in handles:
built[handle] = sender.build(handle, loop.sim, game.viewer_of(handle), loop.ticks, flow.phase)
send_usec += Time.get_ticks_usec() - started
for handle: int in handles:
var bytes: PackedByteArray = built[handle]
var viewer: SimEntity = loop.sim.entities.find(game.viewer_of(handle))
assert_true(bytes.size() <= Tuning.NET_SNAPSHOT_MAX_BYTES)
biggest = maxi(biggest, bytes.size())
var recent: Array = window.get_or_add(handle, [])
recent.append(bytes.size() + PACKET_OVERHEAD_BYTES)
if recent.size() > 20:
recent.pop_front()
var second: int = 0
for size: int in recent:
second += size
worst_second = maxi(worst_second, second)
for entry: Array in NetSnapshot.decode_snapshot(bytes)["records"]:
records_seen += 1
var entity: SimEntity = loop.sim.entities.find(entry[0])
if entity == null or viewer == null:
fail("a record for entity %d, which isn't there" % entry[0])
continue
var away: float = Vector2(entity.position.x - viewer.position.x, entity.position.z - viewer.position.z).length()
var limit: float = limit_pickups if entity.archetype == SimEntity.ARCHETYPE_PICKUP else limit_players
if entity.id != viewer.id and away > limit:
fail("client %d was sent entity %d, %.1f m away (limit %.1f)" % [handle, entity.id, away, limit])
var receiver: SnapshotReceiver = receivers[handle]
if rng.randf() >= 0.1:
receiver.apply(bytes)
most_entities = maxi(most_entities, receiver.entities.size())
var queue: Array = pending[handle]
queue.append(receiver.ack_tick())
if queue.size() > ACK_DELAY:
var ack: int = queue.pop_front()
if rng.randf() >= 0.1:
sender.acknowledge(handle, ack)
assert_true(flow.phase == Match.PLAY or flow.phase == Match.RESULTS, "the run reached play: %s" % flow.phase_name())
var seconds: float = float(rounds) * Tuning.NET_SNAPSHOT_INTERVAL_TICKS / Tuning.TICK_RATE_HZ
var worst_rate: float = 0.0
var total: float = 0.0
var full: int = 0
var deferred: int = 0
for handle: int in handles:
var stats: Dictionary = sender.stats(handle)
var rate: float = (float(stats["bytes"]) + float(stats["snapshots"]) * PACKET_OVERHEAD_BYTES) / seconds
worst_rate = maxf(worst_rate, rate)
total += rate
full += int(stats["full"])
deferred += int(stats["deferred"])
var receiver: SnapshotReceiver = receivers[handle]
assert_eq(receiver.malformed, 0)
assert_eq(receiver.missing_baseline, 0)
print(" %d clients, %.0f s, 10%% loss: mean %.1f KB/s, worst client %.1f KB/s, worst single second %.1f KB, largest snapshot %d bytes" % [handles.size(), seconds, total / handles.size() / 1000.0, worst_rate / 1000.0, worst_second / 1000.0, biggest])
print(" %d records sent, %d full snapshots, %d updates deferred, most entities one client held %d, building all 24 took %.2f ms a round" % [records_seen, full, deferred, most_entities, send_usec / 1000.0 / rounds])
assert_true(worst_rate < 40_000.0, "worst client averaged %.0f bytes a second" % worst_rate)
assert_true(worst_second < 40_000, "worst single second was %d bytes" % worst_second)
assert_true(records_seen > 10_000, "the match really was replicated: %d records" % records_seen)
# Snapshots build on nothing until the first acknowledgement arrives, and again if
# that first one was lost. After that, loss never forces another.
assert_true(full <= handles.size() * (ACK_DELAY + 3), "%d full snapshots" % full)
# With the link quiet again, every client ends with exactly what it may see.
for _round: int in 12:
for _tick: int in Tuning.NET_SNAPSHOT_INTERVAL_TICKS:
loop.run_tick()
if flow.phase == Match.RESULTS:
break
for handle: int in handles:
var receiver: SnapshotReceiver = receivers[handle]
receiver.apply(sender.build(handle, loop.sim, game.viewer_of(handle), loop.ticks, flow.phase))
sender.acknowledge(handle, receiver.ack_tick())
var checked: int = 0
for handle: int in handles:
var receiver: SnapshotReceiver = receivers[handle]
var viewer: SimEntity = loop.sim.entities.find(game.viewer_of(handle))
var visible: Array[int] = SnapshotSender.interest(loop.sim, viewer)
assert_eq(receiver.entities.size(), visible.size(), "client %d's entity count" % handle)
for entity_id: int in visible:
if receiver.entities.has(entity_id):
checked += 1
assert_eq(receiver.entities[entity_id], NetSnapshot.record_of(loop.sim.entities.find(entity_id)))
else:
fail("client %d is missing entity %d" % [handle, entity_id])
assert_true(checked > handles.size())