|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import shlex |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from datetime import datetime |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +LOGGER = logging.getLogger("test") |
| 11 | +logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") |
| 12 | +OUTPUT_FILE = os.environ.get("OUTPUT_FILE", "results.json") |
| 13 | + |
| 14 | + |
| 15 | +def create_report(start_time: datetime): |
| 16 | + """Format the output from the performance tests into a report.json file.""" |
| 17 | + end_time = datetime.now() |
| 18 | + elapsed_secs = (end_time - start_time).total_seconds() |
| 19 | + with open(OUTPUT_FILE) as fid: # noqa: PTH123 |
| 20 | + results = json.load(fid) |
| 21 | + LOGGER.info("%s:\n%s", OUTPUT_FILE, json.dumps(results, indent=2)) |
| 22 | + results = { |
| 23 | + "status": "PASS", |
| 24 | + "exit_code": 0, |
| 25 | + "test_file": "BenchmarkTests", |
| 26 | + "start": int(start_time.timestamp()), |
| 27 | + "end": int(end_time.timestamp()), |
| 28 | + "elapsed": elapsed_secs, |
| 29 | + } |
| 30 | + report = {"results": [results]} |
| 31 | + LOGGER.info("report.json\n%s", json.dumps(report, indent=2)) |
| 32 | + with open("report.json", "w", newline="\n") as fid: # noqa: PTH123 |
| 33 | + json.dump(report, fid) |
| 34 | + |
| 35 | + |
| 36 | +def run_command(cmd: str | list[str], **kwargs) -> None: |
| 37 | + """Run a shell command. Exit on failure.""" |
| 38 | + if isinstance(cmd, list): |
| 39 | + cmd = " ".join(cmd) |
| 40 | + LOGGER.info("Running command '%s'...", cmd) |
| 41 | + kwargs.setdefault("check", True) |
| 42 | + try: |
| 43 | + subprocess.run(shlex.split(cmd), **kwargs) # noqa: PLW1510, S603 |
| 44 | + except subprocess.CalledProcessError as e: |
| 45 | + LOGGER.error(e.output) |
| 46 | + LOGGER.error(str(e)) |
| 47 | + sys.exit(e.returncode) |
| 48 | + LOGGER.info("Running command '%s'... done.", cmd) |
| 49 | + |
| 50 | + |
| 51 | +start_time = datetime.now() |
| 52 | +ROOT = Path(__file__).absolute().parent.parent.parent |
| 53 | +data_dir = ROOT / "specifications/source/benchmarking/odm-data" |
| 54 | +if not data_dir.exists(): |
| 55 | + run_command("git clone --depth 1 https://github.com/mongodb/specifications.git") |
| 56 | + run_command("tar xf flat_models.tgz", cwd=data_dir) |
| 57 | + run_command("tar xf nested_models.tgz", cwd=data_dir) |
| 58 | + |
| 59 | +os.chdir("performance_tests") |
| 60 | +start_time = datetime.now() |
| 61 | +run_command( |
| 62 | + "python runtests.py", |
| 63 | + env=os.environ |
| 64 | + | { |
| 65 | + "DJANGO_MONGODB_PERFORMANCE_TEST_DATA_PATH": str(data_dir), |
| 66 | + "OUTPUT_FILE": OUTPUT_FILE, |
| 67 | + }, |
| 68 | +) |
| 69 | +create_report(start_time) |
0 commit comments