Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

fix: exclude profiled and traced workers from benchmark timing claims

Cinna Davis8260e6e4e0f0
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 994ba3d..c0d724d 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -37,6 +37,8 @@ jobs:
# pytest suite generates synthetic inputs, so this workflow does not run it.
- name: Compile source without executing experiments
run: python -m compileall -q src scripts
+ - name: Check benchmark instrumentation detection
+ run: python scripts/check_benchmark_instrumentation.py
- name: CLI smoke check
run: dopplergeist --help
- name: Check installed native validator and packaged browser assets
diff --git a/scripts/benchmark_prepared_measurements.py b/scripts/benchmark_prepared_measurements.py
index 1f6720e..2f71b71 100644
--- a/scripts/benchmark_prepared_measurements.py
+++ b/scripts/benchmark_prepared_measurements.py
@@ -45,6 +45,19 @@ def canonical(value):
allow_nan=True, default=json_scalar)
+def timing_instrumentation_active():
+ """Observe profiling/debugging without installing or changing any hooks."""
+ if sys.getprofile() is not None or sys.gettrace() is not None:
+ return True
+ monitoring = getattr(sys, "monitoring", None)
+ if monitoring is None:
+ return False
+ # Python 3.12 cProfile uses monitoring, leaving getprofile() unset. Count
+ # all reserved monitoring slots, including coverage/custom tools, even when
+ # events are temporarily paused or enabled only for individual code objects.
+ return any(monitoring.get_tool(tool_id) is not None for tool_id in range(6))
+
+
def query_tracks(index, points, events, budget, scoring, bounds, time_range, *,
anchor_window=None, clock_partition_query=False):
"""Count actual index traversal while exercising the production overload path."""
@@ -374,6 +387,7 @@ def worker(args):
native_acoustic_before = native_acoustic.successful_calls() if native_acoustic is not None else 0
native_cpa_before = native_cpa.successful_calls() if native_cpa is not None else 0
query_accounting, query_operations, execution_stages = None, None, {}
+ diagnostic = args.instrument_numerics or args.stage_timing or timing_instrumentation_active()
began, cpu = time.perf_counter(), time.process_time()
try:
if args.include_track_query:
@@ -410,6 +424,7 @@ def worker(args):
}
finally:
elapsed, cpu_seconds = time.perf_counter() - began, time.process_time() - cpu
+ diagnostic = diagnostic or timing_instrumentation_active()
matching._fit_ridge_uncached, Track.acoustic = original_fit, original_acoustic
tiled_search.score_candidate = original_score
if callable(native_fit):
@@ -475,7 +490,6 @@ def worker(args):
if query_accounting is not None:
results["track_query_accounting"] = query_accounting
write_new(args.output / "results.json", results)
- diagnostic = args.instrument_numerics or args.stage_timing or sys.getprofile() is not None
summary = {"wall_seconds": elapsed, "cpu_seconds": cpu_seconds,
"load_seconds_excluded": load_seconds, "operations": operations,
"numerical_functions_canonical": not (args.instrument_numerics or args.stage_timing),
diff --git a/scripts/check_benchmark_instrumentation.py b/scripts/check_benchmark_instrumentation.py
new file mode 100644
index 0000000..65a0340
--- /dev/null
+++ b/scripts/check_benchmark_instrumentation.py
@@ -0,0 +1,76 @@
+"""Direct controls for benchmark timing eligibility; no flight data or timings.
+
+Run in an uninstrumented interpreter:
+ python scripts/check_benchmark_instrumentation.py
+"""
+import cProfile
+import json
+import sys
+
+from benchmark_prepared_measurements import timing_instrumentation_active
+
+
+def observed_state():
+ monitoring = getattr(sys, "monitoring", None)
+ tools = (() if monitoring is None else
+ tuple((monitoring.get_tool(tool_id), monitoring.get_events(tool_id))
+ for tool_id in range(6)))
+ return sys.getprofile(), sys.gettrace(), tools
+
+
+def check(expected):
+ before = observed_state()
+ assert timing_instrumentation_active() is expected
+ assert observed_state() == before, "Detection changed an execution hook"
+
+
+def main():
+ check(False)
+ controls = ["ordinary interpreter"]
+
+ profile = cProfile.Profile()
+ try:
+ profile.enable()
+ legacy_detector_missed_cprofile = sys.getprofile() is None
+ check(True)
+ finally:
+ profile.disable()
+ check(False)
+ controls.append("active cProfile")
+
+ def hook(*args):
+ return hook
+
+ for name, getter, setter in (("legacy profile", sys.getprofile, sys.setprofile),
+ ("legacy trace", sys.gettrace, sys.settrace)):
+ original = getter()
+ try:
+ setter(hook)
+ check(True)
+ assert getter() is hook
+ finally:
+ setter(original)
+ check(False)
+ controls.append(name)
+
+ monitoring = getattr(sys, "monitoring", None)
+ if monitoring is not None:
+ for tool_id in range(6):
+ monitoring.use_tool_id(tool_id, "benchmark-instrumentation-control")
+ try:
+ # A reserved slot suffices even with no global events or
+ # callbacks. Detection must not alter that reservation.
+ assert monitoring.get_events(tool_id) == 0
+ check(True)
+ finally:
+ monitoring.free_tool_id(tool_id)
+ check(False)
+ controls.append(f"monitoring tool {tool_id}")
+
+ print(json.dumps({"passed": controls,
+ "legacy_detector_missed_cprofile": legacy_detector_missed_cprofile,
+ "ordinary_timing_eligible_after_controls": True}, indent=2))
+
+
+if __name__ == "__main__":
+ main()