Add bandwidth_analysis/src/bandwidth_analysis/cli.py
This commit is contained in:
parent
6aa6e5e85d
commit
06195f5c88
1 changed files with 59 additions and 0 deletions
59
bandwidth_analysis/src/bandwidth_analysis/cli.py
Normal file
59
bandwidth_analysis/src/bandwidth_analysis/cli.py
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from pandas import DataFrame
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from bandwidth_analysis import core
|
||||||
|
|
||||||
|
|
||||||
|
def _load_json_file(path: Path) -> dict[str, Any]:
|
||||||
|
if not path.exists() or not path.is_file():
|
||||||
|
raise FileNotFoundError(f"Input file not found: {path}")
|
||||||
|
with path.open("r", encoding="utf-8") as f:
|
||||||
|
data = json.load(f)
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise ValueError(f"Invalid JSON structure in {path}, expected object.")
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def _write_json_file(path: Path, data: Any) -> None:
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
with path.open("w", encoding="utf-8") as f:
|
||||||
|
json.dump(data, f, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="Analyse der Bandbreitenveränderung zwischen Baseline und Test.")
|
||||||
|
parser.add_argument("--baseline", required=True, help="Pfad zur JSON-Baseline-Datei.")
|
||||||
|
parser.add_argument("--test", required=True, help="Pfad zur JSON-Testdatei.")
|
||||||
|
parser.add_argument("--out", required=False, help="Optionaler Pfad für die Ausgabe der Analyse.")
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
baseline_path = Path(args.baseline)
|
||||||
|
test_path = Path(args.test)
|
||||||
|
out_path = Path(args.out) if args.out else Path("output/analysis_result.json")
|
||||||
|
|
||||||
|
baseline_data = _load_json_file(baseline_path)
|
||||||
|
test_data = _load_json_file(test_path)
|
||||||
|
|
||||||
|
# Falls DataFrame-Input nötig wird, kann Konvertierung erfolgen:
|
||||||
|
baseline_df = DataFrame(baseline_data)
|
||||||
|
test_df = DataFrame(test_data)
|
||||||
|
|
||||||
|
result = core.analyze_bandwidth(baseline_df, test_df)
|
||||||
|
|
||||||
|
# Erwartet, dass result in ein JSON-serialisierbares dict konvertiert werden kann
|
||||||
|
if isinstance(result, dict):
|
||||||
|
serializable_result = result
|
||||||
|
elif hasattr(result, "__dict__"):
|
||||||
|
serializable_result = result.__dict__
|
||||||
|
else:
|
||||||
|
raise TypeError("analysis_result is not JSON serializable")
|
||||||
|
|
||||||
|
_write_json_file(out_path, serializable_result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in a new issue