From 7e8c5dc71a01fa4c3c6a565ba39b60c3383eb4b6 Mon Sep 17 00:00:00 2001 From: derek-hirotsu <132305781+derek-hirotsu@users.noreply.github.com> Date: Wed, 15 Nov 2023 14:36:10 -0800 Subject: [PATCH] Cleanup report json in harnesses (#79) * use format utils for report json in test harnesses * correct start time in dota harness to be timestamp rather than time delta value * dota2 util imports * update dota 2 timestamps * update error message * update error message --- blender_render/blender.py | 6 +++--- cyberpunk2077/cyberpunk2077.py | 12 ++++++++---- dota2/dota2.py | 20 +++++++++++++------- dota2/{utils.py => dota2_utils.py} | 4 ++-- harness_utils/output.py | 1 - rocket_league/rocket_league.py | 13 +++++++++---- 6 files changed, 35 insertions(+), 21 deletions(-) rename dota2/{utils.py => dota2_utils.py} (97%) diff --git a/blender_render/blender.py b/blender_render/blender.py index b24ca9d..b99a614 100644 --- a/blender_render/blender.py +++ b/blender_render/blender.py @@ -8,7 +8,7 @@ import sys import time sys.path.insert(1, os.path.join(sys.path[0], '..')) -from harness_utils.output import write_report_json +from harness_utils.output import write_report_json, seconds_to_milliseconds SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) @@ -55,8 +55,8 @@ try: "score": score, "version": version, "device": args.device, - "start_time": round((start_time * 1000)), - "end_time": round((end_time * 1000)) + "start_time": seconds_to_milliseconds(start_time), + "end_time": seconds_to_milliseconds(end_time) } write_report_json(LOG_DIRECTORY, "report.json", report) diff --git a/cyberpunk2077/cyberpunk2077.py b/cyberpunk2077/cyberpunk2077.py index 8b3fb5b..803e285 100644 --- a/cyberpunk2077/cyberpunk2077.py +++ b/cyberpunk2077/cyberpunk2077.py @@ -12,7 +12,11 @@ sys.path.insert(1, os.path.join(sys.path[0], '..')) #pylint: disable=wrong-import-position from harness_utils.keras_service import KerasService from harness_utils.output import ( - setup_log_directory, write_report_json, DEFAULT_LOGGING_FORMAT, DEFAULT_DATE_FORMAT) + setup_log_directory, + write_report_json, + seconds_to_milliseconds, + DEFAULT_LOGGING_FORMAT, + DEFAULT_DATE_FORMAT) from harness_utils.process import terminate_processes from harness_utils.steam import exec_steam_game #pylint: enable=wrong-import-position @@ -121,12 +125,12 @@ kerasService = KerasService(args.keras_host, args.keras_port, os.path.join( LOG_DIRECTORY, "screenshot.jpg")) try: - start_time, endtime = run_benchmark() + start_time, end_time = run_benchmark() resolution = read_current_resolution() report = { "resolution": f"{resolution}", - "start_time": round((start_time * 1000)), - "end_time": round((endtime * 1000)) + "start_time": seconds_to_milliseconds(start_time), + "end_time": seconds_to_milliseconds(end_time) } write_report_json(LOG_DIRECTORY, "report.json", report) diff --git a/dota2/dota2.py b/dota2/dota2.py index efc880c..0c4f58d 100644 --- a/dota2/dota2.py +++ b/dota2/dota2.py @@ -4,12 +4,17 @@ import os import time import pydirectinput as user import sys -from utils import console_command, get_resolution, copy_replay, copy_config, get_args +from dota2_utils import console_command, get_resolution, copy_replay, copy_config, get_args sys.path.insert(1, os.path.join(sys.path[0], '..')) #pylint: disable=wrong-import-position from harness_utils.output import ( - setup_log_directory, write_report_json, DEFAULT_LOGGING_FORMAT, DEFAULT_DATE_FORMAT) + setup_log_directory, + write_report_json, + format_resolution, + seconds_to_milliseconds, + DEFAULT_LOGGING_FORMAT, + DEFAULT_DATE_FORMAT) from harness_utils.process import terminate_processes from harness_utils.keras_service import KerasService from harness_utils.steam import exec_steam_game @@ -68,6 +73,7 @@ def run_benchmark(): setup_end_time = time.time() elapsed_setup_time = round(setup_end_time - setup_start_time, 2) logging.info("Harness setup took %f seconds", elapsed_setup_time) + test_start_time = time.time() # TODO -> Mark benchmark start time using video OCR by looking for a players name if kerasService.wait_for_word(word="directed", timeout=100, interval=0.5) is None: @@ -83,18 +89,18 @@ def run_benchmark(): logging.info("Run completed. Closing game.") test_end_time = time.time() - elapsed_test_time = round((test_end_time - elapsed_setup_time), 2) + elapsed_test_time = round((test_end_time - test_start_time), 2) logging.info("Benchmark took %f seconds", elapsed_test_time) terminate_processes(PROCESS_NAME) - return elapsed_setup_time, test_end_time + return test_start_time, test_end_time try: start_time, end_time = run_benchmark() height, width = get_resolution() report = { - "resolution": f"{width}x{height}", - "start_time": round((start_time * 1000)), - "end_time": round((end_time * 1000)) + "resolution": format_resolution(width, height), + "start_time": seconds_to_milliseconds(start_time), + "end_time": seconds_to_milliseconds(end_time) } write_report_json(LOG_DIRECTORY, "report.json", report) diff --git a/dota2/utils.py b/dota2/dota2_utils.py similarity index 97% rename from dota2/utils.py rename to dota2/dota2_utils.py index 1742f6d..3f8badf 100644 --- a/dota2/utils.py +++ b/dota2/dota2_utils.py @@ -76,7 +76,7 @@ def copy_replay() -> None: destination = os.path.join(root_dir, "benchmark.dem") shutil.copyfile(source, destination) if not os.path.isfile(src_file): - raise Exception(f"Can't find no intro: {src_file}") + raise Exception(f"Can't find intro: {src_file}") try: Path(replay_path).mkdir(parents=True, exist_ok=True) except FileExistsError as e: @@ -97,7 +97,7 @@ def copy_config() -> None: destination = os.path.join(root_dir, "benchmark.cfg") shutil.copyfile(source, destination) if not os.path.isfile(src_file): - raise Exception(f"Can't find no config: {src_file}") + raise Exception(f"Can't find config: {src_file}") try: Path(config_path).mkdir(parents=True, exist_ok=True) except FileExistsError as e: diff --git a/harness_utils/output.py b/harness_utils/output.py index 7865fba..875751d 100644 --- a/harness_utils/output.py +++ b/harness_utils/output.py @@ -25,4 +25,3 @@ def format_resolution(width: int, height: int) -> str: def seconds_to_milliseconds(seconds: float | int) -> int: """Convert seconds to milliseconds""" return round((seconds * 1000)) -# test diff --git a/rocket_league/rocket_league.py b/rocket_league/rocket_league.py index b0040c1..7c0ca26 100644 --- a/rocket_league/rocket_league.py +++ b/rocket_league/rocket_league.py @@ -11,7 +11,12 @@ from utils import get_resolution, copy_replay, find_rocketleague_executable, get sys.path.insert(1, os.path.join(sys.path[0], '..')) #pylint: disable=wrong-import-position from harness_utils.output import ( - setup_log_directory, write_report_json, DEFAULT_LOGGING_FORMAT, DEFAULT_DATE_FORMAT) + setup_log_directory, + write_report_json, + format_resolution, + seconds_to_milliseconds, + DEFAULT_LOGGING_FORMAT, + DEFAULT_DATE_FORMAT) from harness_utils.process import terminate_processes from harness_utils.keras_service import KerasService @@ -160,9 +165,9 @@ try: start_time, end_time = run_benchmark() height, width = get_resolution() report = { - "resolution": f"{width}x{height}", - "start_time": round((start_time * 1000)), - "end_time": round((end_time * 1000)) + "resolution": format_resolution(width, height), + "start_time": seconds_to_milliseconds(start_time), + "end_time": seconds_to_milliseconds(end_time) } write_report_json(LOG_DIRECTORY, "report.json", report)