123 lines
5.1 KiB
Python
123 lines
5.1 KiB
Python
"""Measured run durations, not estimates derived from wall-clock log timestamps."""
|
|
import math
|
|
import time
|
|
|
|
ACTION_KEYS = {
|
|
'ForwardAnnotate': 'forward_annotation', 'SynchCES': 'synch_ces',
|
|
'LoadCES': 'load_ces', 'BackAnnotate': 'back_annotation',
|
|
}
|
|
|
|
|
|
def parse_save_progress(text):
|
|
markers, durations = [], {}
|
|
for line in text.splitlines():
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
if not line.startswith('TIMING|'):
|
|
markers.append(line)
|
|
continue
|
|
parts = line.split('|')
|
|
if len(parts) != 3:
|
|
continue
|
|
try:
|
|
seconds = float(parts[2])
|
|
except ValueError:
|
|
continue
|
|
if math.isfinite(seconds) and seconds >= 0:
|
|
durations[parts[1]] = seconds
|
|
return {'markers': markers, 'timings_seconds': durations}
|
|
|
|
|
|
class RunTimings:
|
|
def __init__(self, *, clock=None):
|
|
self.clock = clock or time.monotonic
|
|
self.started = self.clock()
|
|
self.elapsed = {}
|
|
self.running = {}
|
|
self._action_totals = {}
|
|
self._save_markers = set()
|
|
|
|
def begin(self, name, at=None):
|
|
self.running.setdefault(name, self.clock() if at is None else at)
|
|
|
|
def end(self, name, at=None):
|
|
started = self.running.pop(name, None)
|
|
if started is not None:
|
|
self.elapsed[name] = self.elapsed.get(name, 0.0) + max(
|
|
0.0, (self.clock() if at is None else at) - started)
|
|
|
|
def record(self, event):
|
|
now = event.get('monotonic_seconds')
|
|
if now is None:
|
|
now = self.clock()
|
|
stage = event.get('stage')
|
|
if stage in ('lm_launch', 'pcb_launch'):
|
|
self.end('worker_setup', now)
|
|
self.begin('pcb_startup', now)
|
|
elif stage == 'identity_verified':
|
|
self.end('pcb_startup', now)
|
|
self.begin('integration', now)
|
|
elif stage == 'diagnostic_native_loaded_candidate':
|
|
self.end('pcb_startup', now)
|
|
self.begin('diagnostic_close', now)
|
|
elif stage == 'diagnostic_complete':
|
|
self.end('diagnostic_close', now)
|
|
elif stage == 'integration_action':
|
|
key = ACTION_KEYS.get(event.get('action'))
|
|
if key:
|
|
self.begin(key, now)
|
|
elif stage == 'integration_action_complete':
|
|
key = ACTION_KEYS.get(event.get('action'))
|
|
if key:
|
|
self.end(key, now)
|
|
# These are measured inside the worker and include COM blocking,
|
|
# independent of delayed supervisor polling or disk writes.
|
|
if 'elapsed_seconds' in event:
|
|
# Replace the just-observed span with the worker's duration.
|
|
# Keep repeat actions as separate accumulated work.
|
|
prior = self._action_totals.get(key, 0.0)
|
|
self._action_totals[key] = prior + float(event['elapsed_seconds'])
|
|
self.elapsed[key] = self._action_totals[key]
|
|
if 'call_elapsed_seconds' in event:
|
|
call = key + '_call'
|
|
self.elapsed[call] = self.elapsed.get(call, 0.0) + float(event['call_elapsed_seconds'])
|
|
elif stage == 'integration_complete':
|
|
self.end('integration', now)
|
|
elif stage == 'save_start':
|
|
self.begin('save_close', now)
|
|
elif stage == 'save_progress':
|
|
boundaries = {
|
|
'VALIDATE_START': ('begin', 'validate'), 'LICENSE_OK': ('end', 'validate'),
|
|
'SAVE_START': ('begin', 'save'), 'SAVE_OK': ('end', 'save'),
|
|
'POST_SAVE_CHECK': ('begin', 'post_save_check'),
|
|
'POST_SAVE_SYNCHRONIZED': ('end', 'post_save_check'),
|
|
'CLOSE_START': ('begin', 'close'), 'CLOSE_OK': ('end', 'close'),
|
|
}
|
|
for marker in event.get('markers', ()):
|
|
if marker in self._save_markers:
|
|
continue
|
|
self._save_markers.add(marker)
|
|
if marker in boundaries:
|
|
operation, name = boundaries[marker]
|
|
getattr(self, operation)(name, now)
|
|
measured = event.get('timings_seconds', {})
|
|
for name in measured:
|
|
# A helper duration proves the COM call returned, even if its
|
|
# Boolean result was false and no success marker followed.
|
|
self.running.pop(name, None)
|
|
self.elapsed.update(measured)
|
|
elif stage == 'saved_and_closed':
|
|
self.end('save_close', now)
|
|
self.begin('pcb_exit', now)
|
|
elif stage == 'complete':
|
|
self.end('pcb_exit', now)
|
|
|
|
def snapshot(self):
|
|
now = self.clock()
|
|
values = dict(self.elapsed)
|
|
for name, started in self.running.items():
|
|
values[name] = values.get(name, 0.0) + max(0.0, now - started)
|
|
values['total'] = max(0.0, now - self.started)
|
|
return {key: round(value, 6) for key, value in values.items()}, tuple(self.running)
|