-
Notifications
You must be signed in to change notification settings - Fork 5
/
compiler_integration_test.py
275 lines (214 loc) · 8.1 KB
/
compiler_integration_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import os.path
import subprocess
import sys
import time
import pathlib
from typing import List, Set, TextIO, Union, NamedTuple
import time
_results_dir = pathlib.Path("compiler_integration_results")
if not _results_dir.exists():
_results_dir.mkdir()
results_dir = _results_dir / str(time.time())
results_dir.mkdir()
class TestCase(NamedTuple):
filename: str
harmony_args: List[str] = None
lines_to_check: Union[List[int], str] = 'all'
is_ephemeral: bool = False
class ExecutionResult(NamedTuple):
output: str
average_duration: float
class TestResult(NamedTuple):
test_case: TestCase
current: ExecutionResult
baseline: ExecutionResult
class ComparisonResult(NamedTuple):
test_result: TestResult
output_expected: bool
duration_expected: bool
def load_test_cases() -> List[TestCase]:
modules_dir = pathlib.Path("harmony_model_checker") / "modules"
runall_script = pathlib.Path("runall.scr")
code_dir = pathlib.Path("code")
test_cases: List[TestCase] = []
# Ensure our modules compile correctly
tested_files: Set[str] = set()
for f in modules_dir.glob("*.hny"):
tested_files.add(str(f.resolve()))
test_cases.append(TestCase(filename=str(f), harmony_args=['--noweb']))
with runall_script.open("r") as f:
# Exclude the first arg, i.e. the harmony script filename
all_tc = [l.strip().split() for l in f.readlines() if l.startswith("./harmony")]
for tc in all_tc:
filename = pathlib.Path(tc[-1])
if filename.exists():
tested_files.add(str(filename.resolve()))
test_cases.append(TestCase(
filename=str(filename),
harmony_args=tc[1:-1]
))
for f in code_dir.glob("*.hny"):
if str(f.resolve()) not in tested_files and f.exists():
tested_files.add(str(f.resolve()))
test_cases.append(TestCase(filename=str(f), harmony_args=['--noweb']))
return test_cases
TEST_CASES = load_test_cases()
print(f"Number of tests: {len(TEST_CASES)}")
def dump_result(result: ComparisonResult, f: TextIO):
test_case = result.test_result.test_case
baseline = result.test_result.baseline
current = result.test_result.current
f.write('---')
f.write('\n')
f.write(f'## {" ".join(test_case.harmony_args or [])} {test_case.filename}\n')
f.write('\n')
f.write("### Summary\n\n")
f.write(f"Duration is good: {'✅' if result.duration_expected else '❌'}\n\n")
f.write(f"Output is good: {'✅' if result.output_expected else '❌'}\n\n")
if result.output_expected:
return
f.write("### Baseline Output\n\n")
f.write(f"```\n{baseline.output}\n```\n\nDuration: {baseline.average_duration}\n\n")
f.write("### Current Output\n\n")
f.write(f"```\n{current.output}\n```\n\nDuration: {current.average_duration}\n\n")
def dump_results(results: List[ComparisonResult], f: TextIO):
f.write('# Compiler Integration Test Results\n')
f.write('\n')
for r in results:
dump_result(r, f)
f.write('---')
def evaluate_test_case(test_case: TestCase, n: int) -> TestResult:
harmony_args = ' '.join(test_case.harmony_args or [])
filename = test_case.filename
print(f'Evaluating `./harmony {harmony_args} {filename}`')
durations = []
baseline_durations = []
for _ in range(n):
start_time = time.process_time()
result = subprocess.run(
f'./harmony -A {harmony_args} {filename}'.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf8'
)
durations.append(time.process_time() - start_time)
start_time = time.process_time()
baseline_result = subprocess.run(
f'./harmony -f -A {harmony_args or ""} {filename}'.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf8'
)
baseline_durations.append(time.process_time() - start_time)
assert isinstance(result.stdout, str) and isinstance(baseline_result.stdout, str)
average_duration = sum(durations) / len(durations)
average_baseline_duration = sum(baseline_durations) / len(baseline_durations)
stdout = result.stdout.strip()
current_execution = ExecutionResult(stdout, average_duration)
stdout = baseline_result.stdout.strip()
baseline_execution = ExecutionResult(stdout, average_baseline_duration)
return TestResult(
test_case=test_case,
current=current_execution,
baseline=baseline_execution
)
def evaluate_and_compare_test_cases(test_cases: List[TestCase], n: int) -> List[ComparisonResult]:
curr_results = []
for test_case in test_cases:
tr = evaluate_test_case(test_case, n)
curr_results.append(compare_result(tr))
return curr_results
def expected_outputs_match(
test_case: TestCase,
prev_expected_output: str,
curr_expected_output: str
) -> bool:
if isinstance(test_case.lines_to_check, list):
assert test_case.lines_to_check != []
prev_split = prev_expected_output.splitlines()
curr_split = curr_expected_output.splitlines()
for line in test_case.lines_to_check:
assert isinstance(line, int)
if line >= len(prev_split) or line >= len(curr_split):
return False
if prev_split[line] != curr_split[line]:
return False
if prev_expected_output != curr_expected_output:
print(
f'{" ".join(test_case.harmony_args or [])}: Previous and current expected '
f'outputs differ, but merge has been suppressed by '
f'test_case.lines_to_check'
)
print()
return True
elif test_case.lines_to_check == 'all':
return prev_expected_output == curr_expected_output
else:
assert False
def merge_expected_outputs(
result: TestResult
) -> bool:
test_case = result.test_case
prev = result.baseline.output
curr = result.current.output
if expected_outputs_match(
test_case,
prev,
curr
):
return True
harmony_args = " ".join(test_case.harmony_args or [])
basename = os.path.basename(test_case.filename)
print(f'{harmony_args}: difference on expected_output')
print('Previous results expected output:')
print('```')
print(prev, end='')
print('```')
print('Current results expected output:', end='')
print('```')
print(curr)
print('```')
with (results_dir / f"{basename}-{harmony_args}-prev".replace(" ", "_")).open("w") as f:
f.write(prev)
with (results_dir / f"{basename}-{harmony_args}-next".replace(" ", "_")).open("w") as f:
f.write(curr)
return False
def merge_average_durations(
result: TestResult,
) -> bool:
test_case = result.test_case
prev = result.baseline.average_duration
curr = result.current.average_duration
if prev == curr:
return True
percent_difference = 2 * (abs(prev - curr) / (prev + curr))
percent_difference = round(100 * percent_difference, 3)
if percent_difference <= 100:
return True
print(f'{" ".join(test_case.harmony_args or [])}: difference on average duration by {percent_difference}% > 100%')
print(f'Previous duration: {prev}')
print(f'Current duration: {curr}')
print()
return True
def compare_result(
result: TestResult
) -> ComparisonResult:
output_is_expected = merge_expected_outputs(result)
duration_is_expected = merge_average_durations(result)
return ComparisonResult(
test_result=result,
output_expected=output_is_expected,
duration_expected=duration_is_expected
)
def main():
subprocess.run('make all'.split())
print()
if '--full' in sys.argv:
final_results = evaluate_and_compare_test_cases(TEST_CASES, n=10)
else:
final_results = evaluate_and_compare_test_cases(TEST_CASES, n=1)
print()
with open('compiler_integration_results.md', 'w', encoding='utf-8') as f:
dump_results(final_results, f)
if __name__ == '__main__':
main()