|
12 | 12 | import subprocess
|
13 | 13 | import sys
|
14 | 14 |
|
15 |
| -def determine_runner(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"] |
| 15 | +class StatRunner(object): |
| 16 | + |
| 17 | + def __init__(self, script_path, output_encoding="utf-8"): |
| 18 | + self.script_path = script_path |
| 19 | + self.runstring = self.__determine_runner() |
| 20 | + self.runner = self.runstring[0] |
| 21 | + self.output_encoding = output_encoding |
21 | 22 |
|
22 |
| -def write_stat_doc(doc_fname, doc): |
23 |
| - with open(doc_fname, "w") as stat_doc: |
24 |
| - stat_doc.write(doc) |
| 23 | + self.__setup() |
25 | 24 |
|
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) |
| 25 | + def __setup(self): |
| 26 | + stats_dir = os.path.join(os.curdir, "stats") |
| 27 | + if not os.path.isdir(stats_dir) and not os.path.isfile(stats_dir): |
| 28 | + print("stats directory does not exist, creating...") |
| 29 | + os.mkdir(stats_dir) |
| 30 | + self.__ensure_data_dir_exists() |
32 | 31 |
|
33 |
| - return "-".join(parsed_path) |
| 32 | + def __determine_runner(self): |
| 33 | + dot_split = self.script_path.split(os.extsep) |
| 34 | + if dot_split[-1] == "rb": |
| 35 | + return ["ruby", script_path, "csv"] |
| 36 | + else: |
| 37 | + return ["python3", "-m", script_path, "csv"] |
| 38 | + |
| 39 | + def __write_stat_doc(self, doc_fname, doc): |
| 40 | + with open(doc_fname, "w") as stat_doc: |
| 41 | + stat_doc.write(doc) |
| 42 | + |
| 43 | + def __make_data_dir_name(self): |
| 44 | + parsed_path = [] |
| 45 | + if self.runner == "python3": |
| 46 | + parsed_path = self.script_path.split(".") |
| 47 | + else: |
| 48 | + parsed_path = self.script_path.split(os.path.sep) |
| 49 | + |
| 50 | + return "-".join(parsed_path) |
| 51 | + |
| 52 | + def __make_data_dir_path(self): |
| 53 | + return os.path.join(os.curdir, "stats", self.__make_data_dir_name()) |
| 54 | + |
| 55 | + def __ensure_data_dir_exists(self): |
| 56 | + data_dir_path = self.__make_data_dir_path() |
| 57 | + if not os.path.isdir(data_dir_path) and not os.path.isfile(data_dir_path): |
| 58 | + print("data dir for %s does not exist, creating..." % self.script_path) |
| 59 | + os.mkdir(data_dir_path) |
| 60 | + |
| 61 | + def __write_data_dump(self, runner, script_path, fname, dump): |
| 62 | + data_fname = os.path.join(self.__make_data_dir_path(), fname) |
| 63 | + with open(data_fname, "w+") as data: |
| 64 | + data.write(dump) |
34 | 65 |
|
35 |
| -def make_data_dir_path(runner, script_path): |
36 |
| - return os.path.join(os.curdir, "stats", __make_data_dir_name(runner, script_path)) |
| 66 | + def gather_stats(self, iters): |
| 67 | + for runcount in range(iters): |
| 68 | + print("Run #%s for %s" % (runcount + 1, self.script_path)) |
| 69 | + out = subprocess.run(self.runstring, stdout=subprocess.PIPE) |
| 70 | + self.__write_data_dump( |
| 71 | + self.runner, |
| 72 | + self.script_path, |
| 73 | + "%s.csv" % (runcount + 1), |
| 74 | + out.stdout.decode(self.output_encoding) |
| 75 | + ) |
37 | 76 |
|
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) |
| 77 | + print("Done. Find stats in %s" % self.__make_data_dir_path()) |
48 | 78 |
|
49 | 79 | if __name__ == "__main__":
|
50 | 80 | if len(sys.argv) != 3:
|
51 | 81 | print("Usage: python3 %s <script.path> <iters>" % __file__)
|
52 | 82 | exit(1)
|
53 | 83 |
|
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 | 84 | limit = int(sys.argv[2])
|
61 |
| - runstring = determine_runner(sys.argv[1]) |
62 |
| - ensure_data_dir_exists(runstring[0], sys.argv[1]) |
| 85 | + script_path = sys.argv[1] |
63 | 86 |
|
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")) |
| 87 | + runner = StatRunner(script_path) |
| 88 | + runner.gather_stats(limit) |
0 commit comments