Skip to content

Commit b60690d

Browse files
committed
refactor(run_performance): use argparse and pathlib for I/O path configuration
1 parent 5a235fb commit b60690d

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

optiphy/tools/run_performance.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import argparse
12
import subprocess
23
import re
34

4-
timings_file = "/tmp/timings.txt"
5-
opticks_file = "/tmp/Opticks.txt"
5+
from pathlib import Path
66

77
run_mac_template = """
88
/run/numberOfThreads {threads}
@@ -29,17 +29,28 @@ def parse_sim_time(output):
2929

3030

3131
def main():
32-
with open(timings_file, "w") as tf, open(opticks_file, "w") as of:
33-
for threads in range(1, 21):
32+
parser = argparse.ArgumentParser()
33+
parser.add_argument('-g', '--gdml', type=Path, default=Path('tests/geom/pfrich_min_FINAL.gdml'), help="Path to a custom GDML geometry file")
34+
parser.add_argument('-o', '--outpath', type=Path, default=Path('./'), help="Path where the output file will be saved")
35+
36+
args = parser.parse_args()
37+
38+
geant_file = args.outpath/"timing_geant.txt"
39+
optix_file = args.outpath/"timing_optix.txt"
40+
41+
with open(geant_file, "w") as gfile, open(optix_file, "w") as ofile:
42+
for threads in range(50, 0, -1):
3443
times = {}
3544
sim_time_true = None
3645
for flag in ['true', 'false']:
3746
# Write run.mac with current flag
3847
with open("run.mac", "w") as rm:
3948
rm.write(run_mac_template.format(threads=threads, flag=flag))
4049
# Run with time in bash to capture real/user/sys
50+
cmd = f"time simg4oxmt -g {args.gdml} -m run.mac"
51+
print(f"Running {threads} threads: {cmd}")
4152
result = subprocess.run(
42-
["bash", "-c", "time simg4oxmt -g tests/geom/pfrich_min_FINAL.gdml -m run.mac"],
53+
["bash", "-c", cmd],
4354
capture_output=True, text=True
4455
)
4556
stdout = result.stdout
@@ -49,8 +60,8 @@ def main():
4960
if flag == 'true':
5061
sim_time_true = parse_sim_time(stdout + stderr)
5162
if sim_time_true is not None:
52-
of.write(f"{threads} {sim_time_true}\n")
53-
of.flush()
63+
ofile.write(f"{threads} {sim_time_true}\n")
64+
ofile.flush()
5465

5566
# Extract real time
5667
real_match = re.search(r"real\s+\d+m[\d.]+s", stderr)
@@ -60,11 +71,11 @@ def main():
6071
else:
6172
print(f"[!] Could not find 'real' time for threads={threads} flag={flag}")
6273

63-
# Write the difference to timings.txt (true - false)
74+
# Write the difference to timing_geant.txt (true - false)
6475
if 'true' in times and 'false' in times:
6576
diff = times['true'] - times['false']
66-
tf.write(f"{threads} {diff}\n")
67-
tf.flush()
77+
gfile.write(f"{threads} {diff}\n")
78+
gfile.flush()
6879
else:
6980
print(f"[!] Missing times for threads={threads}")
7081

0 commit comments

Comments
 (0)