Skip to content

Commit

Permalink
fixup! Add persistence capability for benchmarking results on AWS S3 …
Browse files Browse the repository at this point in the history
…from manual pipeline run

Refactor persistence handling to use dedicated storage classes and improve path construction for benchmark results
  • Loading branch information
fabianliebig committed Nov 25, 2024
1 parent 3b42d0f commit eaad6d0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
23 changes: 18 additions & 5 deletions benchmarks/__main__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
"""Executes the benchmarking module."""
# Run this via 'python -m benchmarks' from the root directory.

import os

from benchmarks.domains import BENCHMARKS
from benchmarks.persistence import make_object_writer, make_path_constructor
from benchmarks.persistence import (
LocalFileObjectStorage,
PathConstructor,
S3ObjectStorage,
)

VARNAME_GITHUB_CI = "CI"
RUNS_ON_GITHUB_CI = VARNAME_GITHUB_CI in os.environ


def main():
"""Run all benchmarks."""
persistence_handler = make_object_writer()

for benchmark in BENCHMARKS:
result = benchmark()
path_constructor = make_path_constructor(benchmark, result)
path_constructor = PathConstructor.from_benchmark_and_result(benchmark, result)
persist_dict = benchmark.to_dict() | result.to_dict()
persistence_handler.write_json(persist_dict, path_constructor)

if not RUNS_ON_GITHUB_CI:
object_storage = LocalFileObjectStorage()
else:
object_storage = S3ObjectStorage()

object_storage.write_json(persist_dict, path_constructor)


if __name__ == "__main__":
Expand Down
7 changes: 4 additions & 3 deletions benchmarks/persistence/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Module for persisting benchmarking results."""

from benchmarks.persistence.persistence import (
make_object_writer,
make_path_constructor,
LocalFileObjectStorage,
PathConstructor,
S3ObjectStorage,
)

__all__ = ["make_path_constructor", "make_object_writer"]
__all__ = ["PathConstructor", "S3ObjectStorage", "LocalFileObjectStorage"]
64 changes: 26 additions & 38 deletions benchmarks/persistence/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
VARNAME_BENCHMARKING_PERSISTENCE_PATH = "BAYBE_BENCHMARKING_PERSISTENCE_PATH"
PERSIST_DATA_TO_S3_BUCKET = VARNAME_BENCHMARKING_PERSISTENCE_PATH in os.environ

VARNAME_GITHUB_CI = "CI"
RUNS_ON_GITHUB_CI = VARNAME_GITHUB_CI in os.environ


class PathStrategy(Enum):
"""The way a path extension is constructed."""
Expand Down Expand Up @@ -76,6 +73,31 @@ def _string_sanitizer(self, string: str) -> str:
)
return "".join([char if char in ALLOWED_CHARS else "-" for char in string])

@classmethod
def from_benchmark_and_result(
cls: "PathConstructor", benchmark: Benchmark, result: Result
) -> "PathConstructor":
"""Create a path constructor from benchmark and result.
Args:
benchmark: The benchmark for which the result is stored.
result: The result of the benchmark.
Returns:
The persistence path constructor.
"""
benchmark_name = benchmark.name
start_datetime = result.metadata.start_datetime
commit_hash = result.metadata.commit_hash
latest_baybe_tag = result.metadata.latest_baybe_tag

return PathConstructor(
benchmark_name=benchmark_name,
latest_baybe_tag=latest_baybe_tag,
commit_hash=commit_hash,
execution_date_time=start_datetime,
)

def get_path(self, strategy: PathStrategy) -> Path:
"""Construct the path of a result object.
Expand Down Expand Up @@ -171,7 +193,7 @@ def write_json(self, object: dict, path_constructor: PathConstructor) -> None:


@define
class LocalFileSystemObjectStorage(ObjectStorage):
class LocalFileObjectStorage(ObjectStorage):
"""Class for persisting JSON serializable dicts locally."""

folder_path_prefix: Path = field(converter=Path, default=Path("."))
Expand Down Expand Up @@ -203,37 +225,3 @@ def write_json(self, object: dict, path_constructor: PathConstructor) -> None:
)
with open(path_object.resolve(), "w") as file:
json.dump(object, file)


def make_object_writer() -> ObjectStorage:
"""Create a persistence handler based on the environment variables.
Returns:
The persistence handler.
"""
if not RUNS_ON_GITHUB_CI:
return LocalFileSystemObjectStorage()
return S3ObjectStorage()


def make_path_constructor(benchmark: Benchmark, result: Result) -> PathConstructor:
"""Create a path constructor.
Args:
benchmark: The benchmark for which the result is stored.
result: The result of the benchmark.
Returns:
The persistence path constructor.
"""
benchmark_name = benchmark.name
start_datetime = result.metadata.start_datetime
commit_hash = result.metadata.commit_hash
latest_baybe_tag = result.metadata.latest_baybe_tag

return PathConstructor(
benchmark_name=benchmark_name,
latest_baybe_tag=latest_baybe_tag,
commit_hash=commit_hash,
execution_date_time=start_datetime,
)

0 comments on commit eaad6d0

Please sign in to comment.