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

Handle resources allocation with scientific notation better #361

Merged
merged 2 commits into from
Nov 11, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ dmypy.json
.DS_Store
robusta_lib
.idea
.vscode
9 changes: 4 additions & 5 deletions robusta_krr/utils/resource_units.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Literal, Union

UNITS = {
UNITS: dict[str, float] = {
"m": 0.001,
"Ki": 1024,
"Mi": 1024**2,
Expand All @@ -23,15 +23,14 @@ def parse(x: str, /) -> Union[float, int]:
for unit, multiplier in UNITS.items():
if x.endswith(unit):
return float(x[: -len(unit)]) * multiplier
if "." in x:
return float(x)
return int(x)

return float(x)


def get_base(x: str, /) -> Literal[1024, 1000]:
"""Returns the base of the unit."""

for unit, multiplier in UNITS.items():
for unit, _ in UNITS.items():
if x.endswith(unit):
return 1024 if unit in ["Ki", "Mi", "Gi", "Ti", "Pi", "Ei"] else 1000
return 1000 if "." in x else 1024
Expand Down
36 changes: 36 additions & 0 deletions tests/models/test_resource_allocations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import Union

import pytest

from robusta_krr.core.models.allocations import ResourceAllocations, ResourceType


@pytest.mark.parametrize(
"cpu",
[
{"request": "5m", "limit": None},
{"request": 0.005, "limit": None},
],
)
@pytest.mark.parametrize(
"memory",
[
{"request": 128974848, "limit": 128974848},
{"request": 128.974848e6, "limit": 128.974848e6},
{"request": "128.9748480M", "limit": "128.9748480M"},
{"request": "128974848000m", "limit": "128974848000m"},
{"request": "123Mi", "limit": "123Mi"},
{"request": "128974848e0", "limit": "128974848e0"},
],
)
def test_resource_allocation_supported_formats(
cpu: dict[str, Union[str, int, float, None]], memory: dict[str, Union[str, int, float, None]]
):
allocations = ResourceAllocations(
requests={ResourceType.CPU: cpu["request"], ResourceType.Memory: memory["request"]},
limits={ResourceType.CPU: cpu["limit"], ResourceType.Memory: memory["limit"]},
)
assert allocations.requests[ResourceType.CPU] == 0.005
assert allocations.limits[ResourceType.CPU] == None
assert (allocations.requests[ResourceType.Memory] // 1) == 128974848.0
assert (allocations.limits[ResourceType.Memory] // 1) == 128974848.0
Loading