2222 Version ,
2323)
2424
25+ # File naming constants
26+ MANIFEST_FILENAME = "manifest.json"
27+ LEADERBOARD_FILENAME = "leaderboard.json"
28+ REQUEST_ID_PREFIX = "request-"
29+
2530
2631class BenchmarkWriter :
2732 """Writes benchmark data to files."""
2833
2934 def __init__ (self , output_dir : Path ) -> None :
3035 self .output_dir = output_dir
3136
37+ def _write_json (self , path : Path , data : object ) -> Path :
38+ """Write data to JSON file, creating directories as needed.
39+
40+ Args:
41+ path: Output file path
42+ data: Data to serialize (dataclass or dict)
43+
44+ Returns the path to the written file.
45+ """
46+ path .parent .mkdir (parents = True , exist_ok = True )
47+ with path .open ("w" ) as f :
48+ json .dump (self ._to_dict (data ), f , indent = 2 )
49+ return path
50+
3251 def write_manifest (self , versions : list [str ], latest_version : str ) -> Path :
3352 """Write manifest.json to base directory.
3453
@@ -44,14 +63,7 @@ def write_manifest(self, versions: list[str], latest_version: str) -> Path:
4463 version_entries .append (entry )
4564
4665 manifest = Manifest (versions = tuple (version_entries ))
47-
48- manifest_path = self .output_dir / "manifest.json"
49- manifest_path .parent .mkdir (parents = True , exist_ok = True )
50-
51- with manifest_path .open ("w" ) as f :
52- json .dump (self ._to_dict (manifest ), f , indent = 2 )
53-
54- return manifest_path
66+ return self ._write_json (self .output_dir / MANIFEST_FILENAME , manifest )
5567
5668 def write_models_leaderboard (
5769 self , leaderboard : ModelsLeaderboard , version : str , strategy : str
@@ -62,13 +74,8 @@ def write_models_leaderboard(
6274
6375 Returns the path to the written file.
6476 """
65- output_path = self .output_dir / version / strategy / "leaderboard.json"
66- output_path .parent .mkdir (parents = True , exist_ok = True )
67-
68- with output_path .open ("w" ) as f :
69- json .dump (self ._to_dict (leaderboard ), f , indent = 2 )
70-
71- return output_path
77+ output_path = self .output_dir / version / strategy / LEADERBOARD_FILENAME
78+ return self ._write_json (output_path , leaderboard )
7279
7380 def write_strategies_leaderboard (
7481 self , leaderboard : StrategiesLeaderboard , version : str , model_key : str
@@ -79,13 +86,8 @@ def write_strategies_leaderboard(
7986
8087 Returns the path to the written file.
8188 """
82- output_path = self .output_dir / version / model_key / "leaderboard.json"
83- output_path .parent .mkdir (parents = True , exist_ok = True )
84-
85- with output_path .open ("w" ) as f :
86- json .dump (self ._to_dict (leaderboard ), f , indent = 2 )
87-
88- return output_path
89+ output_path = self .output_dir / version / model_key / LEADERBOARD_FILENAME
90+ return self ._write_json (output_path , leaderboard )
8991
9092 def write_runs (self , runs : Runs , version : str , strategy : str ) -> Path :
9193 """Write {model}.json for a model.
@@ -99,12 +101,7 @@ def write_runs(self, runs: Runs, version: str, strategy: str) -> Path:
99101 / runs .model .vendor
100102 / f"{ runs .model .name } .json"
101103 )
102- output_path .parent .mkdir (parents = True , exist_ok = True )
103-
104- with output_path .open ("w" ) as f :
105- json .dump (self ._to_dict (runs ), f , indent = 2 )
106-
107- return output_path
104+ return self ._write_json (output_path , runs )
108105
109106 def write_strategy_runs (
110107 self , runs : Runs , version : str , vendor : str , model_name : str
@@ -121,12 +118,7 @@ def write_strategy_runs(
121118 / runs .strategy .name
122119 / "runs.json"
123120 )
124- output_path .parent .mkdir (parents = True , exist_ok = True )
125-
126- with output_path .open ("w" ) as f :
127- json .dump (self ._to_dict (runs ), f , indent = 2 )
128-
129- return output_path
121+ return self ._write_json (output_path , runs )
130122
131123 def write_request_files (
132124 self ,
@@ -153,7 +145,7 @@ def write_request_files(
153145
154146 for custom_id in all_custom_ids :
155147 # Convert "request-00042" to "00042"
156- request_id = custom_id .replace ("request-" , "" )
148+ request_id = custom_id .replace (REQUEST_ID_PREFIX , "" )
157149 request_dir = output_base / run_id / request_id
158150 request_dir .mkdir (parents = True , exist_ok = True )
159151
0 commit comments