29 lines
1.1 KiB
Python
29 lines
1.1 KiB
Python
"""Bounded stage-cost evidence; no scheduling or motion authority."""
|
|
from collections import defaultdict, deque
|
|
import math
|
|
|
|
|
|
class LoopProfile:
|
|
def __init__(self):
|
|
self.samples = defaultdict(lambda: deque(maxlen=600))
|
|
self.last_report = None
|
|
|
|
def add(self, stage, seconds):
|
|
if math.isfinite(seconds) and seconds >= 0:
|
|
self.samples[stage].append(seconds*1000)
|
|
|
|
def due(self, now):
|
|
return self.last_report is None or now-self.last_report >= 5
|
|
|
|
def report(self, now):
|
|
self.last_report = now
|
|
result = {}
|
|
for stage, values in self.samples.items():
|
|
values = sorted(values)
|
|
if values:
|
|
result[stage] = dict(samples=len(values), mean_ms=sum(values)/len(values),
|
|
p50_ms=values[len(values)//2], p95_ms=values[math.ceil(.95*len(values))-1],
|
|
max_ms=values[-1])
|
|
return dict(schema_version='missioncore.loop-profile/v1', monotonic=now,
|
|
scope='rolling stage costs; app update includes physics callbacks', stages=result)
|