11"""Script to run a benchmark and upload results to S3."""
22
33import argparse
4+ import base64
45import json
56import os
67from datetime import datetime , timezone
8+ from pathlib import Path
79
810from botocore .exceptions import ClientError
911
1012from sdgym ._benchmark .benchmark import _benchmark_multi_table_compute_gcp
1113from sdgym .benchmark import benchmark_single_table_aws
1214from sdgym .run_benchmark .utils import (
15+ GCP_PROJECT ,
16+ GCP_ZONE ,
1317 KEY_DATE_FILE ,
1418 OUTPUT_DESTINATION_AWS ,
1519 SYNTHESIZERS_SPLIT_MULTI_TABLE ,
@@ -46,6 +50,50 @@ def append_benchmark_run(
4650 )
4751
4852
53+ def _load_gcp_service_account_from_env ():
54+ """Load GCP service account JSON from env.
55+
56+ Supports:
57+ - raw JSON string
58+ - base64-encoded JSON string
59+ """
60+ raw = os .getenv ('GCP_SERVICE_ACCOUNT_JSON' , '' ) or ''
61+ if not raw .strip ():
62+ return {}
63+
64+ try :
65+ return json .loads (raw )
66+ except json .JSONDecodeError :
67+ decoded = base64 .b64decode (raw ).decode ('utf-8' )
68+ return json .loads (decoded )
69+
70+
71+ def create_credentials_file (filepath ):
72+ """Create credentials file used by the benchmark launcher."""
73+ gcp_sa = _load_gcp_service_account_from_env ()
74+
75+ credentials = {
76+ 'aws' : {
77+ 'aws_access_key_id' : os .getenv ('AWS_ACCESS_KEY_ID' ),
78+ 'aws_secret_access_key' : os .getenv ('AWS_SECRET_ACCESS_KEY' ),
79+ },
80+ 'gcp' : {
81+ ** gcp_sa ,
82+ 'gcp_project' : GCP_PROJECT ,
83+ 'gcp_zone' : GCP_ZONE ,
84+ },
85+ 'sdv' : {
86+ 'username' : os .getenv ('SDV_ENTERPRISE_USERNAME' ),
87+ 'license_key' : os .getenv ('SDV_ENTERPRISE_LICENSE_KEY' ),
88+ },
89+ }
90+
91+ Path (filepath ).parent .mkdir (parents = True , exist_ok = True )
92+ with open (filepath , 'w' , encoding = 'utf-8' ) as f :
93+ json .dump (credentials , f , indent = 2 , sort_keys = True )
94+ f .write ('\n ' )
95+
96+
4997def _parse_args ():
5098 parser = argparse .ArgumentParser ()
5199 parser .add_argument (
@@ -54,12 +102,18 @@ def _parse_args():
54102 default = 'single_table' ,
55103 help = 'Benchmark modality to run.' ,
56104 )
105+ parser .add_argument (
106+ '--gcp-output-destination' ,
107+ default = 's3://sdgym-benchmark/Debug/GCP/' ,
108+ help = 'Where to store GCP benchmark results (S3).' ,
109+ )
57110 return parser .parse_args ()
58111
59112
60113def main ():
61114 """Main function to run the benchmark and upload results."""
62115 args = _parse_args ()
116+
63117 aws_access_key_id = os .getenv ('AWS_ACCESS_KEY_ID' )
64118 aws_secret_access_key = os .getenv ('AWS_SECRET_ACCESS_KEY' )
65119 date_str = datetime .now (timezone .utc ).strftime ('%Y-%m-%d' )
@@ -76,24 +130,36 @@ def main():
76130 )
77131
78132 append_benchmark_run (
79- aws_access_key_id , aws_secret_access_key , date_str , modality = 'single_table'
133+ aws_access_key_id ,
134+ aws_secret_access_key ,
135+ date_str ,
136+ modality = 'single_table' ,
80137 )
138+ compute_service = 'AWS'
81139
82140 else :
141+ runner_temp = os .environ .get ('RUNNER_TEMP' , '/tmp' )
142+ cred_path = os .path .join (runner_temp , 'credentials.json' )
143+ create_credentials_file (cred_path )
144+
83145 for synthesizer_group in SYNTHESIZERS_SPLIT_MULTI_TABLE :
84146 _benchmark_multi_table_compute_gcp (
85- output_destination = 's3://sdgym-benchmark/Debug/GCP/' ,
86- aws_access_key_id = aws_access_key_id ,
87- aws_secret_access_key = aws_secret_access_key ,
147+ output_destination = args .gcp_output_destination ,
148+ credential_filepath = cred_path ,
88149 synthesizers = synthesizer_group ,
89150 compute_privacy_score = False ,
90151 timeout = 345600 , # 4 days
91152 )
153+
92154 append_benchmark_run (
93- aws_access_key_id , aws_secret_access_key , date_str , modality = 'multi_table'
155+ aws_access_key_id ,
156+ aws_secret_access_key ,
157+ date_str ,
158+ modality = 'multi_table' ,
94159 )
160+ compute_service = 'GCP'
95161
96- post_benchmark_launch_message (date_str , compute_service = 'GCP' )
162+ post_benchmark_launch_message (date_str , compute_service = compute_service )
97163
98164
99165if __name__ == '__main__' :
0 commit comments