2024-12-02 14:52:14 -03:00
|
|
|
import argparse
|
2024-11-28 15:15:05 -03:00
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
2024-12-02 14:52:14 -03:00
|
|
|
from typing import Dict
|
2024-11-28 15:15:05 -03:00
|
|
|
|
|
|
|
|
from pydantic_core import ValidationError
|
|
|
|
|
|
2024-12-02 14:52:14 -03:00
|
|
|
from benchmarks.core.config import ConfigParser, ExperimentBuilder
|
|
|
|
|
from benchmarks.core.experiments.experiments import Experiment
|
2024-11-28 15:15:05 -03:00
|
|
|
from benchmarks.deluge.config import DelugeExperimentConfig
|
|
|
|
|
|
2024-12-02 14:52:14 -03:00
|
|
|
config_parser = ConfigParser()
|
|
|
|
|
config_parser.register(DelugeExperimentConfig)
|
2024-11-28 15:15:05 -03:00
|
|
|
|
|
|
|
|
|
2024-12-02 14:52:14 -03:00
|
|
|
def cmd_list(experiments: Dict[str, ExperimentBuilder[Experiment]], _):
|
2024-12-02 09:20:06 -03:00
|
|
|
print(f'Available experiments are:')
|
2024-11-28 15:52:38 -03:00
|
|
|
for experiment in experiments.keys():
|
2024-11-28 15:15:05 -03:00
|
|
|
print(f' - {experiment}')
|
|
|
|
|
|
|
|
|
|
|
2024-12-02 14:52:14 -03:00
|
|
|
def cmd_run(experiments: Dict[str, ExperimentBuilder[Experiment]], args):
|
|
|
|
|
if args.experiment not in experiments:
|
|
|
|
|
print(f'Experiment {args.experiment} not found.')
|
|
|
|
|
sys.exit(-1)
|
|
|
|
|
experiments[args.experiment].build().run()
|
|
|
|
|
|
|
|
|
|
|
2024-12-10 13:55:13 -03:00
|
|
|
def cmd_describe(args):
|
|
|
|
|
if not args.type:
|
|
|
|
|
print(f'Available experiment types are:')
|
|
|
|
|
for experiment in config_parser.experiment_types.keys():
|
|
|
|
|
print(f' - {experiment}')
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
print(config_parser.experiment_types[args.type].schema_json(indent=2))
|
|
|
|
|
|
|
|
|
|
|
2024-12-02 14:52:14 -03:00
|
|
|
def _parse_config(config: Path) -> Dict[str, ExperimentBuilder[Experiment]]:
|
|
|
|
|
if not config.exists():
|
|
|
|
|
print(f'Config file {config} does not exist.')
|
2024-11-28 15:15:05 -03:00
|
|
|
sys.exit(-1)
|
2024-12-02 14:52:14 -03:00
|
|
|
|
|
|
|
|
with config.open(encoding='utf-8') as infile:
|
|
|
|
|
try:
|
|
|
|
|
return config_parser.parse(infile)
|
|
|
|
|
except ValidationError as e:
|
|
|
|
|
print(f'There were errors parsing the config file.')
|
|
|
|
|
for error in e.errors():
|
|
|
|
|
print(f' - {error["loc"]}: {error["msg"]} {error["input"]}')
|
|
|
|
|
sys.exit(-1)
|
2024-11-28 16:52:10 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _init_logging():
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
|
|
|
)
|
2024-11-28 15:15:05 -03:00
|
|
|
|
|
|
|
|
|
2024-12-02 14:52:14 -03:00
|
|
|
def main():
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
|
|
commands = parser.add_subparsers(required=True)
|
|
|
|
|
|
2024-12-10 13:55:13 -03:00
|
|
|
experiments = commands.add_parser('experiments', help='List or run experiments in config file.')
|
|
|
|
|
experiments.add_argument('config', type=Path, help='Path to the experiment configuration file.')
|
|
|
|
|
experiment_commands = experiments.add_subparsers(required=True)
|
|
|
|
|
|
|
|
|
|
list_cmd = experiment_commands.add_parser('list', help='Lists available experiments.')
|
|
|
|
|
list_cmd.set_defaults(func=lambda args: cmd_list(_parse_config(args.config), args))
|
|
|
|
|
|
|
|
|
|
run_cmd = experiment_commands.add_parser('run', help='Runs an experiment')
|
2024-12-02 14:52:14 -03:00
|
|
|
run_cmd.add_argument('experiment', type=str, help='Name of the experiment to run.')
|
2024-12-10 13:55:13 -03:00
|
|
|
run_cmd.set_defaults(func=lambda args: cmd_run(_parse_config(args.config), args))
|
|
|
|
|
|
|
|
|
|
describe = commands.add_parser('describe', help='Shows the JSON schema for the various experiment types.')
|
|
|
|
|
describe.add_argument('type', type=str, help='Type of the experiment to describe.',
|
|
|
|
|
choices=config_parser.experiment_types.keys(), nargs='?')
|
|
|
|
|
|
|
|
|
|
describe.set_defaults(func=cmd_describe)
|
2024-12-02 14:52:14 -03:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2024-12-02 09:20:06 -03:00
|
|
|
|
2024-11-28 16:52:10 -03:00
|
|
|
_init_logging()
|
2024-12-01 23:12:49 -03:00
|
|
|
|
2024-12-10 13:55:13 -03:00
|
|
|
args.func(args)
|
2024-12-02 14:52:14 -03:00
|
|
|
|
2024-12-01 23:12:49 -03:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-12-02 14:52:14 -03:00
|
|
|
main()
|