|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import shlex |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +LOGGER = logging.getLogger("test") |
| 10 | +logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") |
| 11 | +OUTPUT_FILE = os.environ.get("OUTPUT_FILE") |
| 12 | + |
| 13 | + |
| 14 | +def handle_perf(start_time: datetime): |
| 15 | + end_time = datetime.now() |
| 16 | + elapsed_secs = (end_time - start_time).total_seconds() |
| 17 | + with open(OUTPUT_FILE) as fid: # noqa: PTH123 |
| 18 | + results = json.load(fid) |
| 19 | + LOGGER.info("results.json:\n%s", json.dumps(results, indent=2)) |
| 20 | + |
| 21 | + results = { |
| 22 | + "status": "PASS", |
| 23 | + "exit_code": 0, |
| 24 | + "test_file": "BenchMarkTests", |
| 25 | + "start": int(start_time.timestamp()), |
| 26 | + "end": int(end_time.timestamp()), |
| 27 | + "elapsed": elapsed_secs, |
| 28 | + } |
| 29 | + report = {"failures": 0, "results": [results]} |
| 30 | + |
| 31 | + LOGGER.info("report.json\n%s", json.dumps(report, indent=2)) |
| 32 | + |
| 33 | + with open("report.json", "w", newline="\n") as fid: # noqa: PTH123 |
| 34 | + json.dump(report, fid) |
| 35 | + |
| 36 | + |
| 37 | +def run_command(cmd: str | list[str], **kwargs) -> None: |
| 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 | +os.chdir("tests/performance") |
| 52 | + |
| 53 | +start_time = datetime.now() |
| 54 | +run_command(["python manage.py test"]) |
| 55 | +handle_perf(start_time) |
0 commit comments