feat: record execution timings and speed up batch queues

This commit is contained in:
阿白叔 committed 2026-10-15 09:10:26 +08:00
1 parent 20d8b81a34
commit 0225787084
12 files changed
+509 -56

No files matched your search

+6
View File
@@ -386,6 +386,12 @@ def test_thousand_item_multisheet_mixed_queue_has_bounded_runtime_and_memory(tmp
total = project_count * sheet_count * rule_count
saved = len(range(0, total, 11))
(tmp_path / 'stress-metrics.json').write_text(json.dumps({
'mode': 'simulated_worker_with_tracemalloc', 'projects': project_count,
'sheets_per_project': sheet_count, 'rules_per_sheet': rule_count,
'steps': total, 'saved_steps': saved, 'elapsed_seconds': elapsed,
'peak_bytes': peak, 'steps_per_second': total / elapsed,
}, indent=2), encoding='utf-8')
assert result.progress.queue_complete
assert result.progress.total_items == total
assert result.progress.saved_items == saved
+102
View File
@@ -0,0 +1,102 @@
import json
import os
import subprocess
import pytest
from dx_productivity.trial_task_center import worker_monitor
from dx_productivity.trial_task_center.logging_setup import configure_logging
def test_phase_timing_records_completed_units_and_failure_without_success(tmp_path, monkeypatch):
from dx_productivity.trial_task_center import execution_metrics
clock = iter((10.0, 12.0, 20.0, 23.0))
monkeypatch.setattr(execution_metrics.time, 'perf_counter', lambda: next(clock))
logging = configure_logging(tmp_path)
with execution_metrics.measure_phase(logging, 'task', 'execute_step', unit='step') as metric:
metric['completed_units'] = 1
metric['state'] = 'saved_ok'
with pytest.raises(RuntimeError, match='worker failed'):
with execution_metrics.measure_phase(logging, 'task', 'execute_step', unit='step'):
raise RuntimeError('worker failed')
events = [json.loads(line) for line in (tmp_path / 'tasks/task/events.jsonl').read_text().splitlines()]
assert events[0]['details']['elapsed_seconds'] == 2
assert events[0]['details']['units_per_minute'] == 30
assert events[0]['details']['state'] == 'saved_ok'
assert events[1]['details']['elapsed_seconds'] == 3
assert events[1]['details']['state'] == 'error'
assert events[1]['details']['completed_units'] == 0
def test_worker_stages_include_repeated_stages_without_double_counting():
watch = worker_monitor.ProgressWatch('token', 'job', 150, 0)
def frame(n, stage):
return f'DX_PROGRESS\ttoken\tjob\t{n}\t{stage}\t0\t1\t1\n'.encode()
assert watch.accept(frame(1, 'capture'), 2)
assert watch.accept(frame(2, 'native_replace'), 5)
assert not watch.accept(frame(2, 'ignored'), 100)
assert watch.accept(frame(3, 'capture'), 9)
snapshot = watch.snapshot(now=12)
assert snapshot['elapsed_seconds'] == 12
assert snapshot['stage_seconds'] == {'starting': 2, 'capture': 6, 'native_replace': 4}
assert sum(snapshot['stage_seconds'].values()) == snapshot['elapsed_seconds']
assert watch.snapshot(now=12) == snapshot
@pytest.mark.skipif(os.name != 'nt', reason='Windows Script Host path compatibility')
def test_long_evidence_path_remains_visible_to_real_vbs(tmp_path):
from dx_productivity.trial_task_center.quick_run import _vbs_worker_paths, _resolve_cscript
root = tmp_path / ('evidence-' + 'x' * 80) / ('discovery-' + 'y' * 60)
root.mkdir(parents=True)
script = root / 'QuickPartReplace.vbs'
script.write_text("Set f=CreateObject(\"Scripting.FileSystemObject\")\n"
"WScript.Echo CStr(f.FileExists(f.BuildPath(WScript.Arguments(0), WScript.Arguments(1))))\n")
marker = 'monitor-' + 'a' * 32 + '.ready'
(root / marker).write_text('token')
assert len(str(root / marker)) > 259
script_arg, root_arg = _vbs_worker_paths(script, root)
result = subprocess.run((str(_resolve_cscript()), '//nologo', script_arg, root_arg, marker),
capture_output=True, text=True, timeout=10)
assert result.returncode == 0 and result.stdout.strip() == 'True'
def test_external_status_append_blocks_next_worker_before_cad_write(tmp_path):
from pathlib import Path
from threading import Event
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.errors import TaskCenterError
root = tmp_path / 'projects'; root.mkdir()
(root / 'board.prj').write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
data = tmp_path / 'data'; calls = []
def worker(command, timeout):
calls.append(command[-1])
with (Path(command[3]) / 'status.tsv').open('a', encoding='utf-8') as stream:
stream.write(f'{command[-1]}\tno_match\tbatch_closed\tnot found\n')
return subprocess.CompletedProcess(command, 0, '', '')
store = TaskStore(tmp_path / 'tasks.db')
preparer = QuickRunPreparer(data, process_runner=worker, cscript_resolver=lambda: Path('cscript'),
sheet_discoverer=lambda _: (('MAIN', '1'),))
service = TaskCenterService(store, configure_logging(data), quick_run_preparer=preparer)
task = service.create_project_tasks(root)[0]
service.add_rule(task.task_id, 'A', 'B'); service.add_rule(task.task_id, 'C', 'D')
child = service.for_project_task(task.task_id)._quick_run_preparer
original = child.execute_step
def inject(*args, **kwargs):
result = original(*args, **kwargs)
if len(calls) == 1:
with result.status_path.open('a', encoding='utf-8') as stream:
stream.write('foreign_job\tno_match\tbatch_closed\tchanged externally\n')
return result
child.execute_step = inject
progress = service.quick_run_progress(task.task_id)
with pytest.raises(TaskCenterError):
service.execute_quick_run_queue(task.task_id,
expected_item=(str(progress.next_project_path), 'A', 'B', 'preserve'), stop_event=Event())
assert len(calls) == 1
assert (child._data_root / 'quick-run/uncertain.flag').exists()
store.close()
@@ -0,0 +1,17 @@
import importlib.util
from pathlib import Path
import pytest
@pytest.mark.parametrize('behavior', ['success', 'failure_continue', 'uncertain_stop', 'pause_resume'])
def test_multiple_projects_preserve_task_order_scope_results_and_measurements(tmp_path, behavior):
path = Path(__file__).resolve().parents[2] / 'tools/measure_task_queue.py'
spec = importlib.util.spec_from_file_location('measure_task_queue', path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
report = module.run_scenario(tmp_path / behavior, projects=3, sheets=2, rules=2, behavior=behavior)
assert report['checks'] == 'passed'
timings = report['timings']
assert len([row for row in timings if row['phase'] == 'execute_step']) == report['executed_steps']
assert all(row['elapsed_seconds'] >= 0 for row in timings)