|
8 | 8 | description about the best solution arrived at.
|
9 | 9 | """
|
10 | 10 |
|
| 11 | +import os |
11 | 12 | import subprocess
|
12 | 13 | import sys
|
13 | 14 |
|
14 | 15 | 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) |
17 | 48 |
|
18 | 49 | if __name__ == "__main__":
|
19 | 50 | if len(sys.argv) != 3:
|
20 | 51 | print("Usage: python3 %s <script.path> <iters>" % __file__)
|
21 | 52 | exit(1)
|
22 |
| - else: |
23 |
| - running_sum = 0 |
24 |
| - limit = int(sys.argv[2]) |
25 | 53 |
|
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]) |
29 | 63 |
|
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