22import argparse
33import json
44import os
5- import shutil
65import socket
76import subprocess
87import sys
98import time
9+ from pathlib import Path
1010from typing import Optional , Tuple
1111
1212HEARTBEAT_REQUEST = bytes ([0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 4 , 0 , 0 , 0 , 0 ])
@@ -33,8 +33,33 @@ def recv_msg(fileobj) -> Optional[dict]:
3333 return json .loads (line .decode ("utf-8" ))
3434
3535
36- def run_cmd (args , env = None ) -> subprocess .CompletedProcess :
37- return subprocess .run (args , env = env , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True )
36+ def run_cmd (args , env = None , cwd = None ) -> subprocess .CompletedProcess :
37+ return subprocess .run (args , env = env , cwd = cwd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT , text = True )
38+
39+
40+ def resolve_repo_root (value : Optional [str ]) -> Path :
41+ if value :
42+ return Path (value ).expanduser ().resolve ()
43+ return Path (__file__ ).resolve ().parents [1 ]
44+
45+
46+ def has_path_sep (value : str ) -> bool :
47+ return os .sep in value or (os .altsep and os .altsep in value )
48+
49+
50+ def ensure_trakx_binary (bin_arg : str , repo_root : Path ) -> str :
51+ if not has_path_sep (bin_arg ) and not bin_arg .startswith ("." ):
52+ bin_path = repo_root / bin_arg
53+ else :
54+ bin_path = Path (bin_arg )
55+ if not bin_path .is_absolute ():
56+ bin_path = repo_root / bin_path
57+
58+ bin_path .parent .mkdir (parents = True , exist_ok = True )
59+ build = run_cmd (["go" , "build" , "-o" , str (bin_path ), "./cli" ], env = os .environ .copy (), cwd = str (repo_root ))
60+ if build .returncode != 0 :
61+ raise RuntimeError (f"failed to build trakx: { build .stdout .strip ()} " )
62+ return str (bin_path )
3863
3964
4065def wait_for_http (host : str , port : int , timeout : float ) -> bool :
@@ -79,7 +104,7 @@ def clear_cache(cache_dir: str) -> None:
79104def main () -> int :
80105 parser = argparse .ArgumentParser (description = "Trakx benchmark server orchestrator" )
81106 parser .add_argument ("--listen" , default = "0.0.0.0:9077" , help = "control listen address" )
82- parser .add_argument ("--trakx-bin" , default = "trakx" , help = "path to trakx binary" )
107+ parser .add_argument ("--trakx-bin" , default = "bench/bin/ trakx" , help = "path to trakx binary (auto-built every run) " )
83108 parser .add_argument ("--config" , default = "bench/trakx.yaml" , help = "trakx config path" )
84109 parser .add_argument ("--cache-dir" , default = "/tmp/trakx-bench-cache" , help = "cache dir to reset between runs" )
85110 parser .add_argument ("--http-host" , default = "127.0.0.1" , help = "host for readiness check" )
@@ -88,8 +113,14 @@ def main() -> int:
88113 parser .add_argument ("--udp-port" , type = int , default = 1337 , help = "udp port for udp heartbeat" )
89114 parser .add_argument ("--ready-timeout" , type = float , default = 8.0 , help = "seconds to wait for readiness" )
90115 parser .add_argument ("--manual" , action = "store_true" , help = "pause for Enter before each start" )
116+ parser .add_argument ("--repo-root" , default = "" , help = "repo root (default: inferred)" )
91117 args = parser .parse_args ()
92118
119+ repo_root = resolve_repo_root (args .repo_root )
120+ args .trakx_bin = ensure_trakx_binary (args .trakx_bin , repo_root )
121+ if not os .path .isabs (args .config ):
122+ args .config = str ((repo_root / args .config ).resolve ())
123+
93124 listen_host , listen_port = parse_hostport (args .listen )
94125 env_base = os .environ .copy ()
95126 env_base ["TRAKX_CACHE" ] = args .cache_dir
0 commit comments