Instrumentation¶
Data collection, probes, latency/throughput trackers, and simulation summaries.
Instrumentation, tracing, and measurement components.
LatencyTracker ¶
Bases: Entity
Records end-to-end latency from event context['created_at'].
Drop-in replacement for the custom LatencyTrackingSink that every example reimplements. Uses the 'created_at' field that Event.post_init sets automatically.
Stores (completion_time_s, latency_s) pairs in self.data.
Note: Memory usage grows linearly with the number of events processed (~72 bytes per event). For simulations with millions of events, consider using TDigest for approximate percentiles or periodic aggregation to bound memory usage.
ThroughputTracker ¶
BucketedData ¶
Time-windowed aggregation result from Data.bucket().
to_dict ¶
Return dict with keys: time_s, mean, p50, p99, max, count, sum.
Data ¶
Container for timestamped metric samples with analysis utilities.
Stores (time, value) pairs for post-simulation analysis. Values should be numeric (int or float) for aggregation methods to work.
Samples are stored in append order. For time-ordered data, ensure add_stat is called with non-decreasing times.
values
property
¶
All recorded samples as (time_seconds, value) tuples.
add_stat ¶
Record a data point at the given simulation time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The metric value to record. |
required |
time
|
Instant
|
The simulation time of this sample. |
required |
between ¶
Return a new Data with samples in [start, end).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_s
|
float
|
Start time in seconds (inclusive). |
required |
end_s
|
float
|
End time in seconds (exclusive). |
required |
percentile ¶
Calculate percentile from sample values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
Percentile in [0, 1]. E.g., 0.99 for p99. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Interpolated percentile value, or 0.0 if empty. |
std ¶
Population standard deviation of sample values. Returns 0.0 if fewer than 2 samples.
bucket ¶
Group samples into fixed-width time windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_s
|
float
|
Width of each bucket in seconds. |
1.0
|
Returns:
| Type | Description |
|---|---|
BucketedData
|
BucketedData with per-bucket aggregations. |
rate ¶
Compute rate of change (count per window) over time windows.
Useful for throughput data where each sample represents one event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_s
|
float
|
Width of each time window in seconds. |
1.0
|
Returns:
| Type | Description |
|---|---|
Data
|
New Data with (bucket_start, count/window_s) pairs. |
Probe ¶
Probe(
target: Entity,
metric: str,
data: Data,
interval: float = 1.0,
start_time: Instant | None = None,
)
Bases: Source
Periodic metric sampler for monitoring entity state over time.
Extends Source to poll a metric from a target entity at fixed intervals. The sampled values are stored in a Data container for post-simulation analysis or visualization.
Probes run as daemon events, meaning they do not prevent the simulation from auto-terminating when all primary events are processed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Entity
|
The entity to measure. |
required |
metric
|
str
|
Attribute or property name to sample (accessed via getattr). |
required |
data
|
Data
|
Data container to store samples. |
required |
interval
|
float
|
Seconds between measurements. Defaults to 1.0. |
1.0
|
start_time
|
Instant | None
|
When to begin probing. Defaults to Instant.Epoch. |
None
|
on
classmethod
¶
Create a Probe and its Data container in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Entity
|
The entity to measure. |
required |
metric
|
str
|
Attribute or property name to sample. |
required |
interval
|
float
|
Seconds between measurements. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
Probe
|
(probe, data) tuple. Pass probe to Simulation(probes=[...]), |
Data
|
use data for post-simulation analysis. |
on_many
classmethod
¶
on_many(
target: Entity,
metrics: list[str],
interval: float = 1.0,
) -> tuple[list[Probe], dict[str, Data]]
Create Probes for multiple metrics on the same target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Entity
|
The entity to measure. |
required |
metrics
|
list[str]
|
List of attribute/property names to sample. |
required |
interval
|
float
|
Seconds between measurements. Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
tuple[list[Probe], dict[str, Data]]
|
(probes_list, data_dict) where data_dict is keyed by metric name. |
InMemoryTraceRecorder
dataclass
¶
Trace recorder that stores spans in memory.
Useful for debugging, testing, and post-simulation analysis. Provides filtering methods to query specific span types or events.
Attributes:
| Name | Type | Description |
|---|---|---|
spans |
list[dict[str, Any]]
|
List of recorded spans as dictionaries. |
NullTraceRecorder
dataclass
¶
No-op recorder that discards all traces.
Use when tracing is disabled for performance.
TraceRecorder ¶
Bases: Protocol
Protocol defining the trace recording interface.
Implementations can store traces in memory, write to files, send to external monitoring systems, or discard them entirely.
record ¶
record(
*,
time: Instant,
kind: str,
event_id: str | None = None,
event_type: str | None = None,
**data: Any,
) -> None
Record an engine-level trace span.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
Instant
|
Simulation time when the span occurred. |
required |
kind
|
str
|
Category (e.g., "heap.push", "heap.pop", "simulation.dequeue"). |
required |
event_id
|
str | None
|
ID of the associated event (from event.context["id"]). |
None
|
event_type
|
str | None
|
Type of the associated event. |
None
|
**data
|
Any
|
Additional structured data for the span. |
{}
|
EntitySummary
dataclass
¶
EntitySummary(
name: str,
entity_type: str,
events_handled: int,
queue_stats: QueueStats | None = None,
)
Per-entity statistics from a simulation run.
QueueStats
dataclass
¶
Queue-specific statistics for QueuedResource entities.
SimulationSummary
dataclass
¶
SimulationSummary(
duration_s: float,
total_events_processed: int,
events_cancelled: int = 0,
events_per_second: float = 0.0,
wall_clock_seconds: float = 0.0,
entities: dict[str, EntitySummary] = dict(),
)
Auto-generated summary of a simulation run.
Returned by Simulation.run() and also accessible via Simulation.summary.