-
Notifications
You must be signed in to change notification settings - Fork 1
Features/rqs generator runtime #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
52bff29
definition of state and RqsGeneratorRuntime
GioeleB00 79d8f46
defined edge runtime and more central logic for sampler
GioeleB00 57c01eb
minor changes
GioeleB00 df79400
minor changes
GioeleB00 4394654
minor bug fixed
GioeleB00 dd46e2a
pytest adapted to the new structure, added pytest for rqs_state
GioeleB00 515cc3c
Update src/app/core/runtime/rqs_generator.py
GioeleB00 52e37f8
Update src/app/core/runtime/rqs_generator.py
GioeleB00 3848bc0
Update src/app/core/runtime/edge.py
GioeleB00 8e9632f
Update src/app/core/runtime/edge.py
GioeleB00 a2fe3cc
Update tests/unit/runtime/test_requests_generator.py
GioeleB00 c4cab7f
Update src/app/core/event_samplers/common_helpers.py
GioeleB00 52ba6e9
Update src/app/core/event_samplers/common_helpers.py
GioeleB00 6759edb
Update src/app/core/event_samplers/common_helpers.py
GioeleB00 e25460c
minor changes
GioeleB00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,4 @@ | ||
| """"Api to simulate the process""" | ||
|
|
||
| import numpy as np | ||
| from fastapi import APIRouter | ||
|
|
||
| from app.core.simulation.simulation_run import run_simulation | ||
| from app.schemas.full_simulation_input import SimulationPayload | ||
| from app.schemas.simulation_output import SimulationOutput | ||
|
|
||
| router = APIRouter() | ||
|
|
||
| @router.post("/simulation") | ||
| async def event_loop_simulation(input_data: SimulationPayload) -> SimulationOutput: | ||
| """Run the simulation and return aggregate KPIs.""" | ||
| rng = np.random.default_rng() | ||
| return run_simulation(input_data, rng=rng) | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """ | ||
| defining a state in a one to one correspondence | ||
| with the requests generated that will go through | ||
| all the node necessary to accomplish the user request | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
|
|
||
|
|
||
| @dataclass | ||
| class RequestState: | ||
| """ | ||
| State object carried by each request through the simulation. | ||
| Attributes: | ||
| id: Unique identifier of the request. | ||
| t0: Timestamp (simulated env.now) when the request was generated. | ||
| history: List of hop records, each noting a node/edge visit. | ||
| finish_time: Timestamp when the requests is satisfied | ||
| """ | ||
|
|
||
| id: int # Unique request identifier | ||
| initial_time: float # Generation timestamp (env.now) | ||
| finish_time: float | None = None # a requests might be dropped | ||
| history: list[str] = field(default_factory=list) # Trace of hops | ||
|
|
||
| def record_hop(self, node_name: str, now: float) -> None: | ||
| """ | ||
| Append a record of visiting a node or edge. | ||
| Args: | ||
| node_name: Name of the node or edge being recorded. | ||
| now: register the time of the operation | ||
| """ | ||
| # Record hop as "NodeName@Timestamp" | ||
| self.history.append(f"{node_name}@{now:.3f}") | ||
|
|
||
| @property | ||
| def latency(self) -> float | None: | ||
| """ | ||
| Return the total time in the system (finish_time - initial_time), | ||
| or None if the request hasn't completed yet. | ||
| """ | ||
| if self.finish_time is None: | ||
| return None | ||
| return self.finish_time - self.initial_time | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """module for the runtime folder""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """ | ||
| Unidirectional link that simulates message transmission between nodes. | ||
| Encapsulates network behavior—latency sampling (LogNormal, Exponential, etc.), | ||
| drop probability, and optional connection-pool contention—by exposing a | ||
| `send(msg)` method. Each `send` call schedules a SimPy subprocess that | ||
| waits the sampled delay (and any resource wait) before delivering the | ||
| message to the target node's inbox. | ||
| """ | ||
| from collections.abc import Generator | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
| import simpy | ||
|
|
||
| from app.config.rqs_state import RequestState | ||
| from app.core.event_samplers.common_helpers import general_sampler | ||
| from app.schemas.system_topology_schema.full_system_topology_schema import Edge | ||
|
|
||
| if TYPE_CHECKING: | ||
| from app.schemas.random_variables_config import RVConfig | ||
|
|
||
|
|
||
|
|
||
| class EdgeRuntime: | ||
| """definining the logic to handle the edges during the simulation""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| env: simpy.Environment, | ||
| edge_config: Edge, | ||
| rng: np.random.Generator | None = None, | ||
| target_box: simpy.Store, | ||
| ) -> None: | ||
| """Definition of the instance attributes""" | ||
| self.env = env | ||
| self.edge_config = edge_config | ||
| self.target_box = target_box | ||
| self.rng = rng or np.random.default_rng() | ||
|
|
||
| def _deliver(self, state: RequestState) -> Generator[simpy.Event, None, None]: | ||
| """Function to deliver the state to the next node""" | ||
| # extract the random variables defining the latency of the edge | ||
| random_variable: RVConfig = self.edge_config.latency | ||
|
|
||
| uniform_variable = self.rng.uniform() | ||
| if uniform_variable < self.edge_config.dropout_rate: | ||
| state.finish_time = self.env.now | ||
| state.record_hop(f"{self.edge_config.id}-dropped", state.finish_time) | ||
| return | ||
|
|
||
| transit_time = general_sampler(random_variable, self.rng) | ||
| yield self.env.timeout(transit_time) | ||
| state.record_hop(self.edge_config.id, self.env.now) | ||
| yield self.target_box.put(state) | ||
|
|
||
|
|
||
| def transport(self, state: RequestState) -> simpy.Process: | ||
| """ | ||
| Called by the upstream node. Immediately spins off a SimPy process | ||
| that will handle drop + delay + delivery of `state`. | ||
| """ | ||
| return self.env.process(self._deliver(state)) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring uses incorrect attribute name 'history' instead of the actual attribute name 'hops'. However, looking at the implementation, the attribute is actually called 'history', so this comment is about the inconsistency in the test file that references 'hops'.