Skip to content

Commit 54fd0c5

Browse files
committed
Have a decent stat runner at last.
1 parent b22ef4b commit 54fd0c5

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

python/stat_runner.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,59 @@
88
description about the best solution arrived at.
99
"""
1010

11+
import os
1112
import subprocess
1213
import sys
1314

1415
def determine_runner(script_path):
15-
dot_split = script_path.split(".")
16-
return ["ruby", script_path] if dot_split[-1] == "rb" else ["python3", "-m", script_path]
16+
dot_split = script_path.split(os.extsep)
17+
if dot_split[-1] == "rb":
18+
return ["ruby", script_path, "csv"]
19+
else:
20+
return ["python3", "-m", script_path, "csv"]
21+
22+
def write_stat_doc(doc_fname, doc):
23+
with open(doc_fname, "w") as stat_doc:
24+
stat_doc.write(doc)
25+
26+
def __make_data_dir_name(runner, script_path):
27+
parsed_path = []
28+
if runner == "python3":
29+
parsed_path = script_path.split(".")
30+
else:
31+
parsed_path = script_path.split(os.path.sep)
32+
33+
return "-".join(parsed_path)
34+
35+
def make_data_dir_path(runner, script_path):
36+
return os.path.join(os.curdir, "stats", __make_data_dir_name(runner, script_path))
37+
38+
def ensure_data_dir_exists(runner, script_path):
39+
data_dir_path = make_data_dir_path(runner, script_path)
40+
if not os.path.isdir(data_dir_path) and not os.path.isfile(data_dir_path):
41+
print("data dir for %s does not exist, creating..." % script_path)
42+
os.mkdir(data_dir_path)
43+
44+
def write_data_dump(runner, script_path, fname, dump):
45+
data_fname = os.path.join(make_data_dir_path(runner, script_path), fname)
46+
with open(data_fname, "w+") as data:
47+
data.write(dump)
1748

1849
if __name__ == "__main__":
1950
if len(sys.argv) != 3:
2051
print("Usage: python3 %s <script.path> <iters>" % __file__)
2152
exit(1)
22-
else:
23-
running_sum = 0
24-
limit = int(sys.argv[2])
2553

26-
for _ in range(limit):
27-
out = subprocess.run(determine_runner(sys.argv[1]), stdout=subprocess.PIPE)
28-
running_sum += float(out.stdout)
54+
stats_dir = os.path.join(os.curdir, "stats")
55+
if not os.path.isdir(stats_dir) and not os.path.isfile(stats_dir):
56+
print("stats directory does not exist, creating...")
57+
os.mkdir(stats_dir)
58+
59+
running_sum = 0
60+
limit = int(sys.argv[2])
61+
runstring = determine_runner(sys.argv[1])
62+
ensure_data_dir_exists(runstring[0], sys.argv[1])
2963

30-
print("Mean: %s" % (running_sum / limit))
64+
for runcount in range(limit):
65+
out = subprocess.run(runstring, stdout=subprocess.PIPE)
66+
write_data_dump(runstring[0], sys.argv[1], "%s.csv" % (runcount + 1), out.stdout.decode("utf-8"))

0 commit comments

Comments
 (0)