stagetimer.py¶
clew/stagetimer.py
Per-stage wall-clock cost of one build, recorded beside the refresh total.
__init__
None clew.stagetimer.StageTimer.__init__(self)
Start the first segment.
Source · 8 lines
def __init__(self) -> None:
"""@brief Begin timing at construction.
@version 1
"""
self._last = time.perf_counter()
self._stages: list[tuple[str, int]] = []
Activity · 2 steps
Requirements
as_meta
dict[str, str] clew.stagetimer.StageTimer.as_meta(self)
The breakdown as a build_meta section fragment.
Source · 17 lines
def as_meta(self) -> dict[str, str]:
"""EMPTY WHEN NOTHING WAS MARKED, so an unmeasured build stamps no key at all.
`write_build_signature` drops falsy values, so an empty string would vanish
anyway; returning {} says the same thing at the point a reader looks.
@brief Serialize the breakdown for `write_build_signature(refresh=...)`.
@return A one-key mapping, or {}.
@version 1
"""
if not self._stages:
return {}
rendered = STAGE_SEPARATOR.join(
f"{name}{STAGE_ASSIGN}{elapsed}" for name, elapsed in self._stages
)
return {STAGES_KEY: rendered}
Activity · 4 steps
Requirements
mark
None clew.stagetimer.StageTimer.mark(self, str name)
Close the segment that just ran and name it.
Source · 15 lines
def mark(self, name: str) -> None:
"""Elapsed time is measured from the previous `mark` (or from construction),
so the segments partition the timed span with no gaps and no overlaps.
A repeated name is recorded again rather than merged. Two segments that
genuinely ran twice are two facts, and silently summing them would report a
single stage that no single call ever took.
@brief Record the elapsed time since the last mark under `name`.
@version 1
"""
now = time.perf_counter()
self._stages.append((name, int((now - self._last) * 1000)))
self._last = now
Activity · 3 steps
Requirements
parse_stages
tuple[tuple[str, int], ...] clew.stagetimer.parse_stages(str rendered)
Parse a stamped refresh.stages value back into ordered pairs.
Called by none
Tested by
Calls none
Source · 16 lines
def parse_stages(rendered: str) -> tuple[tuple[str, int], ...]:
"""The inverse of `StageTimer.as_meta`, kept beside it so the two cannot drift
into different notions of the separator. A malformed segment is DROPPED rather
than raised on: this reads a stored measurement in a reporting path, and a
breakdown that cannot be shown must not take the status call down with it.
@brief Decode a stored stage breakdown.
@return The decoded pairs.
@version 1
"""
decoded: list[tuple[str, int]] = []
for segment in rendered.split(STAGE_SEPARATOR):
name, _, value = segment.partition(STAGE_ASSIGN)
if name and value.isdigit():
decoded.append((name, int(value)))
return tuple(decoded)1 more lines, not shown
Activity · 6 steps
Requirements
stages
tuple[tuple[str, int], ...] clew.stagetimer.StageTimer.stages(self)
The recorded segments, in execution order.
Source · 8 lines
def stages(self) -> tuple[tuple[str, int], ...]:
"""@brief Read the recorded segments.
@return The (name, milliseconds) pairs in order.
@version 1
"""
return tuple(self._stages)