Module

_timing

Timing utilities and Server-Timing header builder.

Provides monotonic clock helpers for request-level timing and a builder for the Server-Timing HTTP header (RFC 6797 extension).

All functions use time.monotonic_ns() for high-resolution, monotonic timing that is not affected by system clock adjustments.

Example:

>>> start = monotonic_ns()
>>> # ... do work ...
>>> dur = elapsed_ms(start)
>>> header = ServerTiming()
>>> header.add("app", dur)
>>> header.render()
'app;dur=12.3'

Classes

ServerTiming 4
Accumulates Server-Timing metrics for a single request. Collects named duration measurements and r…

Accumulates Server-Timing metrics for a single request.

Collects named duration measurements and renders them as a Server-Timing header value.

Attributes

Name Type Description
_entries list[tuple[str, float]]

Methods

add 2
Record a named timing measurement.
def add(self, name: str, duration_ms: float) -> None
Parameters
Name Type Description
name

Metric name (e.g., "parse", "app", "encode").

duration_ms

Duration in milliseconds.

render 0 str
Render as a Server-Timing header value.
def render(self) -> str
Returns
str Header value string, e.g. 'parse;dur=0.3, app;dur=12.1'. Empty string if no entries have been added.
render_bytes 0 bytes
Render as bytes for direct header insertion.
def render_bytes(self) -> bytes
Returns
bytes UTF-8 encoded header value.

Functions

monotonic_ns 0 int
Return the current monotonic clock value in nanoseconds.
def monotonic_ns() -> int
Returns
int
elapsed_ms 1 float
Compute elapsed time in milliseconds since start_ns.
def elapsed_ms(start_ns: int) -> float
Parameters
Name Type Description
start_ns int

Start time from monotonic_ns().

Returns
float