Skip to content

Commit 7f3655b

Browse files
committed
upload mixer script
1 parent ba8cab5 commit 7f3655b

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ __pycache__
55
.DS_Store
66
env/
77
cinder/
8+
Tool_shed/driver/temp/
9+
Tool_shed/driver/results

Tool_shed/driver/mixer.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import argparse
2+
import os
3+
from collections import defaultdict
4+
import json
5+
6+
script_path = os.path.dirname(os.path.realpath(__file__))
7+
8+
def base3(n: int) -> str:
9+
if n == 0:
10+
return '0'
11+
digits = []
12+
while n:
13+
digits.append(str(n % 3))
14+
n //= 3
15+
return ''.join(reversed(digits))
16+
17+
def mixer(benchmark):
18+
file_contents = {}
19+
# open the benchmark files from ../../Benchmark/{benchmark}/{untyped,shallow,advanced}
20+
with open(f'{script_path}/../../Benchmark/{benchmark}/untyped/main.py', 'r') as f:
21+
file_contents["untyped"] = f.read()
22+
with open(f'{script_path}/../../Benchmark/{benchmark}/shallow/main.py', 'r') as f:
23+
file_contents["shallow"] = f.read()
24+
with open(f'{script_path}/../../Benchmark/{benchmark}/advanced/main.py', 'r') as f:
25+
file_contents["advanced"] = f.read()
26+
27+
# partition the files around "### SECTION SEPARATOR ###" and save the partitions
28+
partitions = defaultdict(dict)
29+
for section in file_contents:
30+
for i, partition in enumerate(file_contents[section].split('### SECTION SEPARATOR ###')):
31+
partitions[i][section] = partition
32+
33+
# now yield all possible combinations of partitions
34+
key = {
35+
0: "untyped",
36+
1: "shallow",
37+
2: "advanced"
38+
}
39+
for i in range(3 ** len(partitions) - 1):
40+
selection = base3(i).zfill(len(partitions) - 1)
41+
metadata = {
42+
"selection": selection,
43+
"ratios": {
44+
"untyped": 0,
45+
"shallow": 0,
46+
"advanced": 0
47+
}
48+
}
49+
50+
combination = partitions[0]["advanced"]
51+
for j in range(len(partitions)-1):
52+
segment = partitions[j+1][key[int(selection[j])]]
53+
combination += segment
54+
55+
untyped_segment = partitions[j+1]["untyped"]
56+
metadata["ratios"][key[int(selection[j])]] += len(untyped_segment)
57+
58+
total = sum(metadata["ratios"].values())
59+
metadata["ratios"]["advanced"] /= total
60+
metadata["ratios"]["shallow"] /= total
61+
metadata["ratios"]["untyped"] /= total
62+
63+
yield combination, metadata
64+
65+
if __name__ == '__main__':
66+
parser = argparse.ArgumentParser()
67+
parser.add_argument('benchmark', type=str, help='Benchmark to run')
68+
parser.add_argument('n', type=int, help='Number of iterations')
69+
args = parser.parse_args()
70+
71+
# create a temporary directory to store the benchmark files
72+
os.makedirs(f'{script_path}/temp', exist_ok=True)
73+
74+
# refresh the results file
75+
os.makedirs(f'{script_path}/results', exist_ok=True)
76+
with open(f'{script_path}/results/{args.benchmark}.jsonl', 'w') as f:
77+
pass
78+
79+
for combination, metadata in mixer(args.benchmark):
80+
# write the combination to a file
81+
with open(f'{script_path}/temp/main.py', 'w') as f:
82+
f.write(combination)
83+
84+
try:
85+
# run the benchmark and timeout after 10 * args.n seconds
86+
out = os.popen(f'timeout {10 * args.n}s python {script_path}/temp/main.py {args.n}').read()
87+
time = float(out)
88+
metadata["time"] = time
89+
90+
# save the metadata to results/{benchmark}.json
91+
with open(f'{script_path}/results/{args.benchmark}.jsonl', 'a') as f:
92+
f.write(json.dumps(metadata) + '\n')
93+
except ValueError:
94+
pass
95+
96+
# delete the temporary directory and its contents
97+
os.system(f'rm -r {script_path}/temp')

0 commit comments

Comments
 (0)