Timer overhaul: update tests

This commit is contained in:
John McCardle 2026-01-03 22:44:53 -05:00
commit cec76b63dc
78 changed files with 521 additions and 495 deletions

View file

@ -97,7 +97,7 @@ def handle_key(key, state):
benchmark.on_key = handle_key
# Update entity positions
def update_entities(ms):
def update_entities(timer, ms):
dt = ms / 1000.0 # Convert to seconds
for entity in entities:
@ -119,13 +119,13 @@ def update_entities(ms):
entity.y = new_y
# Run movement update every frame (16ms)
mcrfpy.setTimer("movement", update_entities, 16)
movement_timer = mcrfpy.Timer("movement", update_entities, 16)
# Benchmark statistics
frame_count = 0
start_time = None
def benchmark_timer(ms):
def benchmark_callback(timer, ms):
global frame_count, start_time
if start_time is None:
@ -152,4 +152,4 @@ def benchmark_timer(ms):
print("=" * 60)
# Don't exit - let user review
mcrfpy.setTimer("benchmark", benchmark_timer, 100)
benchmark_timer = mcrfpy.Timer("benchmark", benchmark_callback, 100)

View file

@ -31,7 +31,7 @@ frame_count = 0
metrics_samples = []
def collect_metrics(runtime):
def collect_metrics(timer, runtime):
"""Timer callback to collect metrics each frame."""
global frame_count, metrics_samples
@ -65,9 +65,9 @@ def collect_metrics(runtime):
def finish_scenario():
"""Calculate statistics and store results for current scenario."""
global results, current_scenario, metrics_samples
global results, current_scenario, metrics_samples, benchmark_timer
mcrfpy.delTimer("benchmark_collect")
benchmark_timer.stop()
if not metrics_samples:
print(f" WARNING: No samples collected for {current_scenario}")
@ -149,7 +149,8 @@ def run_next_scenario():
scenarios[next_idx][1]()
# Start collection timer (runs every frame)
mcrfpy.setTimer("benchmark_collect", collect_metrics, 1)
global benchmark_timer
benchmark_timer = mcrfpy.Timer("benchmark_collect", collect_metrics, 1)
# ============================================================================

View file

@ -427,7 +427,7 @@ def print_analysis():
print(f" Note: This overhead is acceptable given query speedups")
def run_benchmarks(runtime=None):
def run_benchmarks(timer=None, runtime=None):
"""Main benchmark runner."""
global results
@ -458,4 +458,4 @@ if __name__ == "__main__":
if "--headless" in sys.argv or True: # Always run immediately for benchmarks
run_benchmarks()
else:
mcrfpy.setTimer("run_bench", run_benchmarks, 100)
bench_timer = mcrfpy.Timer("run_bench", run_benchmarks, 100, once=True)

View file

@ -34,7 +34,7 @@ frame_count = 0
test_results = {} # Store filenames for each test
def run_test_phase(runtime):
def run_test_phase(timer, runtime):
"""Run through warmup and measurement phases."""
global frame_count
@ -51,7 +51,7 @@ def run_test_phase(runtime):
test_results[current_test] = filename
print(f" {current_test}: saved to {filename}")
mcrfpy.delTimer("test_phase")
timer.stop()
run_next_test()
@ -90,7 +90,8 @@ def run_next_test():
print(f"\n[{next_idx + 1}/{len(tests)}] Running: {current_test}")
tests[next_idx][1]()
mcrfpy.setTimer("test_phase", run_test_phase, 1)
global test_phase_timer
test_phase_timer = mcrfpy.Timer("test_phase", run_test_phase, 1)
# ============================================================================
@ -130,14 +131,15 @@ def setup_base_layer_modified():
# Timer to modify one cell per frame (triggers dirty flag each frame)
mod_counter = [0]
def modify_cell(runtime):
def modify_cell(timer, runtime):
x = mod_counter[0] % GRID_SIZE
y = (mod_counter[0] // GRID_SIZE) % GRID_SIZE
layer.set(x, y, mcrfpy.Color(255, 0, 0, 255))
mod_counter[0] += 1
test_base_mod.activate()
mcrfpy.setTimer("modify", modify_cell, 1)
global modify_timer
modify_timer = mcrfpy.Timer("modify", modify_cell, 1)
def setup_color_layer_static():
@ -170,14 +172,15 @@ def setup_color_layer_modified():
# Timer to modify one cell per frame - triggers re-render
mod_counter = [0]
def modify_cell(runtime):
def modify_cell(timer, runtime):
x = mod_counter[0] % GRID_SIZE
y = (mod_counter[0] // GRID_SIZE) % GRID_SIZE
layer.set(x, y, mcrfpy.Color(255, 0, 0, 255))
mod_counter[0] += 1
test_color_mod.activate()
mcrfpy.setTimer("modify", modify_cell, 1)
global modify_timer
modify_timer = mcrfpy.Timer("modify", modify_cell, 1)
def setup_tile_layer_static():
@ -222,7 +225,7 @@ def setup_tile_layer_modified():
# Timer to modify one cell per frame
mod_counter = [0]
def modify_cell(runtime):
def modify_cell(timer, runtime):
if layer:
x = mod_counter[0] % GRID_SIZE
y = (mod_counter[0] // GRID_SIZE) % GRID_SIZE
@ -230,7 +233,8 @@ def setup_tile_layer_modified():
mod_counter[0] += 1
test_tile_mod.activate()
mcrfpy.setTimer("modify", modify_cell, 1)
global modify_timer
modify_timer = mcrfpy.Timer("modify", modify_cell, 1)
def setup_multi_layer_static():

View file

@ -31,7 +31,7 @@ class StressTestRunner:
def add_test(self, name, setup_fn, description=""):
self.tests.append({'name': name, 'setup': setup_fn, 'description': description})
def tick(self, runtime):
def tick(self, timer, runtime):
"""Single timer callback that manages all test flow"""
self.frames_counted += 1
@ -103,7 +103,7 @@ class StressTestRunner:
self.results[test['name']] = {'error': str(e)}
def finish_suite(self):
mcrfpy.delTimer("tick")
self.tick_timer.stop()
print("\n" + "="*50)
print("STRESS TEST COMPLETE")
@ -137,7 +137,7 @@ class StressTestRunner:
ui = init.children
ui.append(mcrfpy.Frame(pos=(0,0), size=(10,10))) # Required for timer to fire
init.activate()
mcrfpy.setTimer("tick", self.tick, TIMER_INTERVAL_MS)
self.tick_timer = mcrfpy.Timer("tick", self.tick, TIMER_INTERVAL_MS)
# =============================================================================

View file

@ -6,7 +6,7 @@ import mcrfpy
import sys
import time
def run_test(runtime):
def run_test(timer, runtime):
print("=" * 60)
print("FOV Isolation Test - Is TCOD slow, or is it the Python wrapper?")
print("=" * 60)
@ -96,4 +96,4 @@ def run_test(runtime):
init = mcrfpy.Scene("init")
init.activate()
mcrfpy.setTimer("test", run_test, 100)
test_timer = mcrfpy.Timer("test", run_test, 100, once=True)

View file

@ -134,7 +134,7 @@ init = mcrfpy.Scene("init")
init.activate()
# Use a timer to let the engine initialize
def run_benchmark(runtime):
def run_benchmark(timer, runtime):
main()
mcrfpy.setTimer("bench", run_benchmark, 100)
bench_timer = mcrfpy.Timer("bench", run_benchmark, 100, once=True)