Skip to content

Commit

Permalink
Switch from mashumaro to pyserde
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Nov 10, 2023
1 parent e2f0700 commit b362450
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmarks/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ gitpython==3.1.37
git+https://code.it4i.cz/def/cluster.git@428bd7f6c15525942358f5ff6b54d07b3828025f
pandas==1.3.3
tqdm==4.62.3
mashumaro==2.9
pyserde==0.12.3
psutil==5.8.0
humanize==3.12.0
git+https://github.com/it4innovations/snailwatch@4d590c55e6b1e404e0398e8005dd998f5bc50be9#subdirectory=client
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/src/benchmark/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
from pathlib import Path
from typing import Any, Dict, List, Optional

from mashumaro import DataClassDictMixin

from ..environment import EnvironmentDescriptor
from ..workloads import Workload


@dataclasses.dataclass(frozen=True)
class BenchmarkIdentifier(DataClassDictMixin):
class BenchmarkIdentifier:
# Name of the workload
workload: str
# Environment type
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/src/benchmark/result.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import dataclasses

from mashumaro import DataClassDictMixin


@dataclasses.dataclass(frozen=True)
class BenchmarkResult(DataClassDictMixin):
class BenchmarkResult:
pass


Expand Down
35 changes: 27 additions & 8 deletions benchmarks/src/monitoring/record.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import dataclasses
import logging
from typing import Dict, List
import typing
from typing import Dict, List, TypeVar

import dataclasses
import psutil
from mashumaro import DataClassJSONMixin


@dataclasses.dataclass
class ResourceRecord(DataClassJSONMixin):
class ResourceRecord:
cpu: List[float]
mem: float
connections: int
Expand All @@ -18,14 +18,14 @@ class ResourceRecord(DataClassJSONMixin):


@dataclasses.dataclass
class ProcessRecord(DataClassJSONMixin):
class ProcessRecord:
rss: int
vm: int
cpu: float


@dataclasses.dataclass
class MonitoringRecord(DataClassJSONMixin):
class MonitoringRecord:
timestamp: float
resources: ResourceRecord
processes: Dict[str, ProcessRecord]
Expand All @@ -34,11 +34,11 @@ class MonitoringRecord(DataClassJSONMixin):
def deserialize_records(file) -> List["MonitoringRecord"]:
records = []
for line in file:
records.append(MonitoringRecord.from_json(line))
records.append(from_json(MonitoringRecord, line))
return records

def serialize(self, file):
file.write(self.to_json())
to_json(self, file)


def record_resources() -> ResourceRecord:
Expand Down Expand Up @@ -77,3 +77,22 @@ def generate_record(timestamp: int, processes: List[psutil.Process]) -> Monitori
resources=record_resources(),
processes=record_processes(processes),
)


# This code is duplicated because Python cannot handle both relative imports and this file being executed as a package.
Type = TypeVar("Type")


def from_json(cls: type[Type], input: typing.Union[typing.TextIO, str]) -> Type:
from serde import json

if not isinstance(input, str):
input = input.read()
return json.from_json(cls, input)


def to_json(object: typing.Any, file: typing.TextIO):
from serde import json

serialized = json.to_json(object)
file.write(serialized)
8 changes: 4 additions & 4 deletions benchmarks/src/submit/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from pathlib import Path
from typing import Optional

from mashumaro import DataClassJSONMixin
from ..utils.io import from_json, to_json


@dataclasses.dataclass(frozen=True)
class PBSSubmitOptions(DataClassJSONMixin):
class PBSSubmitOptions:
queue: str
nodes: int
walltime: datetime.timedelta
Expand All @@ -18,9 +18,9 @@ class PBSSubmitOptions(DataClassJSONMixin):

def serialize_submit_options(options: PBSSubmitOptions, path: Path):
with open(path, "w") as f:
f.write(options.to_json())
to_json(options, f)


def deserialize_submit_options(path: Path) -> PBSSubmitOptions:
with open(path) as f:
return PBSSubmitOptions.from_json(f.read())
return from_json(PBSSubmitOptions, f)
19 changes: 19 additions & 0 deletions benchmarks/src/utils/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import typing
from typing import TypeVar

Type = TypeVar("Type")


def from_json(cls: type[Type], input: typing.Union[typing.TextIO, str]) -> Type:
from serde import json

if not isinstance(input, str):
input = input.read()
return json.from_json(cls, input)


def to_json(object: typing.Any, file: typing.TextIO):
from serde import json

serialized = json.to_json(object)
file.write(serialized)

0 comments on commit b362450

Please sign in to comment.