AutoGPT/agbenchmark/start_benchmark.py

107 lines
3.0 KiB
Python
Raw Normal View History

import json
import os
2023-07-02 20:14:49 +00:00
import sys
from pathlib import Path
from typing import Any
2023-07-02 20:14:49 +00:00
import click
import pytest
from dotenv import load_dotenv
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("--maintain", is_flag=True, help="Runs only regression tests")
@click.option("--improve", is_flag=True, help="Run only non-regression tests")
@click.option("--mock", is_flag=True, help="Run with mock")
def start(category: str, maintain: bool, improve: 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 maintain and improve:
print(
"Error: You can't use both --maintain and --improve at the same time. Please choose one."
)
return 1
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)
os.environ["MOCK_TEST"] = "True" if mock else "False"
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}")
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])
print("Starting benchmark tests ", category)
else:
print("Running all categories")
if maintain:
print("Running only regression tests")
pytest_args.append("--maintain")
elif improve:
print("Running only non-regression tests")
pytest_args.append("--improve")
2023-06-22 12:18:22 +00:00
if mock:
pytest_args.append("--mock")
2023-07-09 20:31:31 +00:00
# when used as a library, the pytest directory to execute is in the CURRENT_DIRECTORY
pytest_args.append(str(CURRENT_DIRECTORY))
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_data() -> Any:
2023-07-02 20:14:49 +00:00
with open(REGRESSION_TESTS_PATH, "r") as file:
data = json.load(file)
return data
2023-07-02 20:14:49 +00:00
if __name__ == "__main__":
start()