Skip to content
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

feat: Using utility functions #24

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions bursty-traffic.ipynb

Large diffs are not rendered by default.

28 changes: 23 additions & 5 deletions interesting-experiments.ipynb

Large diffs are not rendered by default.

42 changes: 30 additions & 12 deletions simulating-md1-queues.ipynb

Large diffs are not rendered by default.

22 changes: 18 additions & 4 deletions src/queue.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import numpy as np
import numpy.typing as npt


def intervals_to_timestamps(intervals: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
timestamps = np.copy(intervals)
for index, value in enumerate(timestamps):
if index != 0:
timestamps[index] += timestamps[index - 1]
return timestamps


def timestamps_to_intervals(timestamps: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
intervals = np.copy(timestamps)
for index, value in reversed(list(enumerate(intervals))):
if index != 0:
intervals[index] -= intervals[index - 1]
return intervals


class Queue:

def __init__(
Expand All @@ -26,10 +43,7 @@ def process(self) -> None:
self.__process_length()

def __process_arrival_times(self) -> None:
self.__arrival_times = np.copy(self.__inter_arrival_times)
for index, value in enumerate(self.__arrival_times):
if index != 0:
self.__arrival_times[index] += self.__arrival_times[index-1]
self.__arrival_times = intervals_to_timestamps(self.__inter_arrival_times)

def __process_departure_times(self) -> None:
for index, arrive_at in enumerate(self.__arrival_times):
Expand Down
12 changes: 11 additions & 1 deletion test/queue_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from src.queue import Queue
from src.queue import Queue, intervals_to_timestamps, timestamps_to_intervals


class TestQueue:
Expand Down Expand Up @@ -69,3 +69,13 @@ def test_with_two_executors(self):
np.testing.assert_allclose(queue.utilization(1), [1, 1, 1, 1], rtol=1e-02)


class TestIntervalsAndTimestamps:

def test_intervals_to_timestamps(self):
result = intervals_to_timestamps(np.array([1, 1, 1, 1]))
np.testing.assert_equal(result, [1, 2, 3, 4])

def test_timestamps_to_intervals(self):
result = timestamps_to_intervals(np.array([1, 2, 3, 4]))
np.testing.assert_equal(result, [1, 1, 1, 1])

Loading