diff --git a/tests/test_perf_bench.py b/tests/test_perf_bench.py index 17cb6b41a..4729c5e20 100644 --- a/tests/test_perf_bench.py +++ b/tests/test_perf_bench.py @@ -45,3 +45,44 @@ class PerfBenchStatsTests(unittest.TestCase): if __name__ == "__main__": unittest.main() + + +class WaitLoadedTests(unittest.TestCase): + """wait_loaded on a scripted game with a fake clock: one status per 50 ms poll.""" + + def run_script(self, statuses): + import tools.perf_bench as pb + clock = [0.0] + it = iter(statuses) + + def control(_command, timeout=None): + return next(it) + + real_time, real_sleep = pb.time.time, pb.time.sleep + pb.time.time = lambda: clock[0] + pb.time.sleep = lambda s: clock.__setitem__(0, clock[0] + s) + try: + return pb.wait_loaded(control, lambda p: True, 60) + finally: + pb.time.time, pb.time.sleep = real_time, real_sleep + + @staticmethod + def status(chunks, meshed, stages=None): + s = {"player": {"position": [0, 0, 0]}, "pending_chunks": 0, "screen": "game", + "chunks": chunks, "generated_chunks": chunks, "meshed_sections": meshed} + if stages is not None: + s["streaming"] = {"queued": 0, "generating": 0, "protos": 296, "decorating": 0, + "finishing": 0, "lighting": 0, "meshing": stages} + return s + + def test_stages_end_the_load_before_the_remesh_tail(self): + # 20 polls (1 s) of meshing work, then idle stages while blocks keep remeshing + # at 400 meshes/s, with a short burst of work every 2 s. + loading = [self.status(900, 1000 * i, stages=500) for i in range(20)] + tail = [self.status(901, 20000 + 20 * i, stages=40 if i % 40 == 0 else 0) for i in range(400)] + self.assertAlmostEqual(self.run_script(loading + tail), 1.0, places=6) + + def test_old_builds_wait_for_meshing_to_go_quiet(self): + busy = [self.status(901, 1000 * i) for i in range(20)] + quiet = [self.status(901, 20000 + i) for i in range(40)] + self.assertAlmostEqual(self.run_script(busy + quiet), 1.0, places=6) diff --git a/tools/perf_bench.py b/tools/perf_bench.py index 136e607d5..96fb7c8e7 100644 --- a/tools/perf_bench.py +++ b/tools/perf_bench.py @@ -272,12 +272,14 @@ QUIET_MESHES_PER_S = 100 def wait_loaded(control, near, limit): """Waits until the player is spawned (near(position)), nothing is queued, the resident and generated chunk counts have not changed for a second, as the - vanilla driver does, and meshing has gone quiet (fewer than - QUIET_MESHES_PER_S section meshes in a second: the last sections drawn); - returns when the last of these happened. (pending_chunks alone misses work - the threads have already taken, and sections waiting for their neighbours' - light; blocks keep being remeshed as they change, so meshes can't be - required to stop.)""" + vanilla driver does, and the last sections are drawn; returns when the last + of these happened. Builds that report `status.streaming` are drawn once no + stage (generation, decoration, light, meshing) has held work for half a + second; blocks that change after loading (fluids settling, random ticks) + only remesh in short bursts. Older builds fall back to meshing going quiet + (fewer than QUIET_MESHES_PER_S section meshes in a second), which also + waits through that tail. (pending_chunks alone misses work the threads have + already taken, and sections waiting for their neighbours' light.)""" deadline = time.time() + limit last, changed, previous, meshing = None, time.time(), None, time.time() while time.time() < deadline: @@ -293,11 +295,19 @@ def wait_loaded(control, near, limit): counts = (s["chunks"], s["generated_chunks"]) if counts != last: last, changed = counts, now + stages = s.get("streaming") sample = (now, s["meshed_sections"]) - if previous and sample[1] - previous[1] > QUIET_MESHES_PER_S * (now - previous[0]): - meshing = now + if stages is not None: + # Protos outside the view distance wait for neighbours that are never generated. + if any(v for k, v in stages.items() if k != "protos"): + meshing = now + quiet = 0.5 + else: + if previous and sample[1] - previous[1] > QUIET_MESHES_PER_S * (now - previous[0]): + meshing = now + quiet = 1.0 previous = sample - if now - max(changed, meshing) >= 1.0: + if now - changed >= 1.0 and now - meshing >= quiet: return max(changed, meshing) time.sleep(0.05) raise TimeoutError(f"not loaded after {limit} s")