AutoGPT/agbenchmark/start_benchmark.py

113 lines
3.1 KiB
Python
Raw Normal View History

import json
import os
2023-07-02 20:14:49 +00:00
import sys
from pathlib import Path
2023-07-02 20:14:49 +00:00
from typing import List
import click
import pytest
from dotenv import load_dotenv, set_key
load_dotenv()
CURRENT_DIRECTORY = Path(__file__).resolve().parent
2023-07-03 18:53:28 +00:00
CONFIG_PATH = str(Path(os.getcwd()) / "config.json")
REGRESSION_TESTS_PATH = str(Path(os.getcwd()) / "regression_tests.json")
2023-07-02 20:14:49 +00:00
@click.group()
2023-07-02 20:14:49 +00:00
def cli() -> None:
pass
@cli.command()
2023-06-22 12:18:22 +00:00
@click.option("--category", default=None, help="Specific category to run")
@click.option("--reg", is_flag=True, help="Runs only regression tests")
@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
if not os.path.exists(CONFIG_PATH) or os.stat(CONFIG_PATH).st_size == 0:
2023-06-22 12:18:22 +00:00
config = {}
config["workspace"] = click.prompt(
"Please enter a new workspace path",
default=os.path.join(Path.home(), "workspace"),
)
config["entry_path"] = click.prompt(
"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",
)
with open(CONFIG_PATH, "w") as f:
json.dump(config, f)
2023-06-22 12:18:22 +00:00
else:
# If the configuration file exists and is not empty, load it
with open(CONFIG_PATH, "r") as f:
2023-06-22 12:18:22 +00:00
config = json.load(f)
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)
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)
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])
else:
if reg:
print("Running all regression tests")
tests_to_run = get_regression_tests()
2023-06-22 12:18:22 +00:00
else:
print("Running all categories")
2023-06-22 12:18:22 +00:00
if mock:
pytest_args.append("--mock")
2023-06-22 12:18:22 +00:00
# Run pytest with the constructed arguments
if not tests_to_run:
tests_to_run = [str(CURRENT_DIRECTORY)]
pytest_args.extend(tests_to_run)
2023-07-02 20:14:49 +00:00
return sys.exit(pytest.main(pytest_args))
2023-07-02 20:14:49 +00:00
def get_regression_tests() -> List[str]:
if not Path(REGRESSION_TESTS_PATH).exists():
2023-07-02 20:14:49 +00:00
with open(REGRESSION_TESTS_PATH, "w") as file:
json.dump({}, file)
2023-07-02 20:14:49 +00:00
with open(REGRESSION_TESTS_PATH, "r") as file:
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()
]
return regression_tests
2023-07-02 20:14:49 +00:00
if __name__ == "__main__":
start()