-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
86 lines (67 loc) · 2.9 KB
/
Copy pathrun.py
File metadata and controls
86 lines (67 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import argparse
import subprocess
import sys
import os
import getpass
from pathlib import Path
def execute_remote_benchmark(host, username, program_path, output_file):
try:
program_path = Path(program_path).resolve()
if not program_path.exists():
raise FileNotFoundError(f"File not found: {program_path}")
program_name = program_path.name
remote_dir = f"/tmp/bench_{os.urandom(4).hex()}"
remote_program_path = f"{remote_dir}/{program_name}"
create_dir_cmd = f"ssh {username}@{host} 'mkdir -p {remote_dir}'"
subprocess.run(create_dir_cmd, shell=True, check=True, capture_output=True)
scp_cmd = f"scp {program_path} {username}@{host}:{remote_program_path}"
subprocess.run(scp_cmd, shell=True, check=True, capture_output=True)
exec_cmd = (f"ssh {username}@{host} "
f"'chmod +x {remote_program_path} && "
f"cd {remote_dir} && ./{program_name} "
f"--benchmark_out=result.json --benchmark_out_format=json'")
subprocess.run(exec_cmd, shell=True, check=True, capture_output=True)
output_file.parent.mkdir(parents=True, exist_ok=True)
retrieve_cmd = f"scp {username}@{host}:{remote_dir}/result.json {output_file}"
subprocess.run(retrieve_cmd, shell=True, check=True, capture_output=True)
cleanup_cmd = f"ssh {username}@{host} 'rm -rf {remote_dir}'"
subprocess.run(cleanup_cmd, shell=True, check=True, capture_output=True)
print(f"\n✅ saved: {output_file}")
return True
except subprocess.CalledProcessError as e:
print(f"❌ Failed: {e.cmd}")
if e.stderr:
print(f"{e.stderr.decode().strip()}")
return False
except Exception as e:
print(f"❌ Fault: {str(e)}")
return False
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--host', required=True)
parser.add_argument('--user', default=getpass.getuser())
parser.add_argument('--program', required=True)
parser.add_argument('--output')
args = parser.parse_args()
program_path = Path(args.program).resolve()
if args.output:
output_file = Path(args.output).resolve()
else:
parent_dir = program_path.parent
if parent_dir == Path.cwd():
sys.exit(1)
dir_name = parent_dir.name
output_dir = Path.cwd() / "output"
output_file = output_dir / f"{dir_name}.json"
output_file.parent.mkdir(parents=True, exist_ok=True)
success = execute_remote_benchmark(
host=args.host,
username=args.user,
program_path=program_path,
output_file=output_file
)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()