2023-06-21 15:43:18 +00:00
|
|
|
import json
|
|
|
|
import os
|
2023-07-02 20:14:49 +00:00
|
|
|
import sys
|
2023-06-27 22:17:54 +00:00
|
|
|
from pathlib import Path
|
2023-07-02 20:14:49 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import click
|
|
|
|
import pytest
|
2023-06-27 22:17:54 +00:00
|
|
|
from dotenv import load_dotenv, set_key
|
|
|
|
|
|
|
|
load_dotenv()
|
2023-06-21 15:43:18 +00:00
|
|
|
|
2023-07-02 14:38:30 +00:00
|
|
|
CURRENT_DIRECTORY = Path(__file__).resolve().parent
|
|
|
|
|
|
|
|
|
2023-07-03 18:53:28 +00:00
|
|
|
CONFIG_PATH = str(Path(os.getcwd()) / "config.json")
|
2023-07-02 14:38:30 +00:00
|
|
|
|
|
|
|
REGRESSION_TESTS_PATH = str(Path(os.getcwd()) / "regression_tests.json")
|
2023-06-21 15:43:18 +00:00
|
|
|
|
2023-07-02 20:14:49 +00:00
|
|
|
|
2023-06-21 15:43:18 +00:00
|
|
|
@click.group()
|
2023-07-02 20:14:49 +00:00
|
|
|
def cli() -> None:
|
2023-06-21 15:43:18 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@cli.command()
|
2023-06-22 12:18:22 +00:00
|
|
|
@click.option("--category", default=None, help="Specific category to run")
|
2023-07-02 14:38:30 +00:00
|
|
|
@click.option("--reg", is_flag=True, help="Runs only regression tests")
|
2023-06-27 22:17:54 +00:00
|
|
|
@click.option("--mock", is_flag=True, help="Run with mock")
|
2023-07-02 20:14:49 +00:00
|
|
|
def start(category: str, reg: bool, mock: bool) -> int:
|
2023-06-22 12:18:22 +00:00
|
|
|
"""Start the benchmark tests. If a category flag is provided, run the categories with that mark."""
|
|
|
|
# Check if configuration file exists and is not empty
|
2023-07-02 14:38:30 +00:00
|
|
|
if not os.path.exists(CONFIG_PATH) or os.stat(CONFIG_PATH).st_size == 0:
|
2023-06-22 12:18:22 +00:00
|
|
|
config = {}
|
2023-06-21 15:43:18 +00:00
|
|
|
|
|
|
|
config["workspace"] = click.prompt(
|
2023-06-27 22:17:54 +00:00
|
|
|
"Please enter a new workspace path",
|
2023-06-30 15:55:43 +00:00
|
|
|
default=os.path.join(Path.home(), "workspace"),
|
|
|
|
)
|
|
|
|
|
2023-07-04 17:23:00 +00:00
|
|
|
config["entry_path"] = click.prompt(
|
2023-06-30 15:55:43 +00:00
|
|
|
"Please enter a the path to your run_specific_agent function implementation",
|
|
|
|
default="/benchmarks.py",
|
|
|
|
)
|
|
|
|
|
|
|
|
config["cutoff"] = click.prompt(
|
|
|
|
"Please enter a hard cutoff runtime for your agent",
|
|
|
|
default="60",
|
2023-06-21 15:43:18 +00:00
|
|
|
)
|
|
|
|
|
2023-07-02 14:38:30 +00:00
|
|
|
with open(CONFIG_PATH, "w") as f:
|
2023-06-21 15:43:18 +00:00
|
|
|
json.dump(config, f)
|
2023-06-22 12:18:22 +00:00
|
|
|
else:
|
|
|
|
# If the configuration file exists and is not empty, load it
|
2023-07-02 14:38:30 +00:00
|
|
|
with open(CONFIG_PATH, "r") as f:
|
2023-06-22 12:18:22 +00:00
|
|
|
config = json.load(f)
|
2023-06-21 15:43:18 +00:00
|
|
|
|
2023-06-27 22:17:54 +00:00
|
|
|
set_key(".env", "MOCK_TEST", "True" if mock else "False")
|
|
|
|
|
2023-06-26 13:36:13 +00:00
|
|
|
# create workspace directory if it doesn't exist
|
2023-06-27 17:25:47 +00:00
|
|
|
workspace_path = os.path.abspath(config["workspace"])
|
2023-06-26 13:36:13 +00:00
|
|
|
if not os.path.exists(workspace_path):
|
|
|
|
os.makedirs(workspace_path, exist_ok=True)
|
|
|
|
|
2023-07-02 14:38:30 +00:00
|
|
|
if not os.path.exists(REGRESSION_TESTS_PATH):
|
|
|
|
with open(REGRESSION_TESTS_PATH, "a"):
|
2023-06-27 17:25:47 +00:00
|
|
|
pass
|
|
|
|
|
2023-06-22 12:18:22 +00:00
|
|
|
print("Current configuration:")
|
|
|
|
for key, value in config.items():
|
|
|
|
print(f"{key}: {value}")
|
|
|
|
|
|
|
|
print("Starting benchmark tests...", category)
|
2023-07-02 14:38:30 +00:00
|
|
|
tests_to_run = []
|
|
|
|
pytest_args = ["-vs"]
|
2023-06-22 12:18:22 +00:00
|
|
|
if category:
|
2023-07-02 20:14:49 +00:00
|
|
|
pytest_args.extend(["-m", category])
|
2023-06-21 15:43:18 +00:00
|
|
|
else:
|
2023-07-02 14:38:30 +00:00
|
|
|
if reg:
|
|
|
|
print("Running all regression tests")
|
|
|
|
tests_to_run = get_regression_tests()
|
2023-06-22 12:18:22 +00:00
|
|
|
else:
|
2023-07-02 14:38:30 +00:00
|
|
|
print("Running all categories")
|
2023-06-22 12:18:22 +00:00
|
|
|
|
2023-06-27 22:17:54 +00:00
|
|
|
if mock:
|
|
|
|
pytest_args.append("--mock")
|
|
|
|
|
2023-06-22 12:18:22 +00:00
|
|
|
# Run pytest with the constructed arguments
|
2023-07-02 14:38:30 +00:00
|
|
|
if not tests_to_run:
|
|
|
|
tests_to_run = [str(CURRENT_DIRECTORY)]
|
|
|
|
pytest_args.extend(tests_to_run)
|
2023-06-21 15:43:18 +00:00
|
|
|
|
2023-07-02 20:14:49 +00:00
|
|
|
return sys.exit(pytest.main(pytest_args))
|
2023-06-21 15:43:18 +00:00
|
|
|
|
2023-07-02 20:14:49 +00:00
|
|
|
|
|
|
|
def get_regression_tests() -> List[str]:
|
2023-07-02 14:38:30 +00:00
|
|
|
if not Path(REGRESSION_TESTS_PATH).exists():
|
2023-07-02 20:14:49 +00:00
|
|
|
with open(REGRESSION_TESTS_PATH, "w") as file:
|
2023-07-02 14:38:30 +00:00
|
|
|
json.dump({}, file)
|
|
|
|
|
2023-07-02 20:14:49 +00:00
|
|
|
with open(REGRESSION_TESTS_PATH, "r") as file:
|
2023-07-02 14:38:30 +00:00
|
|
|
data = json.load(file)
|
|
|
|
|
2023-07-02 20:14:49 +00:00
|
|
|
regression_tests = [
|
|
|
|
str(CURRENT_DIRECTORY / ".." / value["test"]) for key, value in data.items()
|
|
|
|
]
|
2023-07-02 14:38:30 +00:00
|
|
|
|
|
|
|
return regression_tests
|
|
|
|
|
2023-07-02 20:14:49 +00:00
|
|
|
|
2023-06-21 15:43:18 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
start()
|