pydistsim.metrics.MetricCollector

class MetricCollector[source]

Bases: NodeNetworkObserver, AlgorithmObserver, SimulationObserver

MetricCollector is an observer that collects metrics from the simulation.

### Custom metrics Extend this class and implement the desired event methods to collect custom metrics. For registering events, call the _add_metric method. Add an instance of your custom collector to the simulation observers for it to work.

Even so, you can use the events attribute to register the events you want to listen to. This includes new custom events that you would trigger from an algorithm.

Example of implementing a custom metric collector:

class ExampleCustomMetricCollector(MetricCollector):
    events = ["example_custom_event"]

    class CustomMetricEventType(StrEnum):
        "Definition if this enum is optional. It helps to avoid typos in the event names."
        EXAMPLE_CUSTOM_EVENT_ZERO = "EXAMPLE_CUSTOM_EVENT_ZERO"
        ...

    def on_example_custom_event(self, a, b, c):
        self._add_metric(
            self.CustomMetricEventType.EXAMPLE_CUSTOM_EVENT_ZERO,
            {"a": a, "b": b, "c": c}
        )

Methods

__init__

create_report

Returns a dictionary with the collected metrics.

notify

on_added

Called when the observer is added to an observable object.

on_algorithm_finished

on_algorithm_started

on_message_delivered

on_message_sent

on_network_changed

on_node_status_changed

on_sim_state_changed

on_state_changed

on_step_done

Attributes

events

create_report()[source]

Returns a dictionary with the collected metrics.

Currently, it returns the number of messages sent, messages delivered, and quantity of status changes. Future versions may include more metrics, with better reporting capabilities.