Files
tidewall/test/server/test_tick_times.gd
claudeandClaude Opus 5.5 f87d5eae0d H4: Long-run stability (make stability), and two memory leaks fixed
make stability (tools/stability.sh, StabilityReport) runs one dedicated
server for 50 bot matches back to back, unthrottled, sampling its resident
memory, and fails on a crash, hang, engine or script error, a match with
no winner, or memory that grows after the first five matches.

The first run had no crash, hang or error, but showed two leaks:
- ServerLoop kept every tick's duration in an array (8 bytes a tick,
  about 21 MB a day at 30 Hz) and copied and sorted all of it for every
  progress line. TickTimes keeps exact statistics in fixed memory: a
  one-microsecond histogram up to 65 ms, plus the rare slower ticks.
- Resident memory grew about 28 MB a match (905 MB after 42 matches):
  glibc gives each thread its own malloc arena, and every next island is
  built on a worker thread. The server launchers (packaging/server,
  packaging/dist, so also the .deb, Docker image and systemd unit) and
  tools/godot.sh for --server now set MALLOC_ARENA_MAX=1 unless already
  set. Tick times were unchanged (p50 501 vs 507 us, p99 1635 vs 1625).

After: 50 matches (seed 4242) in 756 s, all with a winner, exit 0, no
errors; static memory +1.6 KiB and resident +145 KiB a match, flat at
204 MB from match 26 on. No match crashed, so there is no crash seed to
turn into a regression test.

Tests: TickTimes against a sorted list, its fixed memory, the launchers'
setting, and StabilityReport. make check, dist-check and
server-package-check pass.

Closes #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 07:15:23 -05:00

41 lines
1.8 KiB
GDScript

extends TestCase
## H4: tick timing in fixed memory, and the server's launchers keeping the allocator to
## one arena so resident memory doesn't grow match after match.
func test_statistics_match_a_sorted_list() -> void:
var times: TickTimes = TickTimes.new()
var values: PackedInt64Array = PackedInt64Array()
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
rng.seed = 4
for index: int in 5000:
var usec: int = rng.randi_range(200, 3000) if index % 500 != 0 else rng.randi_range(70000, 2000000)
times.add(usec)
values.append(usec)
values.sort()
for fraction: float in [0.0, 0.01, 0.5, 0.95, 0.99, 0.999, 1.0]:
assert_eq(times.percentile(fraction), ServerLoop._percentile(values, fraction), "percentile %.3f" % fraction)
var stats: Dictionary = times.stats(ServerLoop.TICK_BUDGET_USEC)
assert_eq(stats["ticks"], 5000)
assert_eq(stats["min_us"], values[0])
assert_eq(stats["max_us"], values[-1])
assert_eq(stats["over_budget"], 10, "the ten slow ticks")
assert_eq(TickTimes.new().stats(1000), {"ticks": 0})
func test_memory_stays_fixed_past_the_slow_tick_cap() -> void:
var times: TickTimes = TickTimes.new()
for index: int in TickTimes.MAX_SLOW + 100:
times.add(TickTimes.HISTOGRAM_USEC + index)
assert_eq(times._slow.size(), TickTimes.MAX_SLOW)
assert_eq(times.size(), TickTimes.MAX_SLOW + 100)
assert_eq(times.percentile(1.0), TickTimes.HISTOGRAM_USEC + TickTimes.MAX_SLOW + 99, "the slowest are kept")
assert_eq(times.over(12000), TickTimes.MAX_SLOW + 100)
func test_server_launchers_use_one_malloc_arena() -> void:
for path: String in ["res://packaging/server/tidewall-server", "res://packaging/dist/tidewall-server", "res://tools/godot.sh"]:
var text: String = FileAccess.get_file_as_string(path)
assert_true(text.contains('export MALLOC_ARENA_MAX="${MALLOC_ARENA_MAX:-1}"'), "%s sets MALLOC_ARENA_MAX" % path)