Skip to content

Commit ce6d19b

Browse files
Update MockMicrogrid for use with datasourcing benchmark (#224)
Extends the mock microgrid with features required for the datasourcing actor benchmark which will be added in a later PR - Add num_values to have a configurable amount of iterations - MockMicrogrid: Make it work outside of pytest as well - Add start_mock_client method
2 parents 08ee2b4 + d282b62 commit ce6d19b

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

tests/timeseries/mock_microgrid.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ class MockMicrogrid: # pylint: disable=too-many-instance-attributes
5353

5454
_microgrid: MockMicrogridClient
5555

56-
def __init__(self, grid_side_meter: bool, sample_rate_s: float = 0.01):
57-
"""Create a new instance."""
56+
def __init__(
57+
self, grid_side_meter: bool, num_values: int = 2000, sample_rate_s: float = 0.01
58+
):
59+
"""Create a new instance.
60+
61+
Args:
62+
grid_side_meter: whether the main meter should be on the grid side or not.
63+
num_values: number of values to generate for each component.
64+
sample_rate_s: sample rate in seconds.
65+
"""
5866
self._components: Set[Component] = set(
5967
[
6068
Component(1, ComponentCategory.GRID),
@@ -64,6 +72,7 @@ def __init__(self, grid_side_meter: bool, sample_rate_s: float = 0.01):
6472
self._connections: Set[Connection] = set([Connection(1, 4)])
6573
self._id_increment = 0
6674
self._grid_side_meter = grid_side_meter
75+
self._num_values = num_values
6776
self._sample_rate_s = sample_rate_s
6877

6978
self._connect_to = self.grid_id
@@ -85,6 +94,31 @@ def __init__(self, grid_side_meter: bool, sample_rate_s: float = 0.01):
8594
self._actors: list[typing.Any] = []
8695
self._start_meter_streaming(4)
8796

97+
def start_mock_client(
98+
self, initialize_cb: Callable[[MockMicrogridClient], None]
99+
) -> MockMicrogridClient:
100+
"""Set up the mock client.
101+
102+
Creates the microgrid mock client, initializes it, and starts the streaming
103+
tasks.
104+
105+
For unittests, users should use the `start()` method.
106+
107+
Args:
108+
initialize_cb: callback to initialize the mock client.
109+
110+
Returns:
111+
A MockMicrogridClient instance.
112+
"""
113+
self._microgrid = MockMicrogridClient(self._components, self._connections)
114+
115+
initialize_cb(self._microgrid)
116+
117+
self._streaming_tasks = [
118+
asyncio.create_task(coro) for coro in self._streaming_coros
119+
]
120+
return self._microgrid
121+
88122
async def start(
89123
self, mocker: MockerFixture
90124
) -> Tuple[Broadcast[ComponentMetricRequest], ChannelRegistry]:
@@ -94,19 +128,16 @@ async def start(
94128
A sender to send requests to the Resampling actor, and a corresponding
95129
channel registry.
96130
"""
97-
self._microgrid = MockMicrogridClient(self._components, self._connections)
98-
self._microgrid.initialize(mocker)
99-
self._streaming_tasks = [
100-
asyncio.create_task(coro) for coro in self._streaming_coros
101-
]
131+
self.start_mock_client(lambda mock_client: mock_client.initialize(mocker))
132+
102133
await asyncio.sleep(self._sample_rate_s / 2)
103134
ret = await self._init_client_and_actors()
104135
return ret
105136

106137
async def _comp_data_send_task(
107138
self, comp_id: int, make_comp_data: Callable[[int, datetime], ComponentData]
108139
) -> None:
109-
for value in range(1, 2000):
140+
for value in range(1, self._num_values + 1):
110141
timestamp = datetime.now(tz=timezone.utc)
111142
val_to_send = value + int(comp_id / 10)
112143
# for inverters with component_id > 100, send only half the messages.

0 commit comments

Comments
 (0)